~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/src/EventLogChannel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// EventLogChannel.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/src/EventLogChannel.cpp#2 $
 
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
// 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:
 
19
// 
 
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.
 
26
// 
 
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.
 
34
//
 
35
 
 
36
 
 
37
#include "Poco/EventLogChannel.h"
 
38
#include "Poco/Message.h"
 
39
#include "Poco/String.h"
 
40
#include "pocomsg.h"
 
41
#if defined(POCO_WIN32_UTF8)
 
42
#include "Poco/UnicodeConverter.h"
 
43
#endif
 
44
 
 
45
 
 
46
namespace Poco {
 
47
 
 
48
 
 
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";
 
53
 
 
54
 
 
55
EventLogChannel::EventLogChannel(): 
 
56
        _logFile("Application"),
 
57
        _h(0)
 
58
{
 
59
#if defined(POCO_WIN32_UTF8)
 
60
        wchar_t name[256];
 
61
        int n = GetModuleFileNameW(NULL, name, sizeof(name));
 
62
        if (n > 0)
 
63
        {
 
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);
 
69
        }
 
70
#else
 
71
        char name[256];
 
72
        int n = GetModuleFileName(NULL, name, sizeof(name));
 
73
        if (n > 0)
 
74
        {
 
75
                char* end = name + n - 1;
 
76
                while (end > name && *end != '\\') --end;
 
77
                if (*end == '\\') ++end;
 
78
                _name = end;
 
79
        }
 
80
#endif
 
81
}
 
82
 
 
83
 
 
84
EventLogChannel::EventLogChannel(const std::string& name): 
 
85
        _name(name), 
 
86
        _logFile("Application"),
 
87
        _h(0)
 
88
{
 
89
}
 
90
 
 
91
 
 
92
EventLogChannel::EventLogChannel(const std::string& name, const std::string& host): 
 
93
        _name(name), 
 
94
        _host(host),
 
95
        _logFile("Application"),
 
96
        _h(0)
 
97
{
 
98
}
 
99
 
 
100
 
 
101
EventLogChannel::~EventLogChannel()
 
102
{
 
103
        close();
 
104
}
 
105
 
 
106
 
 
107
void EventLogChannel::open()
 
108
{
 
109
        setUpRegistry();
 
110
#if defined(POCO_WIN32_UTF8)
 
111
        std::wstring uhost;
 
112
        UnicodeConverter::toUTF16(_host, uhost);
 
113
        std::wstring uname;
 
114
        UnicodeConverter::toUTF16(_name, uname);
 
115
        _h = RegisterEventSourceW(uhost.empty() ? NULL : uhost.c_str(), uname.c_str());
 
116
#else
 
117
        _h = RegisterEventSource(_host.empty() ? NULL : _host.c_str(), _name.c_str());
 
118
#endif
 
119
        if (!_h) throw SystemException("cannot register event source");
 
120
}
 
121
 
 
122
 
 
123
void EventLogChannel::close()
 
124
{
 
125
        if (_h) DeregisterEventSource(_h);
 
126
        _h = 0;
 
127
}
 
128
 
 
129
 
 
130
void EventLogChannel::log(const Message& msg)
 
131
{
 
132
        if (!_h) open();
 
133
#if defined(POCO_WIN32_UTF8)
 
134
        std::wstring utext;
 
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); 
 
138
#else
 
139
        const char* pMsg = msg.getText().c_str();
 
140
        ReportEvent(_h, getType(msg), getCategory(msg), POCO_MSG_LOG, NULL, 1, 0, &pMsg, NULL); 
 
141
#endif
 
142
}
 
143
 
 
144
 
 
145
void EventLogChannel::setProperty(const std::string& name, const std::string& value)
 
146
{
 
147
        if (icompare(name, PROP_NAME) == 0)
 
148
                _name = value;
 
149
        else if (icompare(name, PROP_HOST) == 0)
 
150
                _host = value;
 
151
        else if (icompare(name, PROP_LOGHOST) == 0)
 
152
                _host = value;
 
153
        else if (icompare(name, PROP_LOGFILE) == 0)
 
154
                _logFile = value;
 
155
        else
 
156
                Channel::setProperty(name, value);
 
157
}
 
