ACE_Log_Msg_NT_Event_Log Class Reference

Implements an ACE_Log_Msg_Backend that logs to the WinNT system event log. More...

#include <Log_Msg_NT_Event_Log.h>

Inheritance diagram for ACE_Log_Msg_NT_Event_Log:

Inheritance graph
[legend]
Collaboration diagram for ACE_Log_Msg_NT_Event_Log:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Log_Msg_NT_Event_Log (void)
 Constructor.

virtual ~ACE_Log_Msg_NT_Event_Log (void)
 Destructor.

virtual int open (const ACE_TCHAR *logger_key)
 Open a new event log.

virtual int reset (void)
 Reset the backend.

virtual int close (void)
 Close the backend completely.

virtual int log (ACE_Log_Record &log_record)
 This is called when we want to log a message.


Private Attributes

HANDLE evlog_handle_

Detailed Description

Implements an ACE_Log_Msg_Backend that logs to the WinNT system event log.

Definition at line 35 of file Log_Msg_NT_Event_Log.h.


Constructor & Destructor Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Log_Msg_NT_Event_Log::ACE_Log_Msg_NT_Event_Log void   ) 
 

Constructor.

Definition at line 17 of file Log_Msg_NT_Event_Log.cpp.

00018   : evlog_handle_(0)
00019 {
00020 }

ACE_Log_Msg_NT_Event_Log::~ACE_Log_Msg_NT_Event_Log void   )  [virtual]
 

Destructor.

Definition at line 22 of file Log_Msg_NT_Event_Log.cpp.

References close().

00023 {
00024   this->close ();
00025 }


Member Function Documentation

int ACE_Log_Msg_NT_Event_Log::close void   )  [virtual]
 

Close the backend completely.

Implements ACE_Log_Msg_Backend.

Definition at line 90 of file Log_Msg_NT_Event_Log.cpp.

References evlog_handle_.

Referenced by reset(), and ~ACE_Log_Msg_NT_Event_Log().

00091 {
00092   if (this->evlog_handle_ == 0
00093       || DeregisterEventSource (this->evlog_handle_))
00094     {
00095       this->evlog_handle_ = 0;
00096       return 0;
00097     }
00098   else
00099     return -1;
00100 }

int ACE_Log_Msg_NT_Event_Log::log ACE_Log_Record log_record  )  [virtual]
 

This is called when we want to log a message.

Implements ACE_Log_Msg_Backend.

Definition at line 103 of file Log_Msg_NT_Event_Log.cpp.

References ACE_TCHAR, ACE_Log_Record::length(), LM_ALERT, LM_CRITICAL, LM_DEBUG, LM_EMERGENCY, LM_ERROR, LM_INFO, LM_NOTICE, LM_SHUTDOWN, LM_STARTUP, LM_TRACE, LM_WARNING, ACE_Log_Record::msg_data(), and ACE_Log_Record::type().

00104 {
00105   // Make a copy of the log text and replace any newlines with
00106   // CR-LF. Newline characters on their own do not appear correctly
00107   // in the event viewer. We allow for a doubling in the size of
00108   // the msg data for the worst case of all newlines.
00109   const ACE_TCHAR* src_msg_data = log_record.msg_data ();
00110   ACE_TCHAR msg_data [ACE_Log_Record::MAXLOGMSGLEN * 2];
00111 
00112   for (long i = 0, j = 0; i < log_record.length (); ++i)
00113     {
00114       if (src_msg_data[i] == '\n')
00115         {
00116           msg_data[j++] = '\r';
00117           msg_data[j++] = '\n';
00118         }
00119       else
00120         msg_data[j++] = src_msg_data[i];
00121     }
00122 
00123   // Map the ACE log record type to an event log type.
00124   WORD event_type;
00125   switch (log_record.type ())
00126   {
00127   case LM_STARTUP:
00128   case LM_SHUTDOWN:
00129   case LM_TRACE:
00130   case LM_DEBUG:
00131   case LM_INFO:
00132     event_type = EVENTLOG_INFORMATION_TYPE;
00133     break;
00134   case LM_NOTICE:
00135   case LM_WARNING:
00136     event_type = EVENTLOG_WARNING_TYPE;
00137     break;
00138   case LM_ERROR:
00139   case LM_CRITICAL:
00140   case LM_ALERT:
00141   case LM_EMERGENCY:
00142   default:
00143     event_type = EVENTLOG_ERROR_TYPE;
00144     break;
00145   }
00146 
00147   // Send the log message to the system event log.
00148   const ACE_TCHAR* msgs [1];
00149   msgs[0] = msg_data;
00150 
00151   if (ACE_TEXT_ReportEvent (this->evlog_handle_,
00152                             event_type, 0, 0, 0, 1, 0, msgs, 0) == 0)
00153     return -1;
00154   else
00155     return 0;
00156 }

