ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
slog.h
Go to the documentation of this file.
1// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015 Cherokees of Idaho.
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
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 ost {
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 {
120 classLocal7
121 } Class;
122
123 typedef enum Level {
124 levelEmergency = 1,
131 levelDebug
132 } Level;
133
134private:
135 mutable pthread_mutex_t lock;
136 FILE *syslog;
141
143
144protected:
145 /**
146 * This is the streambuf function that actually outputs the data
147 * to the device. Since all output should be done with the standard
148 * ostream operators, this function should never be called directly.
149 */
150 int overflow(int c) __OVERRIDE;
151
152public:
153 /**
154 * Default (and only) constructor. The default log level is set to
155 * SLOG_DEBUG. There is no default log facility set. One should be
156 * set before attempting any output. This is done by the <code>open()</code> or the
157 * <code>operator()(const char*, Class, Level)</code>
158 * functions.
159 */
160 Slog(void);
161
162 virtual ~Slog(void);
163
164 void close(void);
165
166 /**
167 * (re)opens the output stream.
168 * @param ident The identifier portion of the message sent to the syslog daemon.
169 * @param grp The log facility the message is sent to
170 */
171 void open(const char *ident, Class grp = classUser);
172
173 /**
174 * Sets the log identifier, level, and class to use for subsequent output
175 * @param ident The identifier portion of the message
176 * @param grp The log facility the message is sent to
177 * @param level The log level of the message
178 */
179 Slog &operator()(const char *ident, Class grp = classUser,
180 Level level = levelError);
181
182 /**
183 * Changes the log level and class to use for subsequent output
184 * @param level The log level of the message
185 * @param grp The log facility the message is sent to
186 */
187 Slog &operator()(Level level, Class grp = classDefault);
188
189 /**
190 * Does nothing except return *this.
191 */
192 Slog &operator()(void);
193
194 /**
195 * Print a formatted syslog string.
196 *
197 * @param format string.
198 */
199 void error(const char *format, ...);
200
201 /**
202 * Print a formatted syslog string.
203 *
204 * @param format string.
205 */
206 void warn(const char *format, ...);
207
208 /**
209 * Print a formatted syslog string.
210 *
211 * @param format string.
212 */
213 void debug(const char *format, ...);
214
215 /**
216 * Print a formatted syslog string.
217 *
218 * @param format string.
219 */
220 void emerg(const char *format, ...);
221
222 /**
223 * Print a formatted syslog string.
224 *
225 * @param format string.
226 */
227 void alert(const char *format, ...);
228
229 /**
230 * Print a formatted syslog string.
231 *
232 * @param format string.
233 */
234 void critical(const char *format, ...);
235
236 /**
237 * Print a formatted syslog string.
238 *
239 * @param format string.
240 */
241 void notice(const char *format, ...);
242
243 /**
244 * Print a formatted syslog string.
245 *
246 * @param format string.
247 */
248 void info(const char *format, ...);
249
250 /**
251 * Sets the logging level.
252 * @param enable is the logging level to use for further output
253 */
254 inline void level(Level enable) {
255 _level = enable;
256 }
257
258 /**
259 * Enables or disables the echoing of the messages to clog in addition
260 * to the syslog daemon. This is enabled by the default class constructor.
261 * @param f true to enable, false to disable clog output
262 */
263 inline void clogEnable(bool f=true) {
264 _clogEnable = f;
265 }
266
267 inline Slog &warn(void) {
268 return operator()(Slog::levelWarning);
269 }
270
271 inline Slog &error(void) {
272 return operator()(Slog::levelError);
273 }
274
275 inline Slog &debug(void) {
276 return operator()(Slog::levelDebug);
277 }
278
279 inline Slog &emerg(void) {
280 return operator()(Slog::levelEmergency);
281 }
282
283 inline Slog &alert(void) {
284 return operator()(Slog::levelAlert);
285 }
286
287 inline Slog &critical(void) {
288 return operator()(Slog::levelCritical);
289 }
290
291 inline Slog &notice(void) {
292 return operator()(Slog::levelNotice);
293 }
294
295 inline Slog &info(void) {
296 return operator()(Slog::levelInfo);
297 }
298
299};
300
301extern __EXPORT Slog slog;
302
303} // namespace ost
304
305#endif
306
The slog class is used to stream messages to the system's logging facility (syslogd).
Definition: slog.h:105
Slog & critical(void)
Definition: slog.h:287
Slog & emerg(void)
Definition: slog.h:279
Slog & debug(void)
Definition: slog.h:275
int priority
Definition: slog.h:137
Slog & error(void)
Definition: slog.h:271
@ levelAlert
Definition: slog.h:125
@ levelInfo
Definition: slog.h:130
@ levelWarning
Definition: slog.h:128
@ levelCritical
Definition: slog.h:126
@ levelNotice
Definition: slog.h:129
@ levelError
Definition: slog.h:127
pthread_mutex_t lock
Definition: slog.h:135
bool _enable
Definition: slog.h:139
Level _level
Definition: slog.h:138
void clogEnable(bool f=true)
Enables or disables the echoing of the messages to clog in addition to the syslog daemon.
Definition: slog.h:263
void level(Level enable)
Sets the logging level.
Definition: slog.h:254
FILE * syslog
Definition: slog.h:136
Slog & warn(void)
Definition: slog.h:267
@ classLocal1
Definition: slog.h:114
@ classLocal6
Definition: slog.h:119
@ classDaemon
Definition: slog.h:110
@ classLocal0
Definition: slog.h:113
@ classLocal2
Definition: slog.h:115
@ classLocal3
Definition: slog.h:116
@ classUser
Definition: slog.h:111
@ classAudit
Definition: slog.h:109
@ classSecurity
Definition: slog.h:108
@ classDefault
Definition: slog.h:112
@ classLocal5
Definition: slog.h:118
@ classLocal4
Definition: slog.h:117
Slog & info(void)
Definition: slog.h:295
bool _clogEnable
Definition: slog.h:140
Slog & notice(void)
Definition: slog.h:291
Slog & alert(void)
Definition: slog.h:283
__DELETE_COPY(Slog)
Common C++ generic string class.
Common C++ thread class and sychronization objects.
#define __EXPORT
Definition: config.h:49
Definition: address.cpp:63
Slog slog
Definition: slog.cpp:64
__EXPORT AppLog & debug(AppLog &sl)
Manipulator for debug level.
Definition: applog.h:520
__EXPORT AppLog & alert(AppLog &sl)
Manipulator for alert level.
Definition: applog.h:552
__EXPORT AppLog & notice(AppLog &sl)
Manipulator for notice level.
Definition: applog.h:568
__EXPORT AppLog & info(AppLog &sl)
Manipulator for info level.
Definition: applog.h:576
__EXPORT AppLog & warn(AppLog &sl)
Manipulator for warn level.
Definition: applog.h:528
__EXPORT AppLog & emerg(AppLog &sl)
Manipulator for emerg level.
Definition: applog.h:544
__EXPORT AppLog & critical(AppLog &sl)
Manipulator for critical level.
Definition: applog.h:560
static shell::stringopt error('e', "--error", _TEXT("stderr path to use"), "filename")
#define __OVERRIDE
Definition: platform.h:158