Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Log_Msg_NT_Event_Log.cpp
blob2614600d7bc52919be43d23aacf080fd67b924c6
1 #include "ace/config-all.h"
3 #if defined (ACE_HAS_LOG_MSG_NT_EVENT_LOG)
5 #include "ace/Log_Msg_NT_Event_Log.h"
6 #include "ace/Log_Category.h"
7 #include "ace/Log_Record.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/OS_NS_string.h"
11 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
13 ACE_Log_Msg_NT_Event_Log::~ACE_Log_Msg_NT_Event_Log ()
15 this->close ();
18 int
19 ACE_Log_Msg_NT_Event_Log::open (const ACE_TCHAR *logger_key)
21 // ACE's "resource module" contains the message resource required
22 // for event logging.
23 ACE_TCHAR msg_file [MAXPATHLEN];
25 if (!ACE_TEXT_GetModuleFileName (ACE_OS::get_win32_resource_module (),
26 msg_file,
27 MAXPATHLEN))
28 return -1;
29 DWORD msg_file_length =
30 static_cast<DWORD> ((ACE_OS::strlen (msg_file) + 1) * sizeof (ACE_TCHAR));
32 // If a logger_key has been supplied then we use that as the event
33 // source name, otherwise we default to the program name.
34 const ACE_TCHAR *event_source_name = logger_key ? logger_key : ACE_Log_Msg::program_name ();
36 // Information is stored in the registry at a location based on the
37 // program name.
38 ACE_TCHAR reg_key [MAXPATHLEN];
39 ACE_OS::strcpy (reg_key,
40 ACE_TEXT ("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"));
41 size_t reg_key_length = ACE_OS::strlen(reg_key);
42 ACE_OS::strncat (reg_key,
43 event_source_name,
44 MAXPATHLEN - reg_key_length);
46 // Add the event source to the registry. Note that if this fails it
47 // is not fatal. The application will still be able to write entries
48 // to the event log, they just won't be formatted correctly.
49 HKEY hkey;
50 ACE_TEXT_RegCreateKey (HKEY_LOCAL_MACHINE,
51 reg_key,
52 &hkey);
53 DWORD flags = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
54 ACE_TEXT_RegSetValueEx (hkey,
55 ACE_TEXT ("TypesSupported"),
57 REG_DWORD,
58 (LPBYTE) &flags,
59 sizeof (DWORD));
60 ACE_TEXT_RegSetValueEx (hkey,
61 ACE_TEXT ("EventMessageFile"),
63 REG_SZ,
64 (LPBYTE) msg_file,
65 msg_file_length);
66 RegCloseKey (hkey);
68 // Obtain a handle to the event source.
69 this->evlog_handle_ = ACE_TEXT_RegisterEventSource (0,
70 event_source_name);
71 return this->evlog_handle_ ? 0 : -1;
74 int
75 ACE_Log_Msg_NT_Event_Log::reset ()
77 return this->close ();
80 int
81 ACE_Log_Msg_NT_Event_Log::close ()
83 if (this->evlog_handle_ == 0
84 || DeregisterEventSource (this->evlog_handle_))
86 this->evlog_handle_ = 0;
87 return 0;
89 else
90 return -1;
93 ssize_t
94 ACE_Log_Msg_NT_Event_Log::log (ACE_Log_Record &log_record)
96 // Make a copy of the log text and replace any newlines with
97 // CR-LF. Newline characters on their own do not appear correctly in
98 // the event viewer. We allow for a doubling in the size of the msg
99 // data for the worst case of all newlines.
100 const ACE_TCHAR *src_msg_data = log_record.msg_data ();
101 ACE_TCHAR msg_data [(ACE_Log_Record::MAXLOGMSGLEN * 2) + 1];
103 size_t maxlen = ACE_Log_Record::MAXLOGMSGLEN;
104 if (ACE_Log_Record::MAXLOGMSGLEN > log_record.msg_data_len ())
105 maxlen = log_record.msg_data_len ();
107 size_t end = 0;
108 for (size_t i = 0, j = 0;
109 i < maxlen;
110 ++i)
112 if (src_msg_data[i] == '\n')
114 msg_data[j++] = '\r';
115 msg_data[j++] = '\n';
117 else
118 msg_data[j++] = src_msg_data[i];
120 end = j;
122 msg_data[end] = '\0';
124 // Map the ACE log record type to an event log type.
125 WORD event_type;
126 switch (log_record.type ())
128 case LM_STARTUP:
129 case LM_SHUTDOWN:
130 case LM_TRACE:
131 case LM_DEBUG:
132 case LM_INFO:
133 event_type = EVENTLOG_INFORMATION_TYPE;
134 break;
135 case LM_NOTICE:
136 case LM_WARNING:
137 event_type = EVENTLOG_WARNING_TYPE;
138 break;
139 case LM_ERROR:
140 case LM_CRITICAL:
141 case LM_ALERT:
142 case LM_EMERGENCY:
143 default:
144 event_type = EVENTLOG_ERROR_TYPE;
145 break;
148 // Send the log message to the system event log.
149 const ACE_TCHAR* msgs [1];
150 msgs[0] = msg_data;
152 if (ACE_TEXT_ReportEvent (this->evlog_handle_,
153 event_type, 0, 0, 0, 1, 0, msgs, 0) == 0)
154 return -1;
155 else
156 return 0;
159 ACE_END_VERSIONED_NAMESPACE_DECL
161 #endif /* ACE_HAS_LOG_MSG_NT_EVENT_LOG */