UCommon
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
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
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 mutable pthread_mutex_t lock;
136 FILE *syslog;
137 int priority;
138 Level _level;
139 bool _enable;
140 bool _clogEnable;
141
142 __DELETE_COPY(Slog);
143
144protected:
150 int overflow(int c) __OVERRIDE;
151
152public:
160 Slog(void);
161
162 virtual ~Slog(void);
163
164 void close(void);
165
171 void open(const char *ident, Class grp = classUser);
172
179 Slog &operator()(const char *ident, Class grp = classUser,
180 Level level = levelError);
181
187 Slog &operator()(Level level, Class grp = classDefault);
188
193
199 void error(const char *format, ...);
200
206 void warn(const char *format, ...);
207
213 void debug(const char *format, ...);
214
220 void emerg(const char *format, ...);
221
227 void alert(const char *format, ...);
228
234 void critical(const char *format, ...);
235
241 void notice(const char *format, ...);
242
248 void info(const char *format, ...);
249
254 inline void level(Level enable) {
255 _level = enable;
256 }
257
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
AppLog & notice(AppLog &sl)
Manipulator for notice level.
Definition applog.h:568
AppLog & alert(AppLog &sl)
Manipulator for alert level.
Definition applog.h:552
AppLog & debug(AppLog &sl)
Manipulator for debug level.
Definition applog.h:520
AppLog & critical(AppLog &sl)
Manipulator for critical level.
Definition applog.h:560
AppLog & emerg(AppLog &sl)
Manipulator for emerg level.
Definition applog.h:544
AppLog & warn(AppLog &sl)
Manipulator for warn level.
Definition applog.h:528
AppLog & info(AppLog &sl)
Manipulator for info level.
Definition applog.h:576
The slog class is used to stream messages to the system's logging facility (syslogd).
Definition slog.h:105
void error(const char *format,...)
Print a formatted syslog string.
void info(const char *format,...)
Print a formatted syslog string.
void warn(const char *format,...)
Print a formatted syslog string.
void alert(const char *format,...)
Print a formatted syslog string.
void notice(const char *format,...)
Print a formatted syslog string.
Slog(void)
Default (and only) constructor.
int overflow(int c)
This is the streambuf function that actually outputs the data to the device.
void debug(const char *format,...)
Print a formatted syslog string.
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 critical(const char *format,...)
Print a formatted syslog string.
void emerg(const char *format,...)
Print a formatted syslog string.
void level(Level enable)
Sets the logging level.
Definition slog.h:254
Slog & operator()(void)
Does nothing except return *this.
Slog & operator()(Level level, Class grp=classDefault)
Changes the log level and class to use for subsequent output.
void open(const char *ident, Class grp=classUser)
(re)opens the output stream.
Slog & operator()(const char *ident, Class grp=classUser, Level level=levelError)
Sets the log identifier, level, and class to use for subsequent output.
Common C++ generic string class.
Common C++ thread class and sychronization objects.