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

Log_Msg_UNIX_Syslog.cpp

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

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