ucommon
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 // As a special exception, you may use this file as part of a free software 00019 // library without restriction. Specifically, if other files instantiate 00020 // templates or use macros or inline functions from this file, or you compile 00021 // this file and link it with other files to produce an executable, this 00022 // file does not by itself cause the resulting executable to be covered by 00023 // the GNU General Public License. This exception does not however 00024 // invalidate any other reasons why the executable file might be covered by 00025 // the GNU General Public License. 00026 // 00027 // This exception applies only to the code released under the name GNU 00028 // Common C++. If you copy code from other releases into a copy of GNU 00029 // Common C++, as the General Public License permits, the exception does 00030 // not apply to the code that you add in this way. To avoid misleading 00031 // anyone as to the status of such modified files, you must delete 00032 // this exception notice from them. 00033 // 00034 // If you write modifications of your own for GNU Common C++, it is your choice 00035 // whether to permit this exception to apply to your modifications. 00036 // If you do not wish that, delete this exception notice. 00037 // 00038 00044 #ifndef COMMONCPP_SLOG_H_ 00045 #define COMMONCPP_SLOG_H_ 00046 00047 #include <cstdio> 00048 00049 #ifndef COMMONCPP_CONFIG_H_ 00050 #include <commoncpp/config.h> 00051 #endif 00052 00053 #ifndef COMMONCPP_STRING_H_ 00054 #include <commoncpp/string.h> 00055 #endif 00056 00057 #ifndef COMMONCPP_THREAD_H_ 00058 #include <commoncpp/thread.h> 00059 #endif 00060 00061 NAMESPACE_COMMONCPP 00062 00104 class __EXPORT Slog : protected std::streambuf, public std::ostream 00105 { 00106 public: 00107 typedef enum Class { 00108 classSecurity, 00109 classAudit, 00110 classDaemon, 00111 classUser, 00112 classDefault, 00113 classLocal0, 00114 classLocal1, 00115 classLocal2, 00116 classLocal3, 00117 classLocal4, 00118 classLocal5, 00119 classLocal6, 00120 classLocal7 00121 } Class; 00122 00123 typedef enum Level { 00124 levelEmergency = 1, 00125 levelAlert, 00126 levelCritical, 00127 levelError, 00128 levelWarning, 00129 levelNotice, 00130 levelInfo, 00131 levelDebug 00132 } Level; 00133 00134 private: 00135 pthread_mutex_t lock; 00136 FILE *syslog; 00137 int priority; 00138 Level _level; 00139 bool _enable; 00140 bool _clogEnable; 00141 00142 protected: 00148 int overflow(int c); 00149 00150 public: 00158 Slog(void); 00159 00160 virtual ~Slog(void); 00161 00162 void close(void); 00163 00169 void open(const char *ident, Class grp = classUser); 00170 00177 Slog &operator()(const char *ident, Class grp = classUser, 00178 Level level = levelError); 00179 00185 Slog &operator()(Level level, Class grp = classDefault); 00186 00190 Slog &operator()(void); 00191 00197 void error(const char *format, ...); 00198 00204 void warn(const char *format, ...); 00205 00211 void debug(const char *format, ...); 00212 00218 void emerg(const char *format, ...); 00219 00225 void alert(const char *format, ...); 00226 00232 void critical(const char *format, ...); 00233 00239 void notice(const char *format, ...); 00240 00246 void info(const char *format, ...); 00247 00252 inline void level(Level enable) 00253 {_level = enable;}; 00254 00260 inline void clogEnable(bool f=true) 00261 {_clogEnable = f;}; 00262 00263 inline Slog &warn(void) 00264 {return operator()(Slog::levelWarning);}; 00265 00266 inline Slog &error(void) 00267 {return operator()(Slog::levelError);}; 00268 00269 inline Slog &debug(void) 00270 {return operator()(Slog::levelDebug);}; 00271 00272 inline Slog &emerg(void) 00273 {return operator()(Slog::levelEmergency);}; 00274 00275 inline Slog &alert(void) 00276 {return operator()(Slog::levelAlert);}; 00277 00278 inline Slog &critical(void) 00279 {return operator()(Slog::levelCritical);}; 00280 00281 inline Slog ¬ice(void) 00282 {return operator()(Slog::levelNotice);}; 00283 00284 inline Slog &info(void) 00285 {return operator()(Slog::levelInfo);}; 00286 00287 }; 00288 00289 extern __EXPORT Slog slog; 00290 00291 END_NAMESPACE 00292 00293 #endif 00294