blob: 233a3b43a8493d081cf01734f5bf416f60615933 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39/**
40 * @file commoncpp/slog.h
41 * @short System logging facilities abstraction.
42 **/
43
44#ifndef COMMONCPP_SLOG_H_
45#define COMMONCPP_SLOG_H_
46
47#include <cstdio>
48
49#ifndef COMMONCPP_CONFIG_H_
50#include <commoncpp/config.h>
51#endif
52
53#ifndef COMMONCPP_STRING_H_
54#include <commoncpp/string.h>
55#endif
56
57#ifndef COMMONCPP_THREAD_H_
58#include <commoncpp/thread.h>
59#endif
60
61NAMESPACE_COMMONCPP
62
63/**
64 * The slog class is used to stream messages to the system's logging facility (syslogd).
65 * A default <code>slog</code> object is used to avoid confusion with the native syslog
66 * facility and to imply a logical relationship to the C++ <code>clog()</code>.
67 *
68 * The key difference is that the <code>slog</code> object sends it's output to the
69 * system logging daemon (typically syslogd) rather than through stderr.
70 * <code>slog</code> can be streamed with the <code><<</code> operator just
71 * like <code>clog</code>; a default slog object is pre-initialized, and you stream
72 * character data to it.
73 *
74 * The <code>slog</code> allows one to specify logging levels and other properties through the <code>()</code> operators.
75 * Hence, once can do:
76 *
77 * <code><pre>
78 * slog("mydaemon", SLOG_DAEMON, SLOG_EMERGENCY) << I just died << endl; </pre></code>
79 *
80 * or things like:
81 *
82 * <code><pre>
83 * slog("mydaemon", SLOG_DAEMON);
84 * slog(SLOG_INFO) << "daemon initalized" << endl; </pre></code>
85 *
86 * The intent is to be as common-place and as convenient to use as the stderr based clog facility
87 * found in C++, and this is especially useful for C++ daemons.
88 *
89 * The <code>std::flush</code> manipulator doesn't work. Either the
90 * <code>std::endl</code> or <code>std::ends</code> manipulators
91 * must be used to cause the output to be sent to the daemon.
92 *
93 * When this class is used on a system that doesn't have the syslog headers
94 * (i.e. a non-posix win32 box), the output goes to the a file with the same name
95 * as the syslog identifier string with '.log' appended to it. If the identifier string ends in
96 * '.exe', the '.exe' is removed before the '.log' is appened. (e.g. the identifier foo.exe will
97 * generate a log file named foo.log)
98 *
99 * @author David Sugar <dyfet@ostel.com>
100 * <br>Minor docs & hacks by Jon Little <littlej@arlut.utexas.edu>
101 *
102 * @short system logging facility class.
103 */
104class __EXPORT Slog : protected std::streambuf, public std::ostream
105{
106public:
107 typedef enum Class {
108 classSecurity,
109 classAudit,
110 classDaemon,
111 classUser,
112 classDefault,
113 classLocal0,
114 classLocal1,
115 classLocal2,
116 classLocal3,
117 classLocal4,
118 classLocal5,
119 classLocal6,
120 classLocal7
121 } Class;
122
123 typedef enum Level {
124 levelEmergency = 1,
125 levelAlert,
126 levelCritical,
127 levelError,
128 levelWarning,
129 levelNotice,
130 levelInfo,
131 levelDebug
132 } Level;
133
134private:
135 pthread_mutex_t lock;
136 FILE *syslog;
137 int priority;
138 Level _level;
139 bool _enable;
140 bool _clogEnable;
141
142protected:
143 /**
144 * This is the streambuf function that actually outputs the data
145 * to the device. Since all output should be done with the standard
146 * ostream operators, this function should never be called directly.
147 */
148 int overflow(int c);
149
150public:
151 /**
152 * Default (and only) constructor. The default log level is set to
153 * SLOG_DEBUG. There is no default log facility set. One should be
154 * set before attempting any output. This is done by the <code>open()</code> or the
155 * <code>operator()(const char*, Class, Level)</code>
156 * functions.
157 */
158 Slog(void);
159
160 virtual ~Slog(void);
161
162 void close(void);
163
164 /**
165 * (re)opens the output stream.
166 * @param ident The identifier portion of the message sent to the syslog daemon.
167 * @param grp The log facility the message is sent to
168 */
169 void open(const char *ident, Class grp = classUser);
170
171 /**
172 * Sets the log identifier, level, and class to use for subsequent output
173 * @param ident The identifier portion of the message
174 * @param grp The log facility the message is sent to
175 * @param level The log level of the message
176 */
177 Slog &operator()(const char *ident, Class grp = classUser,
178 Level level = levelError);
179
180 /**
181 * Changes the log level and class to use for subsequent output
182 * @param level The log level of the message
183 * @param grp The log facility the message is sent to
184 */
185 Slog &operator()(Level level, Class grp = classDefault);
186
187 /**
188 * Does nothing except return *this.
189 */
190 Slog &operator()(void);
191
192 /**
193 * Print a formatted syslog string.
194 *
195 * @param format string.
196 */
197 void error(const char *format, ...);
198
199 /**
200 * Print a formatted syslog string.
201 *
202 * @param format string.
203 */
204 void warn(const char *format, ...);
205
206 /**
207 * Print a formatted syslog string.
208 *
209 * @param format string.
210 */
211 void debug(const char *format, ...);
212
213 /**
214 * Print a formatted syslog string.
215 *
216 * @param format string.
217 */
218 void emerg(const char *format, ...);
219
220 /**
221 * Print a formatted syslog string.
222 *
223 * @param format string.
224 */
225 void alert(const char *format, ...);
226
227 /**
228 * Print a formatted syslog string.
229 *
230 * @param format string.
231 */
232 void critical(const char *format, ...);
233
234 /**
235 * Print a formatted syslog string.
236 *
237 * @param format string.
238 */
239 void notice(const char *format, ...);
240
241 /**
242 * Print a formatted syslog string.
243 *
244 * @param format string.
245 */
246 void info(const char *format, ...);
247
248 /**
249 * Sets the logging level.
250 * @param enable is the logging level to use for further output
251 */
252 inline void level(Level enable)
253 {_level = enable;};
254
255 /**
256 * Enables or disables the echoing of the messages to clog in addition
257 * to the syslog daemon. This is enabled by the default class constructor.
258 * @param f true to enable, false to disable clog output
259 */
260 inline void clogEnable(bool f=true)
261 {_clogEnable = f;};
262
263 inline Slog &warn(void)
264 {return operator()(Slog::levelWarning);};
265
266 inline Slog &error(void)
267 {return operator()(Slog::levelError);};
268
269 inline Slog &debug(void)
270 {return operator()(Slog::levelDebug);};
271
272 inline Slog &emerg(void)
273 {return operator()(Slog::levelEmergency);};
274
275 inline Slog &alert(void)
276 {return operator()(Slog::levelAlert);};
277
278 inline Slog &critical(void)
279 {return operator()(Slog::levelCritical);};
280
281 inline Slog &notice(void)
282 {return operator()(Slog::levelNotice);};
283
284 inline Slog &info(void)
285 {return operator()(Slog::levelInfo);};
286
287};
288
289extern __EXPORT Slog slog;
290
291END_NAMESPACE
292
293#endif
294