158
 
 
159
 
 
160
std::string EventLogChannel::getProperty(const std::string& name) const
 
161
{
 
162
        if (icompare(name, PROP_NAME) == 0)
 
163
                return _name;
 
164
        else if (icompare(name, PROP_HOST) == 0)
 
165
                return _host;
 
166
        else if (icompare(name, PROP_LOGHOST) == 0)
 
167
                return _host;
 
168
        else if (icompare(name, PROP_LOGFILE) == 0)
 
169
                return _logFile;
 
170
        else
 
171
                return Channel::getProperty(name);
 
172
}
 
173
 
 
174
 
 
175
int EventLogChannel::getType(const Message& msg)
 
176
{
 
177
        switch (msg.getPriority())
 
178
        {
 
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;
 
186
        default:
 
187
                return EVENTLOG_ERROR_TYPE;
 
188
        }
 
189
}
 
190
 
 
191
 
 
192
int EventLogChannel::getCategory(const Message& msg)
 
193
{
 
194
        switch (msg.getPriority())
 
195
        {
 
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;
 
212
        default:
 
213
                return 0;
 
214
        }
 
215
}
 
216
 
 
217
 
 
218
void EventLogChannel::setUpRegistry() const
 
219
{
 
220
        std::string key = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\";
 
221
        key.append(_logFile);
 
222
        key.append("\\");
 
223
        key.append(_name);
 
224
        HKEY hKey;
 
225
        DWORD disp;
 
226
#if defined(POCO_WIN32_UTF8)
 
227
        std::wstring ukey;
 
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);
 
230
#else
 
231
        DWORD rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disp);
 
232
#endif
 
233
        if (rc != ERROR_SUCCESS) return;
 
234
        
 
235
        if (disp == REG_CREATED_NEW_KEY)
 
236
        {
 
237
#if defined(POCO_WIN32_UTF8)
 
238
                std::wstring path;
 
239
                #if defined(POCO_DLL)
 
240
                        #if defined(_DEBUG)
 
241
                                path = findLibrary(L"PocoFoundationd.dll");
 
242
                        #else
 
243
                                path = findLibrary(L"PocoFoundation.dll");
 
244
                        #endif
 
245
                #endif
 
246
                
 
247
                if (path.empty())
 
248
                        path = findLibrary(L"PocoMsg.dll");
 
249
#else
 
250
                std::string path;
 
251
                #if defined(POCO_DLL)
 
252
                        #if defined(_DEBUG)
 
253
                                path = findLibrary("PocoFoundationd.dll");
 
254
                        #else
 
255
                                path = findLibrary("PocoFoundation.dll");
 
256
                        #endif
 
257
                #endif
 
258
                
 
259
                if (path.empty())
 
260
                        path = findLibrary("PocoMsg.dll");
 
261
#endif
 
262
                
 
263
                if (!path.empty())
 
264
                {
 
265
                        DWORD count = 8;
 
266
                        DWORD types = 7;
 
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));
 
272
#else
 
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));
 
277
#endif
 
278
                }
 
279
        }
 
280
        RegCloseKey(hKey);
 
281
}
 
282
 
 
283
 
 
284
#if defined(POCO_WIN32_UTF8)
 
285
std::wstring EventLogChannel::findLibrary(const wchar_t* name)
 
286
{
 
287
        std::wstring path;
 
288
        HMODULE dll = LoadLibraryW(name);
 
289
        if (dll)
 
290
        {
 
291
                wchar_t name[MAX_PATH + 1];
 
292
                int n = GetModuleFileNameW(dll, name, sizeof(name));
 
293
                if (n > 0) path = name;
 
294
                FreeLibrary(dll);
 
295
        }
 
296
        return path;
 
297
}
 
298
#else
 
299
std::string EventLogChannel::findLibrary(const char* name)
 
300
{
 
301
        std::string path;
 
302
        HMODULE dll = LoadLibrary(name);
 
303
        if (dll)
 
304
        {
 
305
                char name[MAX_PATH + 1];
 
306
                int n = GetModuleFileName(dll, name, sizeof(name));
 
307
                if (n > 0) path = name;
 
308
                FreeLibrary(dll);
 
309
        }
 
310
        return path;
 
311
}
 
312
#endif
 
313
 
 
314
 
 
315
} // namespace Poco