Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Log_Msg_NT_Event_Log.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Log_Msg_NT_Event_Log.cpp,v 1.1.1.2.2.1 2003/03/13 19:44:21 chad Exp $
00003 
00004 #include "ace/config-all.h"
00005 
00006 #if defined (WIN32) && !defined (ACE_HAS_WINCE) && !defined (ACE_HAS_PHARLAP)
00007 
00008 #include "ace/Log_Msg_NT_Event_Log.h"
00009 #include "ace/Log_Msg.h"
00010 #include "ace/Log_Record.h"
00011 
00012 ACE_RCSID(ace, Log_Msg_NT_Event_Log, "$Id: Log_Msg_NT_Event_Log.cpp,v 1.1.1.2.2.1 2003/03/13 19:44:21 chad Exp $")
00013 
00014 ACE_Log_Msg_NT_Event_Log::ACE_Log_Msg_NT_Event_Log (void)
00015   : evlog_handle_(0)
00016 {
00017 }
00018 
00019 ACE_Log_Msg_NT_Event_Log::~ACE_Log_Msg_NT_Event_Log (void)
00020 {
00021   this->close ();
00022 }
00023 
00024 int
00025 ACE_Log_Msg_NT_Event_Log::open (const ACE_TCHAR *logger_key)
00026 {
00027   // ACE's "resource module" contains the message resource required
00028   // for event logging.
00029   ACE_TCHAR msg_file [MAXPATHLEN];
00030 
00031   if (!ACE_TEXT_GetModuleFileName (ACE_OS::get_win32_resource_module (),
00032                                    msg_file,
00033                                    MAXPATHLEN))
00034     return -1;
00035   DWORD msg_file_length = ACE_static_cast (DWORD,
00036                                            ACE_OS_String::strlen (msg_file));
00037 
00038   // If a logger_key has been supplied then we use that as the event
00039   // source name, otherwise we default to the program name.
00040   const ACE_TCHAR *event_source_name = logger_key ? logger_key : ACE_Log_Msg::program_name ();
00041 
00042   // Information is stored in the registry at a location based on the
00043   // program name.
00044   ACE_TCHAR reg_key [MAXPATHLEN];
00045   ACE_OS::strcpy (reg_key,
00046                   ACE_LIB_TEXT ("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"));
00047   size_t reg_key_length = ACE_OS::strlen(reg_key);
00048   ACE_OS::strncat (reg_key,
00049                    event_source_name,
00050                    MAXPATHLEN - reg_key_length);
00051 
00052   // Add the event source to the registry. Note that if this fails it
00053   // is not fatal. The application will still be able to write entries
00054   // to the event log, they just won't be formatted correctly.
00055   HKEY hkey;
00056   ACE_TEXT_RegCreateKey (HKEY_LOCAL_MACHINE,
00057                          reg_key,
00058                          &hkey);
00059   DWORD flags = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
00060   ACE_TEXT_RegSetValueEx (hkey,
00061                           ACE_LIB_TEXT ("TypesSupported"),
00062                           0,
00063                           REG_DWORD,
00064                           (LPBYTE) &flags,
00065                           sizeof (DWORD));
00066   ACE_TEXT_RegSetValueEx (hkey,
00067                           ACE_LIB_TEXT ("EventMessageFile"),
00068                           0,
00069                           REG_SZ,
00070                           (LPBYTE) msg_file,
00071                           msg_file_length + 1);
00072   RegCloseKey (hkey);
00073 
00074   // Obtain a handle to the event source.
00075   this->evlog_handle_ = ACE_TEXT_RegisterEventSource (0,
00076                                                       event_source_name);
00077   return this->evlog_handle_ ? 0 : -1;
00078 }
00079 
00080 int
00081 ACE_Log_Msg_NT_Event_Log::reset (void)
00082 {
00083   return this->close ();
00084 }
00085 
00086 int
00087 ACE_Log_Msg_NT_Event_Log::close (void)
00088 {
00089   if (DeregisterEventSource (this->evlog_handle_))
00090     {
00091       this->evlog_handle_ = 0;
00092       return 0;
00093     }
00094   else
00095     return -1;
00096 }
00097 
00098 int
00099 ACE_Log_Msg_NT_Event_Log::log (ACE_Log_Record &log_record)
00100 {
00101   // Make a copy of the log text and replace any newlines with
00102   // CR-LF. Newline characters on their own do not appear correctly
00103   // in the event viewer. We allow for a doubling in the size of
00104   // the msg data for the worst case of all newlines.
00105   const ACE_TCHAR* src_msg_data = log_record.msg_data ();
00106   ACE_TCHAR msg_data [ACE_Log_Record::MAXLOGMSGLEN * 2];
00107 
00108   for (long i = 0, j = 0; i < log_record.length (); ++i)
00109     {
00110       if (src_msg_data[i] == '\n')
00111         {
00112           msg_data[j++] = '\r';
00113           msg_data[j++] = '\n';
00114         }
00115       else
00116         msg_data[j++] = src_msg_data[i];
00117     }
00118 
00119   // Map the ACE log record type to an event log type.
00120   WORD event_type;
00121   switch (log_record.type ())
00122   {
00123   case LM_STARTUP:
00124   case LM_SHUTDOWN:
00125   case LM_TRACE:
00126   case LM_DEBUG:
00127   case LM_INFO:
00128     event_type = EVENTLOG_INFORMATION_TYPE;
00129     break;
00130   case LM_NOTICE:
00131   case LM_WARNING:
00132     event_type = EVENTLOG_WARNING_TYPE;
00133     break;
00134   case LM_ERROR:
00135   case LM_CRITICAL:
00136   case LM_ALERT:
00137   case LM_EMERGENCY:
00138   default:
00139     event_type = EVENTLOG_ERROR_TYPE;
00140     break;
00141   }
00142 
00143   // Send the log message to the system event log.
00144   const ACE_TCHAR* msgs [1];
00145   msgs[0] = msg_data;
00146 
00147   if (ACE_TEXT_ReportEvent (this->evlog_handle_,
00148                             event_type, 0, 0, 0, 1, 0, msgs, 0) == 0)
00149     return -1;
00150   else
00151     return 0;
00152 }
00153 
00154 #endif /* ACE_WIN32 && !ACE_HAS_WINCE && !ACE_HAS_PHARLAP */

Generated on Mon Jun 16 11:20:00 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002