blob: 7238763ef41ae86232c6506893f85f735647b2c1 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// 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 slog.h
41 * @short System logging facilities abstraction.
42 **/
43
44#ifndef CCXX_SLOG_H_
45#define CCXX_SLOG_H_
46
47#ifndef CCXX_MISSING_H_
48#include <cc++/missing.h>
49#endif
50
51#ifndef CCXX_THREAD_H_
52#include <cc++/thread.h>
53#endif
54
55#ifndef HAVE_SYSLOG_H
56#include <cstdio>
57#endif
58
59#ifdef CCXX_NAMESPACES
60namespace ost {
61#endif
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#ifndef HAVE_SYSLOG_H
136 Mutex lock;
137 FILE *syslog;
138#endif
139 int priority;
140 Level _level;
141 bool _enable;
142 bool _clogEnable;
143
144 ThreadImpl *getPriv(void);
145
146protected:
147 /**
148 * This is the streambuf function that actually outputs the data
149 * to the device. Since all output should be done with the standard
150 * ostream operators, this function should never be called directly.
151 */
152 int overflow(int c);
153
154public:
155 /**
156 * Default (and only) constructor. The default log level is set to
157 * SLOG_DEBUG. There is no default log facility set. One should be
158 * set before attempting any output. This is done by the <code>open()</code> or the
159 * <code>operator()(const char*, Class, Level)</code>
160 * functions.
161 */
162 Slog(void);
163
164 virtual ~Slog(void);
165
166 void close(void);
167
168 /**
169 * (re)opens the output stream.
170 * @param ident The identifier portion of the message sent to the syslog daemon.
171 * @param grp The log facility the message is sent to
172 */
173 void open(const char *ident, Class grp = classUser);
174
175 /**
176 * Sets the log identifier, level, and class to use for subsequent output
177 * @param ident The identifier portion of the message
178 * @param grp The log facility the message is sent to
179 * @param level The log level of the message
180 */
181 Slog &operator()(const char *ident, Class grp = classUser,
182 Level level = levelError);
183
184 /**
185 * Changes the log level and class to use for subsequent output
186 * @param level The log level of the message
187 * @param grp The log facility the message is sent to
188 */
189 Slog &operator()(Level level, Class grp = classDefault);
190
191 /**
192 * Does nothing except return *this.
193 */
194 Slog &operator()(void);
195
196#ifdef HAVE_SNPRINTF
197 /**
198 * Print a formatted syslog string.
199 *
200 * @param format string.
201 */
202 void error(const char *format, ...);
203
204 /**
205 * Print a formatted syslog string.
206 *
207 * @param format string.
208 */
209 void warn(const char *format, ...);
210
211 /**
212 * Print a formatted syslog string.
213 *
214 * @param format string.
215 */
216 void debug(const char *format, ...);
217
218 /**
219 * Print a formatted syslog string.
220 *
221 * @param format string.
222 */
223 void emerg(const char *format, ...);
224
225 /**
226 * Print a formatted syslog string.
227 *
228 * @param format string.
229 */
230 void alert(const char *format, ...);
231
232 /**
233 * Print a formatted syslog string.
234 *
235 * @param format string.
236 */
237 void critical(const char *format, ...);
238
239 /**
240 * Print a formatted syslog string.
241 *
242 * @param format string.
243 */
244 void notice(const char *format, ...);
245
246 /**
247 * Print a formatted syslog string.
248 *
249 * @param format string.
250 */
251 void info(const char *format, ...);
252#endif
253
254 /**
255 * Sets the logging level.
256 * @param enable is the logging level to use for further output
257 */
258 inline void level(Level enable)
259 {_level = enable;};
260
261 /**
262 * Enables or disables the echoing of the messages to clog in addition
263 * to the syslog daemon. This is enabled by the default class constructor.
264 * @param f true to enable, false to disable clog output
265 */
266 inline void clogEnable(bool f=true)
267 {_clogEnable = f;};
268
269 inline Slog &warn(void)
270 {return operator()(Slog::levelWarning);};
271
272 inline Slog &error(void)
273 {return operator()(Slog::levelError);};
274
275 inline Slog &debug(void)
276 {return operator()(Slog::levelDebug);};
277
278 inline Slog &emerg(void)
279 {return operator()(Slog::levelEmergency);};
280
281 inline Slog &alert(void)
282 {return operator()(Slog::levelAlert);};
283
284 inline Slog &critical(void)
285 {return operator()(Slog::levelCritical);};
286
287 inline Slog &notice(void)
288 {return operator()(Slog::levelNotice);};
289
290 inline Slog &info(void)
291 {return operator()(Slog::levelInfo);};
292
293};
294
295//#ifdef CYGWIN_IMPORTS
296//extern __declspec(dllimport) Slog slog;
297//#else
298extern __EXPORT Slog slog;
299//#endif
300
301#ifdef CCXX_NAMESPACES
302}
303#endif
304
305#endif
306
307/** EMACS **
308 * Local variables:
309 * mode: c++
310 * c-basic-offset: 4
311 * End:
312 */