NTEventLogAppender.cpp
Go to the documentation of this file.
1 /*
2  * NTEventLogAppender.cpp
3  *
4  * Copyright 2002, Log4cpp Project. All rights reserved.
5  *
6  * See the COPYING file for the terms of usage and distribution.
7  */
8 
9 #ifdef WIN32 // only available on Win32
10 
12 #include <log4cpp/FactoryParams.hh>
13 #include <memory>
14 
15 namespace log4cpp {
16 
17  NTEventLogAppender::NTEventLogAppender(const std::string& name, const std::string& sourceName) :
18  AppenderSkeleton(name),
19  _strSourceName(sourceName),
20  _hEventSource(NULL)
21  {
22  open();
23  }
24 
25  NTEventLogAppender::~NTEventLogAppender()
26  {
27  close();
28  }
29 
30  void NTEventLogAppender::open()
31  {
32  addRegistryInfo(_strSourceName.c_str());
33  _hEventSource = ::RegisterEventSource(NULL, _strSourceName.c_str());
34  }
35 
36  void NTEventLogAppender::close()
37  {
38  if (_hEventSource) {
39  ::DeregisterEventSource(_hEventSource);
40  }
41  }
42 
43  bool NTEventLogAppender::reopen() {
44  close();
45  open();
46  return true;
47  }
48 
49  bool NTEventLogAppender::requiresLayout() const {
50  return false;
51  }
52 
53  void NTEventLogAppender::setLayout(Layout* layout) {
54  return;
55  }
56 
57  void NTEventLogAppender::_append(const LoggingEvent& event) {
58  const char* ps[1];
59  ps[0] = event.message.c_str();
60 
61  const DWORD messageID = 0x1000;
62  ::ReportEvent(_hEventSource, getType(event.priority),
63  getCategory(event.priority),
64  messageID, NULL, 1, 0, ps, NULL);
65  }
66 
72  WORD NTEventLogAppender::getCategory(Priority::Value priority) {
73  // Priority values map directly to EventLog category values
74  return (WORD)((priority / 100) + 1);
75  }
76 
82  WORD NTEventLogAppender::getType(Priority::Value priority) {
83 
84  WORD ret_val;
85 
86  switch (priority) {
87  case Priority::EMERG:
88  // FATAL is the same value as EMERG
89 // case Priority::FATAL:
90  case Priority::ALERT:
91  case Priority::CRIT:
92  case Priority::ERROR:
93  ret_val = EVENTLOG_ERROR_TYPE;
94  break;
95  case Priority::WARN:
96  ret_val = EVENTLOG_WARNING_TYPE;
97  break;
98  case Priority::NOTICE:
99  case Priority::INFO:
100  case Priority::DEBUG:
101  default:
102  ret_val = EVENTLOG_INFORMATION_TYPE;
103  break;
104  }
105  return ret_val;
106  }
107 
108  HKEY NTEventLogAppender::regGetKey(TCHAR *subkey, DWORD *disposition) {
109  HKEY hkey = 0;
110  RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, NULL,
111  REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL,
112  &hkey, disposition);
113  return hkey;
114  }
115 
116  void NTEventLogAppender::regSetString(HKEY hkey, TCHAR *name, TCHAR *value) {
117  RegSetValueEx(hkey, name, 0, REG_SZ, (LPBYTE)value, lstrlen(value));
118  }
119 
120  void NTEventLogAppender::regSetDword(HKEY hkey, TCHAR *name, DWORD value) {
121  RegSetValueEx(hkey, name, 0, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
122  }
123 
124  /*
125  * Add this source with appropriate configuration keys to the registry.
126  */
127  void NTEventLogAppender::addRegistryInfo(const char *source) {
128  const TCHAR *prefix = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
129  DWORD disposition;
130  HKEY hkey = 0;
131  TCHAR subkey[256];
132 
133  lstrcpy(subkey, prefix);
134  lstrcat(subkey, source);
135  hkey = regGetKey(subkey, &disposition);
136  if (disposition == REG_CREATED_NEW_KEY) {
137  regSetString(hkey, "EventMessageFile", "NTEventLogAppender.dll");
138  regSetString(hkey, "CategoryMessageFile", "NTEventLogAppender.dll");
139  regSetDword(hkey, "TypesSupported", (DWORD)7);
140  regSetDword(hkey, "CategoryCount", (DWORD)8);
141  }
142  RegCloseKey(hkey);
143  return;
144  }
145 
146  std::auto_ptr<Appender> create_nt_event_log_appender(const FactoryParams& params)
147  {
148  std::string name, source_name;
149  params.get_for("nt event log appender").required("name", name)("source_name", source_name);
150 
151  return std::auto_ptr<Appender>(new NTEventLogAppender(name, source_name));
152  }
153 }
154 
155 #endif // WIN32
std::auto_ptr< Appender > create_nt_event_log_appender(const FactoryParams &)
#define ERROR


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00