GitHub Actions: Try MSVC builds with /std:c++17 and 20
[ACE_TAO.git] / ACE / ace / Log_Msg_UNIX_Syslog.cpp
blob0c14e0fbb3a1aeaa54fd2abe48c778198ca59886
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 (void)
29 ACE_Log_Msg_UNIX_Syslog::~ACE_Log_Msg_UNIX_Syslog (void)
31 (void) this->close ();
34 int
35 ACE_Log_Msg_UNIX_Syslog::open (const ACE_TCHAR * logger_key)
37 if (logger_key == 0)
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);
47 #else
48 # if defined (ACE_USES_WCHAR)
49 openlog (ACE_TEXT_ALWAYS_CHAR (logger_key),
50 LOG_CONS|LOG_PID,
51 ACE_DEFAULT_SYSLOG_FACILITY);
52 # else
53 openlog (const_cast<char *> (logger_key),
54 LOG_CONS|LOG_PID,
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 */
69 return 0;
72 int
73 ACE_Log_Msg_UNIX_Syslog::reset (void)
75 return this->close ();
78 int
79 ACE_Log_Msg_UNIX_Syslog::close (void)
81 closelog();
82 return 0;
85 ssize_t
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,
100 ACE_TEXT ("\n"),
101 &strtokp);
102 line != 0;
103 line = ACE_OS::strtok_r (0,
104 ACE_TEXT ("\n"),
105 &strtokp))
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
112 // by syslog().
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,
122 "%s: %s: %s",
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));
131 return 0;
135 ACE_Log_Msg_UNIX_Syslog::convert_log_priority (ACE_UINT32 lm_priority)
137 int syslog_priority;
138 switch (lm_priority)
140 case LM_TRACE:
141 case LM_DEBUG:
142 syslog_priority = LOG_DEBUG;
143 break;
144 case LM_STARTUP:
145 case LM_SHUTDOWN:
146 case LM_INFO:
147 syslog_priority = LOG_INFO;
148 break;
149 case LM_NOTICE:
150 syslog_priority = LOG_NOTICE;
151 break;
152 case LM_WARNING:
153 syslog_priority = LOG_WARNING;
154 break;
155 case LM_CRITICAL:
156 syslog_priority = LOG_CRIT;
157 break;
158 case LM_ALERT:
159 syslog_priority = LOG_ALERT;
160 break;
161 case LM_EMERGENCY:
162 syslog_priority = LOG_EMERG;
163 break;
164 case LM_ERROR:
165 default:
166 syslog_priority = LOG_ERR;
167 break;
170 return syslog_priority;
174 ACE_Log_Msg_UNIX_Syslog::convert_log_mask (int lm_mask)
176 int syslog_mask = 0;
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));
205 return syslog_mask;
208 ACE_END_VERSIONED_NAMESPACE_DECL
210 #endif /* !ACE_LACKS_UNIX_SYSLOG */