Log_Msg_NT_Event_Log.cpp

Go to the documentation of this file.
00001 // Log_Msg_NT_Event_Log.cpp,v 4.16 2005/10/28 16:14:53 ossama Exp
00002 
00003 #include "ace/config-all.h"
00004 
00005 #if defined (ACE_HAS_LOG_MSG_NT_EVENT_LOG)
00006 
00007 #include "ace/Log_Msg_NT_Event_Log.h"
00008 #include "ace/Log_Msg.h"
00009 #include "ace/Log_Record.h"
00010 #include "ace/OS_NS_stdio.h"
00011 #include "ace/OS_NS_string.h"
00012 
00013 ACE_RCSID(ace, Log_Msg_NT_Event_Log, "Log_Msg_NT_Event_Log.cpp,v 4.16 2005/10/28 16:14:53 ossama Exp")
00014 
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016 
00017 ACE_Log_Msg_NT_Event_Log::ACE_Log_Msg_NT_Event_Log (void)
00018   : evlog_handle_(0)
00019 {
00020 }
00021 
00022 ACE_Log_Msg_NT_Event_Log::~ACE_Log_Msg_NT_Event_Log (void)
00023 {
00024   this->close ();
00025 }
00026 
00027 int
00028 ACE_Log_Msg_NT_Event_Log::open (const ACE_TCHAR *logger_key)
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 }
00082 
00083 int
00084 ACE_Log_Msg_NT_Event_Log::reset (void)
00085 {
00086   return this->close ();
00087 }
00088 
00089 int
00090 ACE_Log_Msg_NT_Event_Log::close (void)
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 }
00101 
00102 int
00103 ACE_Log_Msg_NT_Event_Log::log (ACE_Log_Record &log_record)
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 }
00157 
00158 ACE_END_VERSIONED_NAMESPACE_DECL
00159 
00160 #endif /* ACE_HAS_LOG_MSG_NT_EVENT_LOG */

Generated on Thu Nov 9 09:41:53 2006 for ACE by doxygen 1.3.6