ucommon
commoncpp/slog.h
Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2014 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 Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef COMMONCPP_SLOG_H_
00044 #define COMMONCPP_SLOG_H_
00045 
00046 #include <cstdio>
00047 
00048 #ifndef COMMONCPP_CONFIG_H_
00049 #include <commoncpp/config.h>
00050 #endif
00051 
00052 #ifndef COMMONCPP_STRING_H_
00053 #include <commoncpp/string.h>
00054 #endif
00055 
00056 #ifndef COMMONCPP_THREAD_H_
00057 #include <commoncpp/thread.h>
00058 #endif
00059 
00060 namespace ost {
00061 
00103 class __EXPORT Slog : protected std::streambuf, public std::ostream
00104 {
00105 public:
00106     typedef enum Class {
00107         classSecurity,
00108         classAudit,
00109         classDaemon,
00110         classUser,
00111         classDefault,
00112         classLocal0,
00113         classLocal1,
00114         classLocal2,
00115         classLocal3,
00116         classLocal4,
00117         classLocal5,
00118         classLocal6,
00119         classLocal7
00120     } Class;
00121 
00122     typedef enum Level {
00123         levelEmergency = 1,
00124         levelAlert,
00125         levelCritical,
00126         levelError,
00127         levelWarning,
00128         levelNotice,
00129         levelInfo,
00130         levelDebug
00131     } Level;
00132 
00133 private:
00134     pthread_mutex_t lock;
00135     FILE *syslog;
00136     int priority;
00137     Level  _level;
00138     bool _enable;
00139     bool _clogEnable;
00140 
00141 protected:
00147     int overflow(int c);
00148 
00149 public:
00157     Slog(void);
00158 
00159     virtual ~Slog(void);
00160 
00161     void close(void);
00162 
00168     void open(const char *ident, Class grp = classUser);
00169 
00176     Slog &operator()(const char *ident, Class grp = classUser,
00177              Level level = levelError);
00178 
00184     Slog &operator()(Level level, Class grp = classDefault);
00185 
00189     Slog &operator()(void);
00190 
00196     void error(const char *format, ...);
00197 
00203     void warn(const char *format, ...);
00204 
00210     void debug(const char *format, ...);
00211 
00217     void emerg(const char *format, ...);
00218 
00224     void alert(const char *format, ...);
00225 
00231     void critical(const char *format, ...);
00232 
00238     void notice(const char *format, ...);
00239 
00245     void info(const char *format, ...);
00246 
00251     inline void level(Level enable)
00252         {_level = enable;}
00253 
00259     inline void clogEnable(bool f=true)
00260         {_clogEnable = f;}
00261 
00262     inline Slog &warn(void)
00263         {return operator()(Slog::levelWarning);}
00264 
00265     inline Slog &error(void)
00266         {return operator()(Slog::levelError);}
00267 
00268     inline Slog &debug(void)
00269         {return operator()(Slog::levelDebug);}
00270 
00271     inline Slog &emerg(void)
00272         {return operator()(Slog::levelEmergency);}
00273 
00274     inline Slog &alert(void)
00275         {return operator()(Slog::levelAlert);}
00276 
00277     inline Slog &critical(void)
00278         {return operator()(Slog::levelCritical);}
00279 
00280     inline Slog &notice(void)
00281         {return operator()(Slog::levelNotice);}
00282 
00283     inline Slog &info(void)
00284         {return operator()(Slog::levelInfo);}
00285 
00286 };
00287 
00288 extern __EXPORT Slog slog;
00289 
00290 } // namespace ost
00291 
00292 #endif
00293