int ACE_Log_Msg_NT_Event_Log::open const ACE_TCHAR logger_key  )  [virtual]
 

Open a new event log.

Initialize the event logging facility.

Parameters:
logger_key The name of the calling program. This name is used in the Source field of the event log. If it is 0 (no name), the application name as returned from ACE_Log_Msg::program_name() is used.

Implements ACE_Log_Msg_Backend.

Definition at line 28 of file Log_Msg_NT_Event_Log.cpp.

References ACE_LIB_TEXT, ACE_TCHAR, evlog_handle_, MAXPATHLEN, ACE_Log_Msg::program_name(), ACE_OS::strcpy(), ACE_OS::strlen(), and ACE_OS::strncat().

00029 {
00030   // ACE's "resource module" contains the message resource required
00031   // for event logging.
00032   ACE_TCHAR msg_file [MAXPATHLEN];
00033 
00034   if (!ACE_TEXT_GetModuleFileName (ACE_OS::get_win32_resource_module (),
00035                                    msg_file,
00036                                    MAXPATHLEN))
00037     return -1;
00038   DWORD msg_file_length =
00039     static_cast<DWORD> ((ACE_OS::strlen (msg_file) + 1) * sizeof (ACE_TCHAR));
00040 
00041   // If a logger_key has been supplied then we use that as the event
00042   // source name, otherwise we default to the program name.
00043   const ACE_TCHAR *event_source_name = logger_key ? logger_key : ACE_Log_Msg::program_name ();
00044 
00045   // Information is stored in the registry at a location based on the
00046   // program name.
00047   ACE_TCHAR reg_key [MAXPATHLEN];
00048   ACE_OS::strcpy (reg_key,
00049                   ACE_LIB_TEXT ("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"));
00050   size_t reg_key_length = ACE_OS::strlen(reg_key);
00051   ACE_OS::strncat (reg_key,
00052                    event_source_name,
00053                    MAXPATHLEN - reg_key_length);
00054 
00055   // Add the event source to the registry. Note that if this fails it
00056   // is not fatal. The application will still be able to write entries
00057   // to the event log, they just won't be formatted correctly.
00058   HKEY hkey;
00059   ACE_TEXT_RegCreateKey (HKEY_LOCAL_MACHINE,
00060                          reg_key,
00061                          &hkey);
00062   DWORD flags = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
00063   ACE_TEXT_RegSetValueEx (hkey,
00064                           ACE_LIB_TEXT ("TypesSupported"),
00065                           0,
00066                           REG_DWORD,
00067                           (LPBYTE) &flags,
00068                           sizeof (DWORD));
00069   ACE_TEXT_RegSetValueEx (hkey,
00070                           ACE_LIB_TEXT ("EventMessageFile"),
00071                           0,
00072                           REG_SZ,
00073                           (LPBYTE) msg_file,
00074                           msg_file_length);
00075   RegCloseKey (hkey);
00076 
00077   // Obtain a handle to the event source.
00078   this->evlog_handle_ = ACE_TEXT_RegisterEventSource (0,
00079                                                       event_source_name);
00080   return this->evlog_handle_ ? 0 : -1;
00081 }

int ACE_Log_Msg_NT_Event_Log::reset void   )  [virtual]
 

Reset the backend.

Implements ACE_Log_Msg_Backend.

Definition at line 84 of file Log_Msg_NT_Event_Log.cpp.

References close().

00085 {
00086   return this->close ();
00087 }


Member Data Documentation

HANDLE ACE_Log_Msg_NT_Event_Log::evlog_handle_ [private]
 

Definition at line 64 of file Log_Msg_NT_Event_Log.h.

Referenced by close(), and open().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:24:05 2006 for ACE by doxygen 1.3.6