1 #include "ace/config-all.h"
3 #if !defined (ACE_LACKS_UNIX_SYSLOG)
6 #include "ace/Log_Category.h"
7 #include "ace/Log_Msg_UNIX_Syslog.h"
8 #include "ace/Log_Record.h"
9 #include "ace/OS_NS_string.h"
10 #include "ace/os_include/os_syslog.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 // The ACE_Log_Msg_UNIX_Syslog class can use the openlog(),
16 // setlogmask(), syslog() and closelog() routines in a thread safe
17 // manner (versus using openlog_r(), et. al.), as the ACE_Log_Msg
18 // class uses the lock provided by its local ACE_Log_Msg_Manager
19 // class when calling the methods of the backend classes. As a
20 // result, logging semantics with respect to the UNIX syslog
21 // facility, will be the same for all threads in a process. This
22 // should not be too limiting, as the ACE_Log_Msg class itself can
23 // be used to control thread specific logging behavior.
25 ACE_Log_Msg_UNIX_Syslog::ACE_Log_Msg_UNIX_Syslog (void)
29 ACE_Log_Msg_UNIX_Syslog::~ACE_Log_Msg_UNIX_Syslog (void)
31 (void) this->close ();
35 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR
* logger_key
)
38 logger_key
= ACE_Log_Msg::program_name ();
40 // Initialize the UNIX syslog facility. Default the syslog log
41 // options LOG_CONS and LOG_PID to be set. There really should be a
42 // logging strategy option to control the syslog log options,
43 // however, we'll take the easy way out for now.
44 #if defined (ACE_LACKS_OPENLOG)
45 ACE_UNUSED_ARG (logger_key
);
46 ACE_NOTSUP_RETURN (-1);
48 # if defined (ACE_USES_WCHAR)
49 openlog (ACE_TEXT_ALWAYS_CHAR (logger_key
),
51 ACE_DEFAULT_SYSLOG_FACILITY
);
53 openlog (const_cast<char *> (logger_key
),
55 ACE_DEFAULT_SYSLOG_FACILITY
);
56 # endif /* ACE_USES_WCHAR */
57 #endif /* ACE_LACKS_OPENLOG */
59 // Enable logging of all syslog priorities. If logging of all
60 // priorities is not desired, use the ACE_Log_Msg::priority_mask()
61 // method to control the log output sent to the syslog daemon via
62 // the log() method, or use the system's syslog.conf file to select
63 // desired level of information.
65 #if !defined (ACE_LACKS_SETLOGMASK)
66 (void) setlogmask (LOG_UPTO (LOG_DEBUG
));
67 #endif /* ACE_LACKS_SETLOGMASK */
73 ACE_Log_Msg_UNIX_Syslog::reset (void)
75 return this->close ();
79 ACE_Log_Msg_UNIX_Syslog::close (void)
86 ACE_Log_Msg_UNIX_Syslog::log (ACE_Log_Record
&log_record
)
88 int syslog_priority
= this->convert_log_priority (log_record
.type ());
89 u_long flags
= ACE_LOG_MSG
->flags ();
91 // The UNIX syslog() facility does not support multi-line messages.
92 // Break up the message data into separate lines and send each line
93 // to the syslog daemon.
95 ACE_TCHAR message
[ACE_Log_Record::MAXVERBOSELOGMSGLEN
];
96 ACE_OS::strcpy (message
, log_record
.msg_data ());
97 ACE_TCHAR
*strtokp
= 0;
99 for (ACE_TCHAR
*line
= ACE_OS::strtok_r (message
,
103 line
= ACE_OS::strtok_r (0,
107 // Format the message line. Note that the processing for
108 // VERBOSE is the same as for VERBOSE_LITE, since syslog()
109 // already provides us with the hostname and PID. However, the
110 // timestamp is duplicated (albeit a shortened version) to
111 // provide a timestamp with greater precision than that provided
113 if (ACE_BIT_ENABLED (flags
, ACE_Log_Msg::VERBOSE
)
114 || ACE_BIT_ENABLED (flags
, ACE_Log_Msg::VERBOSE_LITE
))
116 ACE_TCHAR date_and_time
[27];
117 if (0 == ACE::timestamp (date_and_time
, sizeof (date_and_time
), 1))
118 ACE_OS::strcpy (date_and_time
, ACE_TEXT ("<time error>"));
119 const ACE_TCHAR
*prio_name
=
120 ACE_Log_Record::priority_name(ACE_Log_Priority(log_record
.type()));
121 syslog (syslog_priority
,
123 ACE_TEXT_ALWAYS_CHAR (date_and_time
),
124 ACE_TEXT_ALWAYS_CHAR (prio_name
),
125 ACE_TEXT_ALWAYS_CHAR (line
));
127 else // No formatting required.
128 syslog (syslog_priority
, "%s", ACE_TEXT_ALWAYS_CHAR (line
));
135 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority
)
142 syslog_priority
= LOG_DEBUG
;
147 syslog_priority
= LOG_INFO
;
150 syslog_priority
= LOG_NOTICE
;
153 syslog_priority
= LOG_WARNING
;
156 syslog_priority
= LOG_CRIT
;
159 syslog_priority
= LOG_ALERT
;
162 syslog_priority
= LOG_EMERG
;
166 syslog_priority
= LOG_ERR
;
170 return syslog_priority
;
174 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask
)
178 if (ACE_BIT_ENABLED (lm_mask
, LM_TRACE
)
179 || ACE_BIT_ENABLED (lm_mask
, LM_DEBUG
))
180 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_DEBUG
));
182 if (ACE_BIT_ENABLED (lm_mask
, LM_STARTUP
)
183 || ACE_BIT_ENABLED (lm_mask
, LM_SHUTDOWN
)
184 || ACE_BIT_ENABLED (lm_mask
, LM_INFO
))
185 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_INFO
));
187 if (ACE_BIT_ENABLED (lm_mask
, LM_NOTICE
))
188 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_NOTICE
));
190 if (ACE_BIT_ENABLED (lm_mask
, LM_ERROR
))
191 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_ERR
));
193 if (ACE_BIT_ENABLED (lm_mask
, LM_WARNING
))
194 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_WARNING
));
196 if (ACE_BIT_ENABLED (lm_mask
, LM_CRITICAL
))
197 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_CRIT
));
199 if (ACE_BIT_ENABLED (lm_mask
, LM_ALERT
))
200 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_ALERT
));
202 if (ACE_BIT_ENABLED (lm_mask
, LM_EMERGENCY
))
203 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_EMERG
));
208 ACE_END_VERSIONED_NAMESPACE_DECL
210 #endif /* !ACE_LACKS_UNIX_SYSLOG */