Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / Log_Msg_UNIX_Syslog.cpp
blob50e787546538bf4c71be8822a560cb13947ea5fb
1 // $Id: Log_Msg_UNIX_Syslog.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/config-all.h"
5 #if !defined (ACE_LACKS_UNIX_SYSLOG)
7 #include "ace/ACE.h"
8 #include "ace/Log_Msg.h"
9 #include "ace/Log_Msg_UNIX_Syslog.h"
10 #include "ace/Log_Record.h"
11 #include "ace/OS_NS_string.h"
12 #include "ace/os_include/os_syslog.h"
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 // NOTE:
17 // The ACE_Log_Msg_UNIX_Syslog class can use the openlog(),
18 // setlogmask(), syslog() and closelog() routines in a thread safe
19 // manner (versus using openlog_r(), et. al.), as the ACE_Log_Msg
20 // class uses the lock provided by its local ACE_Log_Msg_Manager
21 // class when calling the methods of the backend classes. As a
22 // result, logging semantics with respect to the UNIX syslog
23 // facility, will be the same for all threads in a process. This
24 // should not be too limiting, as the ACE_Log_Msg class itself can
25 // be used to control thread specific logging behavior.
27 ACE_Log_Msg_UNIX_Syslog::ACE_Log_Msg_UNIX_Syslog (void)
31 ACE_Log_Msg_UNIX_Syslog::~ACE_Log_Msg_UNIX_Syslog (void)
33 (void) this->close ();
36 int
37 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR * logger_key)
39 if (logger_key == 0)
40 logger_key = ACE_Log_Msg::program_name ();
42 // Initialize the UNIX syslog facility. Default the syslog log
43 // options LOG_CONS and LOG_PID to be set. There really should be a
44 // logging strategy option to control the syslog log options,
45 // however, we'll take the easy way out for now.
46 #if defined (ACE_USES_WCHAR)
47 openlog (ACE_TEXT_ALWAYS_CHAR (logger_key),
48 LOG_CONS|LOG_PID,
49 ACE_DEFAULT_SYSLOG_FACILITY);
50 #else
51 openlog (const_cast<char *> (logger_key),
52 LOG_CONS|LOG_PID,
53 ACE_DEFAULT_SYSLOG_FACILITY);
54 #endif /* ACE_USES_WCHAR */
56 // Enable logging of all syslog priorities. If logging of all
57 // priorities is not desired, use the ACE_Log_Msg::priority_mask()
58 // method to control the log output sent to the syslog daemon via
59 // the log() method, or use the system's syslog.conf file to select
60 // desired level of information.
62 #if !defined (ACE_LACKS_SETLOGMASK)
63 (void) setlogmask (LOG_UPTO (LOG_DEBUG));
64 #endif /* ACE_LACKS_SETLOGMASK */
66 return 0;
69 int
70 ACE_Log_Msg_UNIX_Syslog::reset (void)
72 return this->close ();
75 int
76 ACE_Log_Msg_UNIX_Syslog::close (void)
78 closelog();
79 return 0;
82 ssize_t
83 ACE_Log_Msg_UNIX_Syslog::log (ACE_Log_Record &log_record)
85 int syslog_priority = this->convert_log_priority (log_record.type ());
86 u_long flags = ACE_LOG_MSG->flags ();
88 // The UNIX syslog() facility does not support multi-line messages.
89 // Break up the message data into separate lines and send each line
90 // to the syslog daemon.
92 ACE_TCHAR message[ACE_Log_Record::MAXVERBOSELOGMSGLEN];
93 ACE_OS::strcpy (message, log_record.msg_data ());
94 ACE_TCHAR *strtokp = 0;
96 for (ACE_TCHAR *line = ACE_OS::strtok_r (message,
97 ACE_TEXT ("\n"),
98 &strtokp);
99 line != 0;
100 line = ACE_OS::strtok_r (0,
101 ACE_TEXT ("\n"),
102 &strtokp))
104 // Format the message line. Note that the processing for
105 // VERBOSE is the same as for VERBOSE_LITE, since syslog()
106 // already provides us with the hostname and PID. However, the
107 // timestamp is duplicated (albeit a shortened version) to
108 // provide a timestamp with greater precision than that provided
109 // by syslog().
110 if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::VERBOSE)
111 || ACE_BIT_ENABLED (flags, ACE_Log_Msg::VERBOSE_LITE))
113 ACE_TCHAR date_and_time[35];
114 if (0 == ACE::timestamp (date_and_time, sizeof (date_and_time), 1))
115 ACE_OS::strcpy (date_and_time, ACE_TEXT ("<time error>"));
116 const ACE_TCHAR *prio_name =
117 ACE_Log_Record::priority_name(ACE_Log_Priority(log_record.type()));
118 syslog (syslog_priority,
119 "%s: %s: %s",
120 ACE_TEXT_ALWAYS_CHAR (date_and_time),
121 ACE_TEXT_ALWAYS_CHAR (prio_name),
122 ACE_TEXT_ALWAYS_CHAR (line));
124 else // No formatting required.
125 syslog (syslog_priority, "%s", ACE_TEXT_ALWAYS_CHAR (line));
128 return 0;
132 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority)
134 int syslog_priority;
135 switch (lm_priority)
137 case LM_TRACE:
138 case LM_DEBUG:
139 syslog_priority = LOG_DEBUG;
140 break;
141 case LM_STARTUP:
142 case LM_SHUTDOWN:
143 case LM_INFO:
144 syslog_priority = LOG_INFO;
145 break;
146 case LM_NOTICE:
147 syslog_priority = LOG_NOTICE;
148 break;
149 case LM_WARNING:
150 syslog_priority = LOG_WARNING;
151 break;
152 case LM_CRITICAL:
153 syslog_priority = LOG_CRIT;
154 break;
155 case LM_ALERT:
156 syslog_priority = LOG_ALERT;
157 break;
158 case LM_EMERGENCY:
159 syslog_priority = LOG_EMERG;
160 break;
161 case LM_ERROR:
162 default:
163 syslog_priority = LOG_ERR;
164 break;
167 return syslog_priority;
171 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask)
173 int syslog_mask = 0;
175 if (ACE_BIT_ENABLED (lm_mask, LM_TRACE)
176 || ACE_BIT_ENABLED (lm_mask, LM_DEBUG))
177 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_DEBUG));
179 if (ACE_BIT_ENABLED (lm_mask, LM_STARTUP)
180 || ACE_BIT_ENABLED (lm_mask, LM_SHUTDOWN)
181 || ACE_BIT_ENABLED (lm_mask, LM_INFO))
182 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_INFO));
184 if (ACE_BIT_ENABLED (lm_mask, LM_NOTICE))
185 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_NOTICE));
187 if (ACE_BIT_ENABLED (lm_mask, LM_ERROR))
188 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_ERR));
190 if (ACE_BIT_ENABLED (lm_mask, LM_WARNING))
191 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_WARNING));
193 if (ACE_BIT_ENABLED (lm_mask, LM_CRITICAL))
194 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_CRIT));
196 if (ACE_BIT_ENABLED (lm_mask, LM_ALERT))
197 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_ALERT));
199 if (ACE_BIT_ENABLED (lm_mask, LM_EMERGENCY))
200 ACE_SET_BITS (syslog_mask, LOG_MASK(LOG_EMERG));
202 return syslog_mask;
205 ACE_END_VERSIONED_NAMESPACE_DECL
207 #endif /* !ACE_LACKS_UNIX_SYSLOG */