ucommon
slog.h
Go to the documentation of this file.
1 // 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 
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 
61 NAMESPACE_COMMONCPP
62 
104 class __EXPORT Slog : protected std::streambuf, public std::ostream
105 {
106 public:
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 
134 private:
135  pthread_mutex_t lock;
136  FILE *syslog;
137  int priority;
138  Level _level;
139  bool _enable;
140  bool _clogEnable;
141 
142 protected:
148  int overflow(int c);
149 
150 public:
158  Slog(void);
159 
160  virtual ~Slog(void);
161 
162  void close(void);
163 
169  void open(const char *ident, Class grp = classUser);
170 
177  Slog &operator()(const char *ident, Class grp = classUser,
178  Level level = levelError);
179 
185  Slog &operator()(Level level, Class grp = classDefault);
186 
190  Slog &operator()(void);
191 
197  void error(const char *format, ...);
198 
204  void warn(const char *format, ...);
205 
211  void debug(const char *format, ...);
212 
218  void emerg(const char *format, ...);
219 
225  void alert(const char *format, ...);
226 
232  void critical(const char *format, ...);
233 
239  void notice(const char *format, ...);
240 
246  void info(const char *format, ...);
247 
252  inline void level(Level enable)
253  {_level = enable;};
254 
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 
289 extern __EXPORT Slog slog;
290 
291 END_NAMESPACE
292 
293 #endif
294