diff options
author | alexv-smirnov <alex@ydb.tech> | 2022-12-20 00:50:48 +0300 |
---|---|---|
committer | alexv-smirnov <alex@ydb.tech> | 2022-12-20 00:50:48 +0300 |
commit | 84f2cfa253cc618438ed6e9d68b33fa7c0d88cb9 (patch) | |
tree | f0cf2236e0aafb3e437199f1ac7b559e7fad554a /contrib/libs/poco/Foundation/src | |
parent | bde6febc1ad3b826e72746de21d7250803e8e0b5 (diff) | |
download | ydb-84f2cfa253cc618438ed6e9d68b33fa7c0d88cb9.tar.gz |
add windows platform to ydb github export
Diffstat (limited to 'contrib/libs/poco/Foundation/src')
-rw-r--r-- | contrib/libs/poco/Foundation/src/EventLogChannel.cpp | 327 | ||||
-rw-r--r-- | contrib/libs/poco/Foundation/src/WindowsConsoleChannel.cpp | 302 | ||||
-rw-r--r-- | contrib/libs/poco/Foundation/src/pocomsg.h | 158 |
3 files changed, 787 insertions, 0 deletions
diff --git a/contrib/libs/poco/Foundation/src/EventLogChannel.cpp b/contrib/libs/poco/Foundation/src/EventLogChannel.cpp new file mode 100644 index 0000000000..5e95f6d1b6 --- /dev/null +++ b/contrib/libs/poco/Foundation/src/EventLogChannel.cpp @@ -0,0 +1,327 @@ +// +// EventLogChannel.cpp +// +// Library: Foundation +// Package: Logging +// Module: EventLogChannel +// +// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/EventLogChannel.h" +#include "Poco/Message.h" +#include "Poco/String.h" +#include "pocomsg.h" +#if defined(POCO_WIN32_UTF8) +#include "Poco/UnicodeConverter.h" +#endif + + +namespace Poco { + + +const std::string EventLogChannel::PROP_NAME = "name"; +const std::string EventLogChannel::PROP_HOST = "host"; +const std::string EventLogChannel::PROP_LOGHOST = "loghost"; +const std::string EventLogChannel::PROP_LOGFILE = "logfile"; + + +EventLogChannel::EventLogChannel(): + _logFile("Application"), + _h(0) +{ + const DWORD maxPathLen = MAX_PATH + 1; +#if defined(POCO_WIN32_UTF8) + wchar_t name[maxPathLen]; + int n = GetModuleFileNameW(NULL, name, maxPathLen); + if (n > 0) + { + wchar_t* end = name + n - 1; + while (end > name && *end != '\\') --end; + if (*end == '\\') ++end; + std::wstring uname(end); + UnicodeConverter::toUTF8(uname, _name); + } +#else + char name[maxPathLen]; + int n = GetModuleFileNameA(NULL, name, maxPathLen); + if (n > 0) + { + char* end = name + n - 1; + while (end > name && *end != '\\') --end; + if (*end == '\\') ++end; + _name = end; + } +#endif +} + + +EventLogChannel::EventLogChannel(const std::string& name): + _name(name), + _logFile("Application"), + _h(0) +{ +} + + +EventLogChannel::EventLogChannel(const std::string& name, const std::string& host): + _name(name), + _host(host), + _logFile("Application"), + _h(0) +{ +} + + +EventLogChannel::~EventLogChannel() +{ + try + { + close(); + } + catch (...) + { + poco_unexpected(); + } +} + + +void EventLogChannel::open() +{ + setUpRegistry(); +#if defined(POCO_WIN32_UTF8) + std::wstring uhost; + UnicodeConverter::toUTF16(_host, uhost); + std::wstring uname; + UnicodeConverter::toUTF16(_name, uname); + _h = RegisterEventSourceW(uhost.empty() ? NULL : uhost.c_str(), uname.c_str()); +#else + _h = RegisterEventSource(_host.empty() ? NULL : _host.c_str(), _name.c_str()); +#endif + if (!_h) throw SystemException("cannot register event source"); +} + + +void EventLogChannel::close() +{ + if (_h) DeregisterEventSource(_h); + _h = 0; +} + + +void EventLogChannel::log(const Message& msg) +{ + if (!_h) open(); +#if defined(POCO_WIN32_UTF8) + std::wstring utext; + UnicodeConverter::toUTF16(msg.getText(), utext); + const wchar_t* pMsg = utext.c_str(); + ReportEventW(_h, + static_cast<WORD>(getType(msg)), + static_cast<WORD>(getCategory(msg)), + POCO_MSG_LOG, + NULL, + 1, + 0, + &pMsg, + NULL); +#else + const char* pMsg = msg.getText().c_str(); + ReportEvent(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL); +#endif +} + + +void EventLogChannel::setProperty(const std::string& name, const std::string& value) +{ + if (icompare(name, PROP_NAME) == 0) + _name = value; + else if (icompare(name, PROP_HOST) == 0) + _host = value; + else if (icompare(name, PROP_LOGHOST) == 0) + _host = value; + else if (icompare(name, PROP_LOGFILE) == 0) + _logFile = value; + else + Channel::setProperty(name, value); +} + + +std::string EventLogChannel::getProperty(const std::string& name) const +{ + if (icompare(name, PROP_NAME) == 0) + return _name; + else if (icompare(name, PROP_HOST) == 0) + return _host; + else if (icompare(name, PROP_LOGHOST) == 0) + return _host; + else if (icompare(name, PROP_LOGFILE) == 0) + return _logFile; + else + return Channel::getProperty(name); +} + + +int EventLogChannel::getType(const Message& msg) +{ + switch (msg.getPriority()) + { + case Message::PRIO_TRACE: + case Message::PRIO_DEBUG: + case Message::PRIO_INFORMATION: + return EVENTLOG_INFORMATION_TYPE; + case Message::PRIO_NOTICE: + case Message::PRIO_WARNING: + return EVENTLOG_WARNING_TYPE; + default: + return EVENTLOG_ERROR_TYPE; + } +} + + +int EventLogChannel::getCategory(const Message& msg) +{ + switch (msg.getPriority()) + { + case Message::PRIO_TRACE: + return POCO_CTG_TRACE; + case Message::PRIO_DEBUG: + return POCO_CTG_DEBUG; + case Message::PRIO_INFORMATION: + return POCO_CTG_INFORMATION; + case Message::PRIO_NOTICE: + return POCO_CTG_NOTICE; + case Message::PRIO_WARNING: + return POCO_CTG_WARNING; + case Message::PRIO_ERROR: + return POCO_CTG_ERROR; + case Message::PRIO_CRITICAL: + return POCO_CTG_CRITICAL; + case Message::PRIO_FATAL: + return POCO_CTG_FATAL; + default: + return 0; + } +} + + +void EventLogChannel::setUpRegistry() const +{ + std::string key = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\"; + key.append(_logFile); + key.append("\\"); + key.append(_name); + HKEY hKey; + DWORD disp; +#if defined(POCO_WIN32_UTF8) + std::wstring ukey; + UnicodeConverter::toUTF16(key, ukey); + DWORD rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ukey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp); +#else + DWORD rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp); +#endif + if (rc != ERROR_SUCCESS) return; + + if (disp == REG_CREATED_NEW_KEY) + { +#if defined(POCO_WIN32_UTF8) + std::wstring path; + #if defined(POCO_DLL) + #if defined(_DEBUG) + #if defined(_WIN64) + path = findLibrary(L"PocoFoundation64d.dll"); + #else + path = findLibrary(L"PocoFoundationd.dll"); + #endif + #else + #if defined(_WIN64) + path = findLibrary(L"PocoFoundation64.dll"); + #else + path = findLibrary(L"PocoFoundation.dll"); + #endif + #endif + #endif + + if (path.empty()) + path = findLibrary(L"PocoMsg.dll"); +#else + std::string path; + #if defined(POCO_DLL) + #if defined(_DEBUG) + #if defined(_WIN64) + path = findLibrary("PocoFoundation64d.dll"); + #else + path = findLibrary("PocoFoundationd.dll"); + #endif + #else + #if defined(_WIN64) + path = findLibrary("PocoFoundation64.dll"); + #else + path = findLibrary("PocoFoundation.dll"); + #endif + #endif + #endif + + if (path.empty()) + path = findLibrary("PocoMsg.dll"); +#endif + + if (!path.empty()) + { + DWORD count = 8; + DWORD types = 7; +#if defined(POCO_WIN32_UTF8) + RegSetValueExW(hKey, L"CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(sizeof(wchar_t)*(path.size() + 1))); + RegSetValueExW(hKey, L"EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(sizeof(wchar_t)*(path.size() + 1))); + RegSetValueExW(hKey, L"CategoryCount", 0, REG_DWORD, (const BYTE*) &count, static_cast<DWORD>(sizeof(count))); + RegSetValueExW(hKey, L"TypesSupported", 0, REG_DWORD, (const BYTE*) &types, static_cast<DWORD>(sizeof(types))); +#else + RegSetValueEx(hKey, "CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(path.size() + 1)); + RegSetValueEx(hKey, "EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(path.size() + 1)); + RegSetValueEx(hKey, "CategoryCount", 0, REG_DWORD, (const BYTE*) &count, static_cast<DWORD>(sizeof(count))); + RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (const BYTE*) &types, static_cast<DWORD>(sizeof(types))); +#endif + } + } + RegCloseKey(hKey); +} + + +#if defined(POCO_WIN32_UTF8) +std::wstring EventLogChannel::findLibrary(const wchar_t* name) +{ + std::wstring path; + HMODULE dll = LoadLibraryW(name); + if (dll) + { + const DWORD maxPathLen = MAX_PATH + 1; + wchar_t moduleName[maxPathLen]; + int n = GetModuleFileNameW(dll, moduleName, maxPathLen); + if (n > 0) path = moduleName; + FreeLibrary(dll); + } + return path; +} +#else +std::string EventLogChannel::findLibrary(const char* name) +{ + std::string path; + HMODULE dll = LoadLibraryA(name); + if (dll) + { + const DWORD maxPathLen = MAX_PATH + 1; + char name[maxPathLen]; + int n = GetModuleFileNameA(dll, name, maxPathLen); + if (n > 0) path = name; + FreeLibrary(dll); + } + return path; +} +#endif + + +} // namespace Poco diff --git a/contrib/libs/poco/Foundation/src/WindowsConsoleChannel.cpp b/contrib/libs/poco/Foundation/src/WindowsConsoleChannel.cpp new file mode 100644 index 0000000000..07e352935f --- /dev/null +++ b/contrib/libs/poco/Foundation/src/WindowsConsoleChannel.cpp @@ -0,0 +1,302 @@ +// +// WindowsConsoleChannel.cpp +// +// Library: Foundation +// Package: Logging +// Module: WindowsConsoleChannel +// +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/WindowsConsoleChannel.h" +#include "Poco/Message.h" +#if defined(POCO_WIN32_UTF8) +#include "Poco/UnicodeConverter.h" +#endif +#include "Poco/String.h" +#include "Poco/Exception.h" + + +namespace Poco { + + +WindowsConsoleChannel::WindowsConsoleChannel(): + _isFile(false), + _hConsole(INVALID_HANDLE_VALUE) +{ + _hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + // check whether the console has been redirected + DWORD mode; + _isFile = (GetConsoleMode(_hConsole, &mode) == 0); +} + + +WindowsConsoleChannel::~WindowsConsoleChannel() +{ +} + + +void WindowsConsoleChannel::log(const Message& msg) +{ + std::string text = msg.getText(); + text += "\r\n"; + +#if defined(POCO_WIN32_UTF8) + if (_isFile) + { + DWORD written; + WriteFile(_hConsole, text.data(), static_cast<DWORD>(text.size()), &written, NULL); + } + else + { + std::wstring utext; + UnicodeConverter::toUTF16(text, utext); + DWORD written; + WriteConsoleW(_hConsole, utext.data(), static_cast<DWORD>(utext.size()), &written, NULL); + } +#else + DWORD written; + WriteFile(_hConsole, text.data(), text.size(), &written, NULL); +#endif +} + + +WindowsColorConsoleChannel::WindowsColorConsoleChannel(): + _enableColors(true), + _isFile(false), + _hConsole(INVALID_HANDLE_VALUE) +{ + _hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + // check whether the console has been redirected + DWORD mode; + _isFile = (GetConsoleMode(_hConsole, &mode) == 0); + initColors(); +} + + +WindowsColorConsoleChannel::~WindowsColorConsoleChannel() +{ +} + + +void WindowsColorConsoleChannel::log(const Message& msg) +{ + std::string text = msg.getText(); + text += "\r\n"; + + if (_enableColors && !_isFile) + { + WORD attr = _colors[0]; + attr &= 0xFFF0; + attr |= _colors[msg.getPriority()]; + SetConsoleTextAttribute(_hConsole, attr); + } + +#if defined(POCO_WIN32_UTF8) + if (_isFile) + { + DWORD written; + WriteFile(_hConsole, text.data(), static_cast<DWORD>(text.size()), &written, NULL); + } + else + { + std::wstring utext; + UnicodeConverter::toUTF16(text, utext); + DWORD written; + WriteConsoleW(_hConsole, utext.data(), static_cast<DWORD>(utext.size()), &written, NULL); + } +#else + DWORD written; + WriteFile(_hConsole, text.data(), text.size(), &written, NULL); +#endif + + if (_enableColors && !_isFile) + { + SetConsoleTextAttribute(_hConsole, _colors[0]); + } +} + + +void WindowsColorConsoleChannel::setProperty(const std::string& name, const std::string& value) +{ + if (name == "enableColors") + { + _enableColors = icompare(value, "true") == 0; + } + else if (name == "traceColor") + { + _colors[Message::PRIO_TRACE] = parseColor(value); + } + else if (name == "debugColor") + { + _colors[Message::PRIO_DEBUG] = parseColor(value); + } + else if (name == "informationColor") + { + _colors[Message::PRIO_INFORMATION] = parseColor(value); + } + else if (name == "noticeColor") + { + _colors[Message::PRIO_NOTICE] = parseColor(value); + } + else if (name == "warningColor") + { + _colors[Message::PRIO_WARNING] = parseColor(value); + } + else if (name == "errorColor") + { + _colors[Message::PRIO_ERROR] = parseColor(value); + } + else if (name == "criticalColor") + { + _colors[Message::PRIO_CRITICAL] = parseColor(value); + } + else if (name == "fatalColor") + { + _colors[Message::PRIO_FATAL] = parseColor(value); + } + else + { + Channel::setProperty(name, value); + } +} + + +std::string WindowsColorConsoleChannel::getProperty(const std::string& name) const +{ + if (name == "enableColors") + { + return _enableColors ? "true" : "false"; + } + else if (name == "traceColor") + { + return formatColor(_colors[Message::PRIO_TRACE]); + } + else if (name == "debugColor") + { + return formatColor(_colors[Message::PRIO_DEBUG]); + } + else if (name == "informationColor") + { + return formatColor(_colors[Message::PRIO_INFORMATION]); + } + else if (name == "noticeColor") + { + return formatColor(_colors[Message::PRIO_NOTICE]); + } + else if (name == "warningColor") + { + return formatColor(_colors[Message::PRIO_WARNING]); + } + else if (name == "errorColor") + { + return formatColor(_colors[Message::PRIO_ERROR]); + } + else if (name == "criticalColor") + { + return formatColor(_colors[Message::PRIO_CRITICAL]); + } + else if (name == "fatalColor") + { + return formatColor(_colors[Message::PRIO_FATAL]); + } + else + { + return Channel::getProperty(name); + } +} + + +WORD WindowsColorConsoleChannel::parseColor(const std::string& color) const +{ + if (icompare(color, "default") == 0) + return _colors[0]; + else if (icompare(color, "black") == 0) + return CC_BLACK; + else if (icompare(color, "red") == 0) + return CC_RED; + else if (icompare(color, "green") == 0) + return CC_GREEN; + else if (icompare(color, "brown") == 0) + return CC_BROWN; + else if (icompare(color, "blue") == 0) + return CC_BLUE; + else if (icompare(color, "magenta") == 0) + return CC_MAGENTA; + else if (icompare(color, "cyan") == 0) + return CC_CYAN; + else if (icompare(color, "gray") == 0) + return CC_GRAY; + else if (icompare(color, "darkGray") == 0) + return CC_DARKGRAY; + else if (icompare(color, "lightRed") == 0) + return CC_LIGHTRED; + else if (icompare(color, "lightGreen") == 0) + return CC_LIGHTGREEN; + else if (icompare(color, "yellow") == 0) + return CC_YELLOW; + else if (icompare(color, "lightBlue") == 0) + return CC_LIGHTBLUE; + else if (icompare(color, "lightMagenta") == 0) + return CC_LIGHTMAGENTA; + else if (icompare(color, "lightCyan") == 0) + return CC_LIGHTCYAN; + else if (icompare(color, "white") == 0) + return CC_WHITE; + else throw InvalidArgumentException("Invalid color value", color); +} + + +std::string WindowsColorConsoleChannel::formatColor(WORD color) const +{ + switch (color) + { + case CC_BLACK: return "black"; + case CC_RED: return "red"; + case CC_GREEN: return "green"; + case CC_BROWN: return "brown"; + case CC_BLUE: return "blue"; + case CC_MAGENTA: return "magenta"; + case CC_CYAN: return "cyan"; + case CC_GRAY: return "gray"; + case CC_DARKGRAY: return "darkGray"; + case CC_LIGHTRED: return "lightRed"; + case CC_LIGHTGREEN: return "lightGreen"; + case CC_YELLOW: return "yellow"; + case CC_LIGHTBLUE: return "lightBlue"; + case CC_LIGHTMAGENTA: return "lightMagenta"; + case CC_LIGHTCYAN: return "lightCyan"; + case CC_WHITE: return "white"; + default: return "invalid"; + } +} + + +void WindowsColorConsoleChannel::initColors() +{ + if (!_isFile) + { + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(_hConsole, &csbi); + _colors[0] = csbi.wAttributes; + } + else + { + _colors[0] = CC_WHITE; + } + _colors[Message::PRIO_FATAL] = CC_LIGHTRED; + _colors[Message::PRIO_CRITICAL] = CC_LIGHTRED; + _colors[Message::PRIO_ERROR] = CC_LIGHTRED; + _colors[Message::PRIO_WARNING] = CC_YELLOW; + _colors[Message::PRIO_NOTICE] = _colors[0]; + _colors[Message::PRIO_INFORMATION] = _colors[0]; + _colors[Message::PRIO_DEBUG] = CC_GRAY; + _colors[Message::PRIO_TRACE] = CC_GRAY; +} + + +} // namespace Poco diff --git a/contrib/libs/poco/Foundation/src/pocomsg.h b/contrib/libs/poco/Foundation/src/pocomsg.h new file mode 100644 index 0000000000..fe68ae436f --- /dev/null +++ b/contrib/libs/poco/Foundation/src/pocomsg.h @@ -0,0 +1,158 @@ +// +// pocomsg.mc[.h] +// +// $Id: //poco/1.4/Foundation/src/pocomsg.mc#1 $ +// +// The Poco message source/header file. +// +// NOTE: pocomsg.h is automatically generated from pocomsg.mc. +// Never edit pocomsg.h directly! +// +// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// +// Categories +// +// +// Values are 32 bit values laid out as follows: +// +// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 +// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 +// +---+-+-+-----------------------+-------------------------------+ +// |Sev|C|R| Facility | Code | +// +---+-+-+-----------------------+-------------------------------+ +// +// where +// +// Sev - is the severity code +// +// 00 - Success +// 01 - Informational +// 10 - Warning +// 11 - Error +// +// C - is the Customer code flag +// +// R - is a reserved bit +// +// Facility - is the facility code +// +// Code - is the facility's status code +// +// +// Define the facility codes +// + + +// +// Define the severity codes +// + + +// +// MessageId: POCO_CTG_FATAL +// +// MessageText: +// +// Fatal +// +#define POCO_CTG_FATAL 0x00000001L + +// +// MessageId: POCO_CTG_CRITICAL +// +// MessageText: +// +// Critical +// +#define POCO_CTG_CRITICAL 0x00000002L + +// +// MessageId: POCO_CTG_ERROR +// +// MessageText: +// +// Error +// +#define POCO_CTG_ERROR 0x00000003L + +// +// MessageId: POCO_CTG_WARNING +// +// MessageText: +// +// Warning +// +#define POCO_CTG_WARNING 0x00000004L + +// +// MessageId: POCO_CTG_NOTICE +// +// MessageText: +// +// Notice +// +#define POCO_CTG_NOTICE 0x00000005L + +// +// MessageId: POCO_CTG_INFORMATION +// +// MessageText: +// +// Information +// +#define POCO_CTG_INFORMATION 0x00000006L + +// +// MessageId: POCO_CTG_DEBUG +// +// MessageText: +// +// Debug +// +#define POCO_CTG_DEBUG 0x00000007L + +// +// MessageId: POCO_CTG_TRACE +// +// MessageText: +// +// Trace +// +#define POCO_CTG_TRACE 0x00000008L + +// +// Event Identifiers +// +// +// MessageId: POCO_MSG_LOG +// +// MessageText: +// +// %1 +// +#define POCO_MSG_LOG 0x00001000L + |