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 //
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 Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef COMMONCPP_SLOG_H_
44 #define COMMONCPP_SLOG_H_
45 
46 #include <cstdio>
47 
48 #ifndef COMMONCPP_CONFIG_H_
49 #include <commoncpp/config.h>
50 #endif
51 
52 #ifndef COMMONCPP_STRING_H_
53 #include <commoncpp/string.h>
54 #endif
55 
56 #ifndef COMMONCPP_THREAD_H_
57 #include <commoncpp/thread.h>
58 #endif
59 
60 NAMESPACE_COMMONCPP
61 
103 class __EXPORT Slog : protected std::streambuf, public std::ostream
104 {
105 public:
106  typedef enum Class {
107  classSecurity,
108  classAudit,
109  classDaemon,
110  classUser,
111  classDefault,
112  classLocal0,
113  classLocal1,
114  classLocal2,
115  classLocal3,
116  classLocal4,
117  classLocal5,
118  classLocal6,
119  classLocal7
120  } Class;
121 
122  typedef enum Level {
123  levelEmergency = 1,
124  levelAlert,
125  levelCritical,
126  levelError,
127  levelWarning,
128  levelNotice,
129  levelInfo,
130  levelDebug
131  } Level;
132 
133 private:
134  pthread_mutex_t lock;
135  FILE *syslog;
136  int priority;
137  Level _level;
138  bool _enable;
139  bool _clogEnable;
140 
141 protected:
147  int overflow(int c);
148 
149 public:
157  Slog(void);
158 
159  virtual ~Slog(void);
160 
161  void close(void);
162 
168  void open(const char *ident, Class grp = classUser);
169 
176  Slog &operator()(const char *ident, Class grp = classUser,
177  Level level = levelError);
178 
184  Slog &operator()(Level level, Class grp = classDefault);
185 
189  Slog &operator()(void);
190 
196  void error(const char *format, ...);
197 
203  void warn(const char *format, ...);
204 
210  void debug(const char *format, ...);
211 
217  void emerg(const char *format, ...);
218 
224  void alert(const char *format, ...);
225 
231  void critical(const char *format, ...);
232 
238  void notice(const char *format, ...);
239 
245  void info(const char *format, ...);
246 
251  inline void level(Level enable)
252  {_level = enable;};
253 
259  inline void clogEnable(bool f=true)
260  {_clogEnable = f;};
261 
262  inline Slog &warn(void)
263  {return operator()(Slog::levelWarning);};
264 
265  inline Slog &error(void)
266  {return operator()(Slog::levelError);};
267 
268  inline Slog &debug(void)
269  {return operator()(Slog::levelDebug);};
270 
271  inline Slog &emerg(void)
272  {return operator()(Slog::levelEmergency);};
273 
274  inline Slog &alert(void)
275  {return operator()(Slog::levelAlert);};
276 
277  inline Slog &critical(void)
278  {return operator()(Slog::levelCritical);};
279 
280  inline Slog &notice(void)
281  {return operator()(Slog::levelNotice);};
282 
283  inline Slog &info(void)
284  {return operator()(Slog::levelInfo);};
285 
286 };
287 
288 extern __EXPORT Slog slog;
289 
290 END_NAMESPACE
291 
292 #endif
293 
Common C++ thread class and sychronization objects.
void level(Level enable)
Sets the logging level.
Definition: slog.h:251
The slog class is used to stream messages to the system&#39;s logging facility (syslogd).
Definition: slog.h:103
void clogEnable(bool f=true)
Enables or disables the echoing of the messages to clog in addition to the syslog daemon...
Definition: slog.h:259
Common C++ generic string class.
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it&#39;s protocol.
Definition: access.h:239