1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/StringUtil.h>
#include <Ice/EventLoggerI.h>
#include <Ice/EventLoggerMsg.h>
#include <Ice/LocalException.h>
#include <IceUtil/StaticMutex.h>
using namespace std;
HMODULE Ice::EventLoggerI::_module = 0;
static IceUtil::StaticMutex outputMutex = ICE_STATIC_MUTEX_INITIALIZER;
Ice::EventLoggerI::EventLoggerI(const string& theAppName) :
_source(0)
{
string appName = theAppName;
if(appName.empty())
{
InitializationException ex(__FILE__, __LINE__);
ex.reason = "event logger requires a value for Ice.ProgramName";
throw ex;
}
//
// We first need to ensure that there is a registry entry for this application
// under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application.
// The application name cannot contain backslashes.
//
string::size_type pos = 0;
while((pos = appName.find('\\', pos)) != string::npos)
{
appName[pos] = '/';
}
string key = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" + appName;
HKEY hKey;
DWORD d;
//
// Try to create the key and set the values.
//
LONG err = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0,
&hKey, &d);
if(err == ERROR_SUCCESS)
{
//
// Get the filename of this DLL.
//
char path[_MAX_PATH];
assert(_module != 0);
if(!GetModuleFileName(_module, path, _MAX_PATH))
{
RegCloseKey(hKey);
SyscallException ex(__FILE__, __LINE__);
ex.error = GetLastError();
throw ex;
}
//
// The event resources are bundled into this DLL, therefore the
// "EventMessageFile" key should contain the path to this DLL.
//
err = RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ,
reinterpret_cast<unsigned char*>(path), static_cast<DWORD>(strlen(path) + 1));
if(err != ERROR_SUCCESS)
{
RegCloseKey(hKey);
SyscallException ex(__FILE__, __LINE__);
ex.error = err;
throw ex;
}
//
// The "TypesSupported" key indicates the supported event types.
//
DWORD typesSupported = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
err = RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, reinterpret_cast<unsigned char*>(&typesSupported),
sizeof(typesSupported));
if(err != ERROR_SUCCESS)
{
RegCloseKey(hKey);
SyscallException ex(__FILE__, __LINE__);
ex.error = err;
throw ex;
}
RegCloseKey(hKey);
}
//
// The event source must match the registry key.
//
_source = RegisterEventSource(0, appName.c_str());
if(_source == 0)
{
SyscallException ex(__FILE__, __LINE__);
ex.error = GetLastError();
throw ex;
}
}
Ice::EventLoggerI::~EventLoggerI()
{
assert(_source != 0);
DeregisterEventSource(_source);
}
void
Ice::EventLoggerI::print(const string& message)
{
const char* str[1];
str[0] = message.c_str();
if(!ReportEvent(_source, EVENTLOG_INFORMATION_TYPE, 0, EVENT_LOGGER_MSG, 0, 1, 0, str, 0))
{
IceUtil::StaticMutex::Lock sync(outputMutex);
cerr << "ReportEvent failed `" << IceUtilInternal::errorToString(GetLastError()) << "':\n" << message << endl;
}
}
void
Ice::EventLoggerI::trace(const string& category, const string& message)
{
string s;
if(!category.empty())
{
s = category;
s.append(": ");
}
s.append(message);
const char* str[1];
str[0] = s.c_str();
if(!ReportEvent(_source, EVENTLOG_INFORMATION_TYPE, 0, EVENT_LOGGER_MSG, 0, 1, 0, str, 0))
{
IceUtil::StaticMutex::Lock sync(outputMutex);
cerr << "ReportEvent failed `" << IceUtilInternal::errorToString(GetLastError()) << "':\n" << message << endl;
}
}
void
Ice::EventLoggerI::warning(const string& message)
{
const char* str[1];
str[0] = message.c_str();
if(!ReportEvent(_source, EVENTLOG_WARNING_TYPE, 0, EVENT_LOGGER_MSG, 0, 1, 0, str, 0))
{
IceUtil::StaticMutex::Lock sync(outputMutex);
cerr << "ReportEvent failed `" << IceUtilInternal::errorToString(GetLastError()) << "':\n" << message << endl;
}
}
void
Ice::EventLoggerI::error(const string& message)
{
const char* str[1];
str[0] = message.c_str();
if(!ReportEvent(_source, EVENTLOG_ERROR_TYPE, 0, EVENT_LOGGER_MSG, 0, 1, 0, str, 0))
{
IceUtil::StaticMutex::Lock sync(outputMutex);
cerr << "ReportEvent failed `" << IceUtilInternal::errorToString(GetLastError()) << "':\n" << message << endl;
}
}
void
Ice::EventLoggerI::setModuleHandle(HMODULE module)
{
_module = module;
}
|