00001
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
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00043
00044
00045
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
00055
00056
00057
00058
00059
00060
00061
00062 #if !defined (ACE_LACKS_SETLOGMASK)
00063 (void) setlogmask (LOG_UPTO (LOG_DEBUG));
00064 #endif
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
00089
00090
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
00105
00106
00107
00108
00109
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
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