diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 2001-03-04 16:46:52 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 2001-03-04 16:46:52 +0000 |
commit | f39d069d6036626d8e9579a1412636b3fb18efcd (patch) | |
tree | e4a4c9cd67a74dfa037bf8f69e38196e65444056 /ace | |
parent | 36a5b5cfb353f6339e92019df1dd5fbce5fbddd0 (diff) | |
download | ATCD-f39d069d6036626d8e9579a1412636b3fb18efcd.tar.gz |
ChangeLogTag:Sun Mar 4 10:41:57 2001 Douglas C. Schmidt <schmidt@tango.cs.wustl.edu>
Diffstat (limited to 'ace')
-rw-r--r-- | ace/Log_Msg.cpp | 95 | ||||
-rw-r--r-- | ace/Log_Msg.h | 4 | ||||
-rw-r--r-- | ace/Log_Msg_Backend.h | 11 | ||||
-rw-r--r-- | ace/Log_Msg_NT_Event_Log.cpp | 142 | ||||
-rw-r--r-- | ace/Log_Msg_NT_Event_Log.h | 61 | ||||
-rw-r--r-- | ace/Logging_Strategy.cpp | 5 | ||||
-rw-r--r-- | ace/Makefile.bor | 1 | ||||
-rw-r--r-- | ace/OS.cpp | 13 | ||||
-rw-r--r-- | ace/OS.h | 13 | ||||
-rw-r--r-- | ace/OS.i | 12 | ||||
-rw-r--r-- | ace/ace.rc | 8 | ||||
-rw-r--r-- | ace/ace_message_table.bin | bin | 0 -> 28 bytes |
12 files changed, 330 insertions, 35 deletions
diff --git a/ace/Log_Msg.cpp b/ace/Log_Msg.cpp index 4603c975f2b..8eec36f8fe9 100644 --- a/ace/Log_Msg.cpp +++ b/ace/Log_Msg.cpp @@ -24,6 +24,7 @@ #include "ace/Log_Msg.h" #include "ace/Log_Msg_Callback.h" #include "ace/Log_Msg_IPC.h" +#include "ace/Log_Msg_NT_Event_Log.h" ACE_RCSID(ace, Log_Msg, "$Id$") @@ -77,7 +78,11 @@ class ACE_Log_Msg_Manager // For internal use by ACE, only! { public: - static ACE_Log_Msg_Backend *ipc_backend_; + static ACE_Log_Msg_Backend *log_backend_; + + static u_long log_backend_flags_; + + static int init_backend (const u_long *flags = 0); #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) static void close (void); @@ -89,7 +94,52 @@ private: #endif /* ! ACE_MT_SAFE */ }; -ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::ipc_backend_ = 0; +ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::log_backend_ = 0; +u_long ACE_Log_Msg_Manager::log_backend_flags_ = 0; + +int ACE_Log_Msg_Manager::init_backend (const u_long *flags) +{ + // If flags have been supplied, and they are different from the flags + // we had last time, then we may have to re-create the backend as a + // different type. + if (flags) + { + if ((ACE_BIT_ENABLED (*flags, ACE_Log_Msg::SYSLOG) + && ACE_BIT_DISABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG)) + || (ACE_BIT_DISABLED (*flags, ACE_Log_Msg::SYSLOG) + && ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG))) + { + delete ACE_Log_Msg_Manager::log_backend_; + ACE_Log_Msg_Manager::log_backend_ = 0; + } + + ACE_Log_Msg_Manager::log_backend_flags_ = *flags; + } + + if (ACE_Log_Msg_Manager::log_backend_ == 0) + { + ACE_NO_HEAP_CHECK; + +#if defined (WIN32) + // Allocate the ACE_Log_Msg_Backend instance. + if (ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG)) + ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_, + ACE_Log_Msg_NT_Event_Log, + -1); + else + ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_, + ACE_Log_Msg_IPC, + -1); +#else /* WIN32 */ + // Allocate the ACE_Log_Msg IPC instance. + ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_, + ACE_Log_Msg_IPC, + -1); +#endif /* WIN32 */ + } + + return 0; +} #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) ACE_Recursive_Thread_Mutex *ACE_Log_Msg_Manager::lock_ = 0; @@ -110,16 +160,8 @@ ACE_Log_Msg_Manager::get_lock (void) 0); } - - if (ACE_Log_Msg_Manager::ipc_backend_ == 0) - { - ACE_NO_HEAP_CHECK; - - // Allocate the ACE_Log_Msg IPC instance. - ACE_NEW_RETURN (ACE_Log_Msg_Manager::ipc_backend_, - ACE_Log_Msg_IPC, - 0); - } + if (init_backend () == -1) + return 0; return ACE_Log_Msg_Manager::lock_; } @@ -140,8 +182,8 @@ ACE_Log_Msg_Manager::close (void) delete ACE_Log_Msg_Manager::lock_; ACE_Log_Msg_Manager::lock_ = 0; - delete ACE_Log_Msg_Manager::ipc_backend_; - ACE_Log_Msg_Manager::ipc_backend_ = 0; + delete ACE_Log_Msg_Manager::log_backend_; + ACE_Log_Msg_Manager::log_backend_ = 0; } # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \ @@ -270,10 +312,8 @@ ACE_Log_Msg::instance (void) // We don't have threads, we cannot call // ACE_Log_Msg_Manager::get_lock () to initialize the logger // callback, so instead we do it here. - if (ACE_Log_Msg_Manager::ipc_backend_ == 0) - ACE_NEW_RETURN (ACE_Log_Msg_Manager::ipc_backend_, - ACE_Log_Msg_IPC, - 0); + if (ACE_Log_Msg_Manager::init_backend () == -1) + return 0; // Singleton implementation. static ACE_Cleanup_Adapter<ACE_Log_Msg> *log_msg = 0; @@ -534,8 +574,8 @@ ACE_Log_Msg::~ACE_Log_Msg (void) if (instance_count == 0) { // Destroy the message queue instance. - if (ACE_Log_Msg_Manager::ipc_backend_ != 0) - ACE_Log_Msg_Manager::ipc_backend_->close (); + if (ACE_Log_Msg_Manager::log_backend_ != 0) + ACE_Log_Msg_Manager::log_backend_->close (); # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) # if defined (ACE_HAS_TSS_EMULATION) @@ -599,23 +639,24 @@ ACE_Log_Msg::open (const ACE_TCHAR *prog_name, int status = 0; // Be sure that there is a message_queue_, with multiple threads. - ACE_MT (ACE_Log_Msg_Manager::get_lock ()); + ACE_MT (ACE_Log_Msg_Manager::init_backend (&flags)); // Always close the current handle before doing anything else. - ACE_Log_Msg_Manager::ipc_backend_->reset (); + ACE_Log_Msg_Manager::log_backend_->reset (); // Note that if we fail to open the message queue the default action // is to use stderr (set via static initialization in the // Log_Msg.cpp file). - if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER)) + if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER) + || ACE_BIT_ENABLED (flags, ACE_Log_Msg::SYSLOG)) { if (logger_key == 0) status = -1; else { status = - ACE_Log_Msg_Manager::ipc_backend_->open (logger_key); + ACE_Log_Msg_Manager::log_backend_->open (logger_key); } if (status == -1) @@ -1417,10 +1458,10 @@ ACE_Log_Msg::log (ACE_Log_Record &log_record, ACE_Log_Msg::LOGGER)) { // Be sure that there is a message_queue_, with multiple threads. - ACE_MT (ACE_Log_Msg_Manager::get_lock ()); + ACE_MT (ACE_Log_Msg_Manager::init_backend ()); result = - ACE_Log_Msg_Manager::ipc_backend_->log (log_record); + ACE_Log_Msg_Manager::log_backend_->log (log_record); } // This must come last, after the other two print operations @@ -1572,7 +1613,7 @@ ACE_Log_Msg::dump (void) const ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nmsg_off_ = %d\n"), this->msg_off_)); // Be sure that there is a message_queue_, with multiple threads. - ACE_MT (ACE_Log_Msg_Manager::get_lock ()); + ACE_MT (ACE_Log_Msg_Manager::init_backend ()); ACE_MT (ACE_Log_Msg_Manager::get_lock ()->dump ()); // Synchronize output operations. diff --git a/ace/Log_Msg.h b/ace/Log_Msg.h index a8d5c945407..2e570ac9987 100644 --- a/ace/Log_Msg.h +++ b/ace/Log_Msg.h @@ -168,7 +168,9 @@ public: VERBOSE_LITE = 32, /// Do not print messages at all (just leave in thread-specific /// storage for later inspection). - SILENT = 64 + SILENT = 64, + /// Write messages to the system's event log. + SYSLOG = 128 }; // = Initialization and termination routines. diff --git a/ace/Log_Msg_Backend.h b/ace/Log_Msg_Backend.h index 73e90d8b8ff..bc8e867018a 100644 --- a/ace/Log_Msg_Backend.h +++ b/ace/Log_Msg_Backend.h @@ -43,16 +43,15 @@ public: /// Open a new connection virtual int open (const ACE_TCHAR *logger_key) = 0; - /// Reset the backend. /* - * When changing the logging destination the backend may need to - * properly disconnect from the remote logging daemon and reclaim - * some local resources. But we try to reduce the number of local - * allocations/deallocations. + * Reset the backend. When changing the logging destination the + * backend may need to properly disconnect from the remote logging + * daemon and reclaim some local resources. But we try to reduce + * the number of local allocations/deallocations. */ virtual int reset (void) = 0; - /// Close the Backend completely. + /// Close the backend completely. virtual int close (void) = 0; /// Backend routine. This is called when we want to log a message. diff --git a/ace/Log_Msg_NT_Event_Log.cpp b/ace/Log_Msg_NT_Event_Log.cpp new file mode 100644 index 00000000000..28760c255de --- /dev/null +++ b/ace/Log_Msg_NT_Event_Log.cpp @@ -0,0 +1,142 @@ +// $Id$ + +#include "ace/config-all.h" + +#if defined (ACE_WIN32) + +#include "ace/Log_Msg_NT_Event_Log.h" +#include "ace/Log_Record.h" + +ACE_RCSID(ace, Log_Msg_NT_Event_Log, "$Id$") + +ACE_Log_Msg_NT_Event_Log::ACE_Log_Msg_NT_Event_Log (void) + : evlog_handle_(0) +{ +} + +ACE_Log_Msg_NT_Event_Log::~ACE_Log_Msg_NT_Event_Log (void) +{ + (void) this->close (); +} + +int +ACE_Log_Msg_NT_Event_Log::open (const ACE_TCHAR *logger_key) +{ + // ACE's "resource module" contains the message resource required + // for event logging. + ACE_TCHAR msg_file [MAXPATHLEN]; + + if (!GetModuleFileName (ACE_OS::get_win32_resource_module (), + msg_file, + MAXPATHLEN)) + return -1; + int msg_file_length = ACE_OS::strlen (msg_file); + + // Information is stored in the registry at a location based on the + // logger_key. + ACE_TCHAR reg_key [MAXPATHLEN]; + ACE_OS::strcpy (reg_key, + "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"); + int reg_key_length = ACE_OS::strlen(reg_key); + ACE_OS::strncat (reg_key, + logger_key, + MAXPATHLEN - reg_key_length); + + // Add the event source to the registry. Note that if this fails it + // is not fatal. The application will still be able to write entries + // to the event log, they just won't be formatted correctly. + HKEY hkey; + RegCreateKey (HKEY_LOCAL_MACHINE, + reg_key, + &hkey); + DWORD flags = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; + RegSetValueEx (hkey, + "TypesSupported", + 0, + REG_DWORD, + (LPBYTE) &flags, + sizeof (DWORD)); + RegSetValueEx (hkey, + "EventMessageFile", + 0, + REG_SZ, + (LPBYTE) msg_file, + msg_file_length + 1); + RegCloseKey (hkey); + + // Obtain a handle to the event source. + this->evlog_handle_ = RegisterEventSource (0, logger_key); + return this->evlog_handle_ ? 0 : -1; +} + +int +ACE_Log_Msg_NT_Event_Log::reset (void) +{ + return this->close (); +} + +int +ACE_Log_Msg_NT_Event_Log::close (void) +{ + if (DeregisterEventSource (this->evlog_handle_)) + { + this->evlog_handle_ = 0; + return 0; + } + else + return -1; +} + +int +ACE_Log_Msg_NT_Event_Log::log (ACE_Log_Record &log_record) +{ + // Make a copy of the log text and replace any newlines with + // spaces. Newline characters do not appear correctly in the event + // viewer. + const ACE_TCHAR* src_msg_data = log_record.msg_data (); + ACE_TCHAR msg_data [ACE_Log_Record::MAXLOGMSGLEN]; + + for (long i = 0; i < log_record.length (); ++i) + { + if (src_msg_data[i] == '\n') + msg_data[i] = ' '; + else + msg_data[i] = src_msg_data[i]; + } + + // Map the ACE log record type to an event log type. + WORD event_type; + switch (log_record.type ()) + { + case LM_STARTUP: + case LM_SHUTDOWN: + case LM_TRACE: + case LM_DEBUG: + case LM_INFO: + event_type = EVENTLOG_INFORMATION_TYPE; + break; + case LM_NOTICE: + case LM_WARNING: + event_type = EVENTLOG_WARNING_TYPE; + break; + case LM_ERROR: + case LM_CRITICAL: + case LM_ALERT: + case LM_EMERGENCY: + default: + event_type = EVENTLOG_ERROR_TYPE; + break; + } + + // Send the log message to the system event log. + const ACE_TCHAR* msgs [1]; + msgs[0] = msg_data; + + if (ReportEvent (this->evlog_handle_, + event_type, 0, 0, 0, 1, 0, msgs, 0) == 0) + return -1; + else + return 0; +} + +#endif /* ACE_WIN32 */ diff --git a/ace/Log_Msg_NT_Event_Log.h b/ace/Log_Msg_NT_Event_Log.h new file mode 100644 index 00000000000..c0d0d3966f4 --- /dev/null +++ b/ace/Log_Msg_NT_Event_Log.h @@ -0,0 +1,61 @@ +// -*- C++ -*- + +//============================================================================= +/** + * @file Log_Msg_NT_Event_Log.h + * + * $Id$ + * + * @author Christopher Kohlhoff <chris@kohlhoff.com> + */ +//============================================================================= + +#ifndef ACE_LOG_MSG_NT_EVENT_LOG_H +#define ACE_LOG_MSG_NT_EVENT_LOG_H +#include "ace/pre.h" + +#include "ace/config-all.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#if defined (ACE_WIN32) + +#include "ace/Log_Msg_Backend.h" + +/** + * @class ACE_Log_Msg_NT_Event_Log + * + * @brief Implements an ACE_Log_Msg_Backend that logs to the WinNT system + * event log. + */ +class ACE_Export ACE_Log_Msg_NT_Event_Log : public ACE_Log_Msg_Backend +{ +public: + /// Constructor + ACE_Log_Msg_NT_Event_Log (void); + + /// Destructor + virtual ~ACE_Log_Msg_NT_Event_Log (void); + + /// Open a new event log. + virtual int open (const ACE_TCHAR *logger_key); + + /// Reset the backend. + virtual int reset (void); + + /// Close the backend completely. + virtual int close (void); + + /// This is called when we want to log a message. + virtual int log (ACE_Log_Record &log_record); + +private: + HANDLE evlog_handle_; +}; + +#endif /* ACE_WIN32 */ + +#include "ace/post.h" +#endif /* ACE_LOG_MSG_NT_EVENT_LOG_H */ diff --git a/ace/Logging_Strategy.cpp b/ace/Logging_Strategy.cpp index df334f74b1f..69a271b0f13 100644 --- a/ace/Logging_Strategy.cpp +++ b/ace/Logging_Strategy.cpp @@ -97,6 +97,8 @@ ACE_Logging_Strategy::tokenize (ACE_TCHAR *flag_string) ACE_SET_BITS (this->flags_, ACE_Log_Msg::VERBOSE_LITE); else if (ACE_OS::strcmp (flag, ACE_LIB_TEXT ("SILENT")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::SILENT); + else if (ACE_OS::strcmp (flag, ACE_LIB_TEXT ("SYSLOG")) == 0) + ACE_SET_BITS (this->flags_, ACE_Log_Msg::SYSLOG); } } @@ -250,7 +252,8 @@ ACE_Logging_Strategy::init (int argc, ACE_TCHAR *argv[]) | ACE_Log_Msg::OSTREAM | ACE_Log_Msg::VERBOSE | ACE_Log_Msg::VERBOSE_LITE - | ACE_Log_Msg::SILENT); + | ACE_Log_Msg::SILENT + | ACE_Log_Msg::SYSLOG); // Check if OSTREAM bit is set if (ACE_BIT_ENABLED (this->flags_, ACE_Log_Msg::OSTREAM)) diff --git a/ace/Makefile.bor b/ace/Makefile.bor index fff1588b626..e1ffea7f45b 100644 --- a/ace/Makefile.bor +++ b/ace/Makefile.bor @@ -78,6 +78,7 @@ OBJFILES = \ $(OBJDIR)\Log_Msg_Backend.obj \ $(OBJDIR)\Log_Msg_Callback.obj \ $(OBJDIR)\Log_Msg_IPC.obj \ + $(OBJDIR)\Log_Msg_NT_Event_Log.obj \ $(OBJDIR)\Log_Record.obj \ $(OBJDIR)\Logging_Strategy.obj \ $(OBJDIR)\LSOCK.obj \ diff --git a/ace/OS.cpp b/ace/OS.cpp index 5b70ab494d8..050674d499f 100644 --- a/ace/OS.cpp +++ b/ace/OS.cpp @@ -31,6 +31,19 @@ static const ACE_TCHAR *ACE_OS_CTIME_R_FMTSTR = ACE_LIB_TEXT ("%3s %3s %02d %02d # if defined (ACE_WIN32) OSVERSIONINFO ACE_OS::win32_versioninfo_; // Cached win32 version information. + +HINSTANCE ACE_OS::win32_resource_module_; + +# if defined (ACE_OS_HAS_DLL) && (ACE_OS_HAS_DLL == 1) +// This function is called by the OS when the ACE DLL is loaded. We +// use it to determine the default module containing ACE's resources. +BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID) +{ + if (reason == DLL_PROCESS_ATTACH) + ACE_OS::set_win32_resource_module(instance); + return TRUE; +} +# endif /* ACE_OS_HAS_DLL && ACE_OS_HAS_DLL == 1 */ # endif /* ACE_WIN32 */ class ACE_OS_Thread_Mutex_Guard @@ -5269,6 +5269,17 @@ public: /// Return the win32 OSVERSIONINFO structure. static const OSVERSIONINFO &get_win32_versioninfo (void); + // = A pair of functions for modifying ACE's Win32 resource usage. + /// Return the handle of the module containing ACE's resources. By + /// default, for a DLL build of ACE this is a handle to the ACE DLL + /// itself, and for a static build it is a handle to the executable. + static HINSTANCE get_win32_resource_module (void); + + /// Allow an application to modify which module contains ACE's + /// resources. This is mainly useful for a static build of ACE where + /// the required resources reside somewhere other than the executable. + static void set_win32_resource_module (HINSTANCE); + # endif /* ACE_WIN32 */ // = A set of wrappers for miscellaneous operations. @@ -6587,6 +6598,8 @@ private: static OSVERSIONINFO win32_versioninfo_; + static HINSTANCE win32_resource_module_; + # endif /* ACE_WIN32 */ }; @@ -12148,4 +12148,16 @@ ACE_OS::get_win32_versioninfo () { return ACE_OS::win32_versioninfo_; } + +ACE_INLINE HINSTANCE +ACE_OS::get_win32_resource_module () +{ + return ACE_OS::win32_resource_module_; +} + +ACE_INLINE void +ACE_OS::set_win32_resource_module (HINSTANCE instance) +{ + ACE_OS::win32_resource_module_ = instance; +} #endif /* ACE_WIN32 */ diff --git a/ace/ace.rc b/ace/ace.rc index ede20b12cdf..54792491900 100644 --- a/ace/ace.rc +++ b/ace/ace.rc @@ -28,3 +28,11 @@ BEGIN VALUE "Translation", 0x409, 1200 END END + +/* + * The following resource is used by the ACE logger to write messages + * to the NT event log. If you are statically linking to the ACE + * library, and you wish to use the NT event log, you should copy this + * message table to your application's resource script. + */ +1 MESSAGETABLE ace_message_table.bin diff --git a/ace/ace_message_table.bin b/ace/ace_message_table.bin Binary files differnew file mode 100644 index 00000000000..6ac08e5dc69 --- /dev/null +++ b/ace/ace_message_table.bin |