4
// $Id: //poco/1.2/Foundation/src/EventLogChannel.cpp#2 $
8
// Module: EventLogChannel
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/EventLogChannel.h"
38
#include "Poco/Message.h"
39
#include "Poco/String.h"
41
#if defined(POCO_WIN32_UTF8)
42
#include "Poco/UnicodeConverter.h"
49
const std::string EventLogChannel::PROP_NAME = "name";
50
const std::string EventLogChannel::PROP_HOST = "host";
51
const std::string EventLogChannel::PROP_LOGHOST = "loghost";
52
const std::string EventLogChannel::PROP_LOGFILE = "logfile";
55
EventLogChannel::EventLogChannel():
56
_logFile("Application"),
59
#if defined(POCO_WIN32_UTF8)
61
int n = GetModuleFileNameW(NULL, name, sizeof(name));
64
wchar_t* end = name + n - 1;
65
while (end > name && *end != '\\') --end;
66
if (*end == '\\') ++end;
67
std::wstring uname(end);
68
UnicodeConverter::toUTF8(uname, _name);
72
int n = GetModuleFileName(NULL, name, sizeof(name));
75
char* end = name + n - 1;
76
while (end > name && *end != '\\') --end;
77
if (*end == '\\') ++end;
84
EventLogChannel::EventLogChannel(const std::string& name):
86
_logFile("Application"),
92
EventLogChannel::EventLogChannel(const std::string& name, const std::string& host):
95
_logFile("Application"),
101
EventLogChannel::~EventLogChannel()
107
void EventLogChannel::open()
110
#if defined(POCO_WIN32_UTF8)
112
UnicodeConverter::toUTF16(_host, uhost);
114
UnicodeConverter::toUTF16(_name, uname);
115
_h = RegisterEventSourceW(uhost.empty() ? NULL : uhost.c_str(), uname.c_str());
117
_h = RegisterEventSource(_host.empty() ? NULL : _host.c_str(), _name.c_str());
119
if (!_h) throw SystemException("cannot register event source");
123
void EventLogChannel::close()
125
if (_h) DeregisterEventSource(_h);
130
void EventLogChannel::log(const Message& msg)
133
#if defined(POCO_WIN32_UTF8)
135
UnicodeConverter::toUTF16(msg.getText(), utext);
136
const wchar_t* pMsg = utext.c_str();
137
ReportEventW(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL);
139
const char* pMsg = msg.getText().c_str();
140
ReportEvent(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL);
145
void EventLogChannel::setProperty(const std::string& name, const std::string& value)
147
if (icompare(name, PROP_NAME) == 0)
149
else if (icompare(name, PROP_HOST) == 0)
151
else if (icompare(name, PROP_LOGHOST) == 0)
153
else if (icompare(name, PROP_LOGFILE) == 0)
156
Channel::setProperty(name, value);
160
std::string EventLogChannel::getProperty(const std::string& name) const
162
if (icompare(name, PROP_NAME) == 0)
164
else if (icompare(name, PROP_HOST) == 0)
166
else if (icompare(name, PROP_LOGHOST) == 0)
168
else if (icompare(name, PROP_LOGFILE) == 0)
171
return Channel::getProperty(name);
175
int EventLogChannel::getType(const Message& msg)
177
switch (msg.getPriority())
179
case Message::PRIO_TRACE:
180
case Message::PRIO_DEBUG:
181
case Message::PRIO_INFORMATION:
182
return EVENTLOG_INFORMATION_TYPE;
183
case Message::PRIO_NOTICE:
184
case Message::PRIO_WARNING:
185
return EVENTLOG_WARNING_TYPE;
187
return EVENTLOG_ERROR_TYPE;
192
int EventLogChannel::getCategory(const Message& msg)
194
switch (msg.getPriority())
196
case Message::PRIO_TRACE:
197
return POCO_CTG_TRACE;
198
case Message::PRIO_DEBUG:
199
return POCO_CTG_DEBUG;
200
case Message::PRIO_INFORMATION:
201
return POCO_CTG_INFORMATION;
202
case Message::PRIO_NOTICE:
203
return POCO_CTG_NOTICE;
204
case Message::PRIO_WARNING:
205
return POCO_CTG_WARNING;
206
case Message::PRIO_ERROR:
207
return POCO_CTG_ERROR;
208
case Message::PRIO_CRITICAL:
209
return POCO_CTG_CRITICAL;
210
case Message::PRIO_FATAL:
211
return POCO_CTG_FATAL;
218
void EventLogChannel::setUpRegistry() const
220
std::string key = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\";
221
key.append(_logFile);
226
#if defined(POCO_WIN32_UTF8)
228
UnicodeConverter::toUTF16(key, ukey);
229
DWORD rc = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ukey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp);
231
DWORD rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp);
233
if (rc != ERROR_SUCCESS) return;
235
if (disp == REG_CREATED_NEW_KEY)
237
#if defined(POCO_WIN32_UTF8)
239
#if defined(POCO_DLL)
241
path = findLibrary(L"PocoFoundationd.dll");
243
path = findLibrary(L"PocoFoundation.dll");
248
path = findLibrary(L"PocoMsg.dll");
251
#if defined(POCO_DLL)
253
path = findLibrary("PocoFoundationd.dll");
255
path = findLibrary("PocoFoundation.dll");
260
path = findLibrary("PocoMsg.dll");
267
#if defined(POCO_WIN32_UTF8)
268
RegSetValueExW(hKey, L"CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), (DWORD) path.size() + 1);
269
RegSetValueExW(hKey, L"EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), (DWORD) path.size() + 1);
270
RegSetValueExW(hKey, L"CategoryCount", 0, REG_DWORD, (const BYTE*) &count, (DWORD) sizeof(count));
271
RegSetValueExW(hKey, L"TypesSupported", 0, REG_DWORD, (const BYTE*) &types, (DWORD) sizeof(types));
273
RegSetValueEx(hKey, "CategoryMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), (DWORD) path.size() + 1);
274
RegSetValueEx(hKey, "EventMessageFile", 0, REG_SZ, (const BYTE*) path.c_str(), (DWORD) path.size() + 1);
275
RegSetValueEx(hKey, "CategoryCount", 0, REG_DWORD, (const BYTE*) &count, (DWORD) sizeof(count));
276
RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (const BYTE*) &types, (DWORD) sizeof(types));
284
#if defined(POCO_WIN32_UTF8)
285
std::wstring EventLogChannel::findLibrary(const wchar_t* name)
288
HMODULE dll = LoadLibraryW(name);
291
wchar_t name[MAX_PATH + 1];
292
int n = GetModuleFileNameW(dll, name, sizeof(name));
293
if (n > 0) path = name;
299
std::string EventLogChannel::findLibrary(const char* name)
302
HMODULE dll = LoadLibrary(name);
305
char name[MAX_PATH + 1];
306
int n = GetModuleFileName(dll, name, sizeof(name));
307
if (n > 0) path = name;