Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Log_Msg_UNIX_Syslog.cpp
blobaaaec35abcc3e7d7e914aec14ee982963bfb779c
1 #include "ace/config-all.h"
3 #if !defined (ACE_LACKS_UNIX_SYSLOG)
5 #include "ace/ACE.h"
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
14 // NOTE:
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 ();
30 int
31 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR * logger_key)
33 if (logger_key == 0)
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);
43 #else
44 # if defined (ACE_USES_WCHAR)
45 openlog (ACE_TEXT_ALWAYS_CHAR (logger_key),
46 LOG_CONS|LOG_PID,
47 ACE_DEFAULT_SYSLOG_FACILITY);
48 # else
49 openlog (const_cast<char *> (logger_key),
50 LOG_CONS|LOG_PID,
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 */
65 return 0;
68 int
69 ACE_Log_Msg_UNIX_Syslog::reset ()
71 return this->close ();
74 int
75 ACE_Log_Msg_UNIX_Syslog::close ()
77 closelog();
78 return 0;
81 ssize_t
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,
96 ACE_TEXT ("\n"),
97 &strtokp);
98 line != 0;
99 line = ACE_OS::strtok_r (0,
100 ACE_TEXT ("\n"),
101 &strtokp))
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
108 // by syslog().
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,
118 "%s: %s: %s",
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));
127 return 0;
131 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority)
133 int syslog_priority;
134 switch (lm_priority)
136 case LM_TRACE:
137 case LM_DEBUG:
138 syslog_priority = LOG_DEBUG;
139 break;
140 case LM_STARTUP:
141 case LM_SHUTDOWN:
142 case LM_INFO:
143 syslog_priority = LOG_INFO;
144 break;
145 case LM_NOTICE:
146 syslog_priority = LOG_NOTICE;
147 break;
148 case LM_WARNING:
149 syslog_priority = LOG_WARNING;
150 break;
151 case LM_CRITICAL:
152 syslog_priority = LOG_CRIT;
153 break;
154 case LM_ALERT:
155 syslog_priority = LOG_ALERT;
156 break;
157 case LM_EMERGENCY:
158 syslog_priority = LOG_EMERG;
159 break;
160 case LM_ERROR:
161 default:
162 syslog_priority = LOG_ERR;
163 break;
166 return syslog_priority;
170 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask)
172 int syslog_mask = 0;
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));
201 return syslog_mask;
204 ACE_END_VERSIONED_NAMESPACE_DECL
206 #endif /* !ACE_LACKS_UNIX_SYSLOG */