summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/EventLoggerI.cpp
blob: 69a59f89173c3c7dd5ac46ee0318381cadeb22ad (plain)
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
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Ice/EventLoggerI.h>
#include <Ice/EventLoggerMsg.h>
#include <Ice/LocalException.h>

using namespace std;

HMODULE Ice::EventLoggerI::_module = NULL;

Ice::EventLoggerI::EventLoggerI(const string& appName) : 
    _appName(appName), _source(NULL)
{
    //
    // 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;

    //
    // Create the key if it doesn't already exist.
    //
    HKEY hKey;
    DWORD d;
    LONG err;
    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)
    {
        SyscallException ex(__FILE__, __LINE__);
        ex.error = err;
        throw ex;
    }

    //
    // Get the filename of this DLL.
    //
    char path[_MAX_PATH];
    assert(_module != NULL);
    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, (unsigned char*)path, 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, (unsigned char*)&typesSupported, sizeof(typesSupported));
    if(err != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        SyscallException ex(__FILE__, __LINE__);
        ex.error = err;
        throw ex;
    }

    RegCloseKey(hKey);

    _source = RegisterEventSource(NULL, appName.c_str());
    if(_source == NULL)
    {
        SyscallException ex(__FILE__, __LINE__);
        ex.error = GetLastError();
        throw ex;
    }
}

Ice::EventLoggerI::~EventLoggerI()
{
    if(_source != NULL)
    {
        DeregisterEventSource(_source);
    }
}

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, NULL, 1, 0, str, NULL))
    {
        SyscallException ex(__FILE__, __LINE__);
        ex.error = GetLastError();
        throw ex;
    }
}

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, NULL, 1, 0, str, NULL))
    {
        SyscallException ex(__FILE__, __LINE__);
        ex.error = GetLastError();
        throw ex;
    }
}

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, NULL, 1, 0, str, NULL))
    {
        SyscallException ex(__FILE__, __LINE__);
        ex.error = GetLastError();
        throw ex;
    }
}

void
Ice::EventLoggerI::setModuleHandle(HMODULE module)
{
    _module = module;
}