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 ()
27 (void) this->close ();
31 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR
* logger_key
)
34 logger_key
= ACE_Log_Msg::program_name ();
36 // Initialize the UNIX syslog facility. Default the syslog log
37 // options LOG_CONS and LOG_PID to be set. There really should be a
38 // logging strategy option to control the syslog log options,
39 // however, we'll take the easy way out for now.
40 #if defined (ACE_LACKS_OPENLOG)
41 ACE_UNUSED_ARG (logger_key
);
42 ACE_NOTSUP_RETURN (-1);
44 # if defined (ACE_USES_WCHAR)
45 openlog (ACE_TEXT_ALWAYS_CHAR (logger_key
),
47 ACE_DEFAULT_SYSLOG_FACILITY
);
49 openlog (const_cast<char *> (logger_key
),
51 ACE_DEFAULT_SYSLOG_FACILITY
);
52 # endif /* ACE_USES_WCHAR */
53 #endif /* ACE_LACKS_OPENLOG */
55 // Enable logging of all syslog priorities. If logging of all
56 // priorities is not desired, use the ACE_Log_Msg::priority_mask()
57 // method to control the log output sent to the syslog daemon via
58 // the log() method, or use the system's syslog.conf file to select
59 // desired level of information.
61 #if !defined (ACE_LACKS_SETLOGMASK)
62 (void) setlogmask (LOG_UPTO (LOG_DEBUG
));
63 #endif /* ACE_LACKS_SETLOGMASK */
69 ACE_Log_Msg_UNIX_Syslog::reset ()
71 return this->close ();
75 ACE_Log_Msg_UNIX_Syslog::close ()
82 ACE_Log_Msg_UNIX_Syslog::log (ACE_Log_Record
&log_record
)
84 int syslog_priority
= this->convert_log_priority (log_record
.type ());
85 u_long flags
= ACE_LOG_MSG
->flags ();
87 // The UNIX syslog() facility does not support multi-line messages.
88 // Break up the message data into separate lines and send each line
89 // to the syslog daemon.
91 ACE_TCHAR message
[ACE_Log_Record::MAXVERBOSELOGMSGLEN
];
92 ACE_OS::strcpy (message
, log_record
.msg_data ());
93 ACE_TCHAR
*strtokp
= 0;
95 for (ACE_TCHAR
*line
= ACE_OS::strtok_r (message
,
99 line
= ACE_OS::strtok_r (0,
103 // Format the message line. Note that the processing for
104 // VERBOSE is the same as for VERBOSE_LITE, since syslog()
105 // already provides us with the hostname and PID. However, the
106 // timestamp is duplicated (albeit a shortened version) to
107 // provide a timestamp with greater precision than that provided
109 if (ACE_BIT_ENABLED (flags
, ACE_Log_Msg::VERBOSE
)
110 || ACE_BIT_ENABLED (flags
, ACE_Log_Msg::VERBOSE_LITE
))
112 ACE_TCHAR date_and_time
[27];
113 if (0 == ACE::timestamp (date_and_time
, sizeof (date_and_time
), 1))
114 ACE_OS::strcpy (date_and_time
, ACE_TEXT ("<time error>"));
115 const ACE_TCHAR
*prio_name
=
116 ACE_Log_Record::priority_name(ACE_Log_Priority(log_record
.type()));
117 syslog (syslog_priority
,
119 ACE_TEXT_ALWAYS_CHAR (date_and_time
),
120 ACE_TEXT_ALWAYS_CHAR (prio_name
),
121 ACE_TEXT_ALWAYS_CHAR (line
));
123 else // No formatting required.
124 syslog (syslog_priority
, "%s", ACE_TEXT_ALWAYS_CHAR (line
));
131 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority
)
138 syslog_priority
= LOG_DEBUG
;
143 syslog_priority
= LOG_INFO
;
146 syslog_priority
= LOG_NOTICE
;
149 syslog_priority
= LOG_WARNING
;
152 syslog_priority
= LOG_CRIT
;
155 syslog_priority
= LOG_ALERT
;
158 syslog_priority
= LOG_EMERG
;
162 syslog_priority
= LOG_ERR
;
166 return syslog_priority
;
170 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask
)
174 if (ACE_BIT_ENABLED (lm_mask
, LM_TRACE
)
175 || ACE_BIT_ENABLED (lm_mask
, LM_DEBUG
))
176 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_DEBUG
));
178 if (ACE_BIT_ENABLED (lm_mask
, LM_STARTUP
)
179 || ACE_BIT_ENABLED (lm_mask
, LM_SHUTDOWN
)
180 || ACE_BIT_ENABLED (lm_mask
, LM_INFO
))
181 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_INFO
));
183 if (ACE_BIT_ENABLED (lm_mask
, LM_NOTICE
))
184 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_NOTICE
));
186 if (ACE_BIT_ENABLED (lm_mask
, LM_ERROR
))
187 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_ERR
));
189 if (ACE_BIT_ENABLED (lm_mask
, LM_WARNING
))
190 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_WARNING
));
192 if (ACE_BIT_ENABLED (lm_mask
, LM_CRITICAL
))
193 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_CRIT
));
195 if (ACE_BIT_ENABLED (lm_mask
, LM_ALERT
))
196 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_ALERT
));
198 if (ACE_BIT_ENABLED (lm_mask
, LM_EMERGENCY
))
199 ACE_SET_BITS (syslog_mask
, LOG_MASK(LOG_EMERG
));
204 ACE_END_VERSIONED_NAMESPACE_DECL
206 #endif /* !ACE_LACKS_UNIX_SYSLOG */