Log_Msg_UNIX_Syslog.cpp

Go to the documentation of this file.
00001 // Log_Msg_UNIX_Syslog.cpp,v 4.14 2005/10/28 16:14:53 ossama Exp
00002 
00003 #include "ace/config-all.h"
00004 
00005 #if !defined (ACE_LACKS_UNIX_SYSLOG)
00006 
00007 #include "ace/ACE.h"
00008 #include "ace/Log_Msg.h"
00009 #include "ace/Log_Msg_UNIX_Syslog.h"
00010 #include "ace/Log_Record.h"
00011 #include "ace/OS_NS_string.h"
00012 #include "ace/os_include/os_syslog.h"
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 // NOTE:
00017 //   The ACE_Log_Msg_UNIX_Syslog class can use the openlog(),
00018 //   setlogmask(), syslog() and closelog() routines in a thread safe
00019 //   manner (versus using openlog_r(), et. al.), as the ACE_Log_Msg
00020 //   class uses the lock provided by its local ACE_Log_Msg_Manager
00021 //   class when calling the methods of the backend classes.  As a
00022 //   result, logging semantics with respect to the UNIX syslog
00023 //   facility, will be the same for all threads in a process.  This
00024 //   should not be too limiting, as the ACE_Log_Msg class itself can
00025 //   be used to control thread specific logging behavior.
00026 
00027 ACE_Log_Msg_UNIX_Syslog::ACE_Log_Msg_UNIX_Syslog (void)
00028 {
00029 }
00030 
00031 ACE_Log_Msg_UNIX_Syslog::~ACE_Log_Msg_UNIX_Syslog (void)
00032 {
00033   (void) this->close ();
00034 }
00035 
00036 int
00037 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR * logger_key)
00038 {
00039   if (logger_key == 0)
00040     logger_key = ACE_Log_Msg::program_name ();
00041 
00042   // Initialize the UNIX syslog facility.  Default the syslog log
00043   // options LOG_CONS and LOG_PID to be set.  There really should be a
00044   // logging strategy option to control the syslog log options,
00045   // however, we'll take the easy way out for now.
00046 #if defined (ACE_USES_WCHAR)
00047   openlog (ACE_TEXT_ALWAYS_CHAR (logger_key),
00048            LOG_CONS|LOG_PID,
00049            ACE_DEFAULT_SYSLOG_FACILITY);
00050 #else
00051   openlog (const_cast<char *> (logger_key),
00052            LOG_CONS|LOG_PID,
00053            ACE_DEFAULT_SYSLOG_FACILITY);
00054 #endif /* ACE_USES_WCHAR */
00055 
00056   // Enable logging of all syslog priorities.  If logging of all
00057   // priorities is not desired, use the ACE_Log_Msg::priority_mask()
00058   // method to control the log output sent to the syslog daemon via
00059   // the log() method, or use the system's syslog.conf file to select
00060   // desired level of information.
00061 
00062 #if !defined (ACE_LACKS_SETLOGMASK)
00063   (void) setlogmask (LOG_UPTO (LOG_DEBUG));
00064 #endif /* ACE_LACKS_SETLOGMASK */
00065 
00066   return 0;
00067 }
00068 
00069 int
00070 ACE_Log_Msg_UNIX_Syslog::reset (void)
00071 {
00072   return this->close ();
00073 }
00074 
00075 int
00076 ACE_Log_Msg_UNIX_Syslog::close (void)
00077 {
00078   closelog();
00079   return 0;
00080 }
00081 
00082 int
00083 ACE_Log_Msg_UNIX_Syslog::log (ACE_Log_Record &log_record)
00084 {
00085   int syslog_priority = this->convert_log_priority (log_record.type ());
00086   u_long flags = ACE_LOG_MSG->flags ();
00087 
00088   // The UNIX syslog() facility does not support multi-line messages.
00089   // Break up the message data into separate lines and send each line
00090   // to the syslog daemon.
00091 
00092   ACE_TCHAR message[ACE_Log_Record::MAXVERBOSELOGMSGLEN];
00093   ACE_OS::strcpy (message, log_record.msg_data ());
00094   ACE_TCHAR *strtokp = 0;
00095 
00096   for (ACE_TCHAR *line = ACE_OS::strtok_r (message,
00097                                            ACE_LIB_TEXT ("\n"),
00098                                            &strtokp);
00099        line != 0;
00100        line = ACE_OS::strtok_r (0,
00101                                 ACE_LIB_TEXT ("\n"),
00102                                 &strtokp))
00103     {
00104       // Format the message line.  Note that the processing for
00105       // VERBOSE is the same as for VERBOSE_LITE, since syslog()
00106       // already provides us with the hostname and PID.  However, the
00107       // timestamp is duplicated (albeit a shortened version) to
00108       // provide a timestamp with greater precision than that provided
00109       // by syslog().
00110       if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::VERBOSE)
00111           || ACE_BIT_ENABLED (flags, ACE_Log_Msg::VERBOSE_LITE))
00112         {
00113           ACE_TCHAR date_and_time[35];
00114           if (0 == ACE::timestamp (date_and_time, sizeof (date_and_time), 1))
00115             ACE_OS::strcpy (date_and_time, ACE_LIB_TEXT ("<time error>"));
00116           const ACE_TCHAR *prio_name =
00117             ACE_Log_Record::priority_name(ACE_Log_Priority(log_record.type()));
00118           syslog (syslog_priority,
00119                   "%s: %s: %s",
00120                   ACE_TEXT_ALWAYS_CHAR (date_and_time),
00121                   ACE_TEXT_ALWAYS_CHAR (prio_name),
00122                   ACE_TEXT_ALWAYS_CHAR (line));
00123         }
00124       else // No formatting required.
00125         syslog (syslog_priority, "%s", ACE_TEXT_ALWAYS_CHAR (line));
00126     }
00127 
00128   return 0;
00129 }
00130 
00131 int
00132 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority)
00133 {
00134   int syslog_priority;
00135   switch (lm_priority)
00136   {
00137   case LM_TRACE:
00138   case LM_DEBUG:
00139     syslog_priority = LOG_DEBUG;
00140     break;
00141   case LM_STARTUP:
00142   case LM_SHUTDOWN:
00143   case LM_INFO:
00144     syslog_priority = LOG_INFO;
00145     break;
00146   case LM_NOTICE:
00147     syslog_priority = LOG_NOTICE;
00148     break;
00149   case LM_WARNING:
00150     syslog_priority = LOG_WARNING;
00151     break;
00152   case LM_CRITICAL:
00153     syslog_priority = LOG_CRIT;
00154     break;
00155   case LM_ALERT:
00156     syslog_priority = LOG_ALERT;
00157     break;
00158   case LM_EMERGENCY:
00159     syslog_priority = LOG_EMERG;
00160     break;
00161   case LM_ERROR:
00162   default:
00163     syslog_priority = LOG_ERR;
00164     break;
00165   }
00166 
00167   return syslog_priority;
00168 }
00169 
00170 int
00171 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask)
00172 {
00173   int syslog_mask = 0;
00174 
00175   if (ACE_BIT_ENABLED (lm_mask, LM_TRACE)
00176       || ACE_BIT_ENABLED (lm_mask, LM_DEBUG))
00177     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_DEBUG));
00178 
00179   if (ACE_BIT_ENABLED (lm_mask, LM_STARTUP)
00180       || ACE_BIT_ENABLED (lm_mask, LM_SHUTDOWN)
00181       || ACE_BIT_ENABLED (lm_mask, LM_INFO))
00182     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_INFO));
00183 
00184   if (ACE_BIT_ENABLED (lm_mask, LM_NOTICE))
00185     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_NOTICE));
00186 
00187   if (ACE_BIT_ENABLED (lm_mask, LM_ERROR))
00188     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_ERR));
00189 
00190   if (ACE_BIT_ENABLED (lm_mask, LM_WARNING))
00191     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_WARNING));
00192 
00193   if (ACE_BIT_ENABLED (lm_mask, LM_CRITICAL))
00194     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_CRIT));
00195 
00196   if (ACE_BIT_ENABLED (lm_mask, LM_ALERT))
00197     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_ALERT));
00198 
00199   if (ACE_BIT_ENABLED (lm_mask, LM_EMERGENCY))
00200     ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_EMERG));
00201 
00202   return syslog_mask;
00203 }
00204 
00205 ACE_END_VERSIONED_NAMESPACE_DECL
00206 
00207 #endif /* !ACE_LACKS_UNIX_SYSLOG */

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