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 ()
19 ACE_Log_Msg_NT_Event_Log::open (const ACE_TCHAR
*logger_key
)
21 // ACE's "resource module" contains the message resource required
23 ACE_TCHAR msg_file
[MAXPATHLEN
];
25 if (!ACE_TEXT_GetModuleFileName (ACE_OS::get_win32_resource_module (),
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
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
,
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.
50 ACE_TEXT_RegCreateKey (HKEY_LOCAL_MACHINE
,
53 DWORD flags
= EVENTLOG_ERROR_TYPE
| EVENTLOG_WARNING_TYPE
| EVENTLOG_INFORMATION_TYPE
;
54 ACE_TEXT_RegSetValueEx (hkey
,
55 ACE_TEXT ("TypesSupported"),
60 ACE_TEXT_RegSetValueEx (hkey
,
61 ACE_TEXT ("EventMessageFile"),
68 // Obtain a handle to the event source.
69 this->evlog_handle_
= ACE_TEXT_RegisterEventSource (0,
71 return this->evlog_handle_
? 0 : -1;
75 ACE_Log_Msg_NT_Event_Log::reset ()
77 return this->close ();
81 ACE_Log_Msg_NT_Event_Log::close ()
83 if (this->evlog_handle_
== 0
84 || DeregisterEventSource (this->evlog_handle_
))
86 this->evlog_handle_
= 0;
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 ();
108 for (size_t i
= 0, j
= 0;
112 if (src_msg_data
[i
] == '\n')
114 msg_data
[j
++] = '\r';
115 msg_data
[j
++] = '\n';
118 msg_data
[j
++] = src_msg_data
[i
];
122 msg_data
[end
] = '\0';
124 // Map the ACE log record type to an event log type.
126 switch (log_record
.type ())
133 event_type
= EVENTLOG_INFORMATION_TYPE
;
137 event_type
= EVENTLOG_WARNING_TYPE
;
144 event_type
= EVENTLOG_ERROR_TYPE
;
148 // Send the log message to the system event log.
149 const ACE_TCHAR
* msgs
[1];
152 if (ACE_TEXT_ReportEvent (this->evlog_handle_
,
153 event_type
, 0, 0, 0, 1, 0, msgs
, 0) == 0)
159 ACE_END_VERSIONED_NAMESPACE_DECL
161 #endif /* ACE_HAS_LOG_MSG_NT_EVENT_LOG */