EventLogChannel.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // EventLogChannel.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/EventLogChannel.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: EventLogChannel
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/EventLogChannel.h"
  16. #include "Poco/Message.h"
  17. #include "Poco/String.h"
  18. #include "pocomsg.h"
  19. #if defined(POCO_WIN32_UTF8)
  20. #include "Poco/UnicodeConverter.h"
  21. #endif
  22. namespace Poco {
  23. const std::string EventLogChannel::PROP_NAME = "name";
  24. const std::string EventLogChannel::PROP_HOST = "host";
  25. const std::string EventLogChannel::PROP_LOGHOST = "loghost";
  26. const std::string EventLogChannel::PROP_LOGFILE = "logfile";
  27. EventLogChannel::EventLogChannel():
  28. _logFile("Application"),
  29. _h(0)
  30. {
  31. const DWORD maxPathLen = MAX_PATH + 1;
  32. #if defined(POCO_WIN32_UTF8)
  33. wchar_t name[maxPathLen];
  34. int n = GetModuleFileNameW(NULL, name, maxPathLen);
  35. if (n > 0)
  36. {
  37. wchar_t* end = name + n - 1;
  38. while (end > name && *end != '\\') --end;
  39. if (*end == '\\') ++end;
  40. std::wstring uname(end);
  41. UnicodeConverter::toUTF8(uname, _name);
  42. }
  43. #else
  44. char name[maxPathLen];
  45. int n = GetModuleFileNameA(NULL, name, maxPathLen);
  46. if (n > 0)
  47. {
  48. char* end = name + n - 1;
  49. while (end > name && *end != '\\') --end;
  50. if (*end == '\\') ++end;
  51. _name = end;
  52. }
  53. #endif
  54. }
  55. EventLogChannel::EventLogChannel(const std::string& name):
  56. _name(name),
  57. _logFile("Application"),
  58. _h(0)
  59. {
  60. }
  61. EventLogChannel::EventLogChannel(const std::string& name, const std::string& host):
  62. _name(name),
  63. _host(host),
  64. _logFile("Application"),
  65. _h(0)
  66. {
  67. }
  68. EventLogChannel::~EventLogChannel()
  69. {
  70. try
  71. {
  72. close();
  73. }
  74. catch (...)
  75. {
  76. poco_unexpected();
  77. }
  78. }
  79. void EventLogChannel::open()
  80. {
  81. setUpRegistry();
  82. #if defined(POCO_WIN32_UTF8)
  83. std::wstring uhost;
  84. UnicodeConverter::toUTF16(_host, uhost);
  85. std::wstring uname;
  86. UnicodeConverter::toUTF16(_name, uname);
  87. _h = RegisterEventSourceW(uhost.empty() ? NULL : uhost.c_str(), uname.c_str());
  88. #else
  89. _h = RegisterEventSource(_host.empty() ? NULL : _host.c_str(), _name.c_str());
  90. #endif
  91. if (!_h) throw SystemException("cannot register event source");
  92. }
  93. void EventLogChannel::close()
  94. {
  95. if (_h) DeregisterEventSource(_h);
  96. _h = 0;
  97. }
  98. void EventLogChannel::log(const Message& msg)
  99. {
  100. if (!_h) open();
  101. #if defined(POCO_WIN32_UTF8)
  102. std::wstring utext;
  103. UnicodeConverter::toUTF16(msg.getText(), utext);
  104. const wchar_t* pMsg = utext.c_str();
  105. ReportEventW(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL);
  106. #else
  107. const char* pMsg = msg.getText().c_str();
  108. ReportEvent(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL);
  109. #endif
  110. }
  111. void EventLogChannel::setProperty(const std::string& name, const std::string& value)
  112. {
  113. if (icompare(name, PROP_NAME) == 0)
  114. _name = value;
  115. else if (icompare(name, PROP_HOST) == 0)
  116. _host = value;
  117. else if (icompare(name, PROP_LOGHOST) == 0)
  118. _host = value;
  119. else if (icompare(name, PROP_LOGFILE) == 0)
  120. _logFile = value;
  121. else
  122. Channel::setProperty(name, value);
  123. }
  124. std::string EventLogChannel::getProperty(const std::string& name) const
  125. {
  126. if (icompare(name, PROP_NAME) == 0)
  127. return _name;
  128. else if (icompare(name, PROP_HOST) == 0)
  129. return _host;
  130. else if (icompare(name, PROP_LOGHOST) == 0)
  131. return _host;
  132. else if (icompare(name, PROP_LOGFILE) == 0)
  133. return _logFile;
  134. else
  135. return Channel::getProperty(name);
  136. }
  137. int EventLogChannel::getType(const Message& msg)
  138. {
  139. switch (msg.getPriority())
  140. {
  141. case Message::PRIO_TRACE:
  142. case Message::PRIO_DEBUG:
  143. case Message::PRIO_INFORMATION:
  144. return EVENTLOG_INFORMATION_TYPE;
  145. case Message::PRIO_NOTICE:
  146. case Message::PRIO_WARNING:
  147. return EVENTLOG_WARNING_TYPE;
  148. default:
  149. return EVENTLOG_ERROR_TYPE;
  150. }
  151. }
  152. int EventLogChannel::getCategory(const Message& msg)
  153. {
  154. switch (msg.getPriority())
  155. {
  156. case Message::PRIO_TRACE:
  157. return POCO_CTG_TRACE;
  158. case Message::PRIO_DEBUG:
  159. return POCO_CTG_DEBUG;
  160. case Message::PRIO_INFORMATION:
  161. return POCO_CTG_INFORMATION;
  162. case Message::PRIO_NOTICE:
  163. return POCO_CTG_NOTICE;
  164. case Message::PRIO_WARNING:
  165. return POCO_CTG_WARNING;
  166. case Message::PRIO_ERROR:
  167. return POCO_CTG_ERROR;
  168. case Message::PRIO_CRITICAL:
  169. return POCO_CTG_CRITICAL;
  170. case Message::PRIO_FATAL:
  171. return POCO_CTG_FATAL;
  172. default:
  173. return 0;
  174. }
  175. }
  176. void EventLogChannel::setUpRegistry() const
  177. {
  178. std::string key = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\";
  179. key.append(_logFile);
  180. key.append("\\");
  181. key.append(_name);
  182. HKEY hKey;
  183. DWORD disp;
  184. #if defined(POCO_WIN32_UTF8)
  185. std::wstring ukey;
  186. UnicodeConverter::toUTF16(key, ukey);
  187. DWORD rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ukey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp);
  188. #else
  189. DWORD rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp);
  190. #endif
  191. if (rc != ERROR_SUCCESS) return;
  192. if (disp == REG_CREATED_NEW_KEY)
  193. {
  194. #if defined(POCO_WIN32_UTF8)
  195. std::wstring path;
  196. #if defined(POCO_DLL)
  197. #if defined(_DEBUG)
  198. #if defined(_WIN64)
  199. path = findLibrary(L"PocoFoundation64d.dll");
  200. #else
  201. path = findLibrary(L"PocoFoundationd.dll");
  202. #endif
  203. #else
  204. #if defined(_WIN64)
  205. path = findLibrary(L"PocoFoundation64.dll");
  206. #else
  207. path = findLibrary(L"PocoFoundation.dll");
  208. #endif
  209. #endif
  210. #endif
  211. if (path.empty())
  212. path = findLibrary(L"PocoMsg.dll");
  213. #else
  214. std::string path;
  215. #if defined(POCO_DLL)
  216. #if defined(_DEBUG)
  217. #if defined(_WIN64)
  218. path = findLibrary("PocoFoundation64d.dll");
  219. #else
  220. path = findLibrary("PocoFoundationd.dll");
  221. #endif
  222. #else
  223. #if defined(_WIN64)
  224. path = findLibrary("PocoFoundation64.dll");
  225. #else
  226. path = findLibrary("PocoFoundation.dll");
  227. #endif
  228. #endif
  229. #endif
  230. if (path.empty())
  231. path = findLibrary("PocoMsg.dll");
  232. #endif
  233. if (!path.empty())
  234. {
  235. DWORD count = 8;
  236. DWORD types = 7;
  237. #if defined(POCO_WIN32_UTF8)
  238. RegSetValueExW(hKey, L"CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(sizeof(wchar_t)*(path.size() + 1)));
  239. RegSetValueExW(hKey, L"EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(sizeof(wchar_t)*(path.size() + 1)));
  240. RegSetValueExW(hKey, L"CategoryCount", 0, REG_DWORD, (const BYTE*) &count, static_cast<DWORD>(sizeof(count)));
  241. RegSetValueExW(hKey, L"TypesSupported", 0, REG_DWORD, (const BYTE*) &types, static_cast<DWORD>(sizeof(types)));
  242. #else
  243. RegSetValueEx(hKey, "CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(path.size() + 1));
  244. RegSetValueEx(hKey, "EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), static_cast<DWORD>(path.size() + 1));
  245. RegSetValueEx(hKey, "CategoryCount", 0, REG_DWORD, (const BYTE*) &count, static_cast<DWORD>(sizeof(count)));
  246. RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (const BYTE*) &types, static_cast<DWORD>(sizeof(types)));
  247. #endif
  248. }
  249. }
  250. RegCloseKey(hKey);
  251. }
  252. #if defined(POCO_WIN32_UTF8)
  253. std::wstring EventLogChannel::findLibrary(const wchar_t* name)
  254. {
  255. std::wstring path;
  256. HMODULE dll = LoadLibraryW(name);
  257. if (dll)
  258. {
  259. const DWORD maxPathLen = MAX_PATH + 1;
  260. wchar_t name[maxPathLen];
  261. int n = GetModuleFileNameW(dll, name, maxPathLen);
  262. if (n > 0) path = name;
  263. FreeLibrary(dll);
  264. }
  265. return path;
  266. }
  267. #else
  268. std::string EventLogChannel::findLibrary(const char* name)
  269. {
  270. std::string path;
  271. HMODULE dll = LoadLibraryA(name);
  272. if (dll)
  273. {
  274. const DWORD maxPathLen = MAX_PATH + 1;
  275. char name[maxPathLen];
  276. int n = GetModuleFileNameA(dll, name, maxPathLen);
  277. if (n > 0) path = name;
  278. FreeLibrary(dll);
  279. }
  280. return path;
  281. }
  282. #endif
  283. } // namespace Poco