GNU CommonC++

slog.h

Go to the documentation of this file.
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 CCXX_SLOG_H_
00045 #define CCXX_SLOG_H_
00046 
00047 #ifndef CCXX_MISSING_H_
00048 #include <cc++/missing.h>
00049 #endif
00050 
00051 #ifndef CCXX_THREAD_H_
00052 #include <cc++/thread.h>
00053 #endif
00054 
00055 #ifndef HAVE_SYSLOG_H
00056 #include <cstdio>
00057 #endif
00058 
00059 #ifdef  CCXX_NAMESPACES
00060 namespace ost {
00061 #endif
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 #ifndef HAVE_SYSLOG_H
00136     Mutex lock;
00137     FILE *syslog;
00138 #endif
00139     int priority;
00140     Level  _level;
00141     bool _enable;
00142     bool _clogEnable;
00143 
00144     ThreadImpl *getPriv(void);
00145 
00146 protected:
00152     int overflow(int c);
00153 
00154 public:
00162     Slog(void);
00163 
00164     virtual ~Slog(void);
00165 
00166     void close(void);
00167 
00173     void open(const char *ident, Class grp = classUser);
00174 
00181     Slog &operator()(const char *ident, Class grp = classUser,
00182              Level level = levelError);
00183 
00189     Slog &operator()(Level level, Class grp = classDefault);
00190 
00194     Slog &operator()(void);
00195 
00196 #ifdef  HAVE_SNPRINTF
00197 
00202     void error(const char *format, ...);
00203 
00209     void warn(const char *format, ...);
00210 
00216     void debug(const char *format, ...);
00217 
00223     void emerg(const char *format, ...);
00224 
00230     void alert(const char *format, ...);
00231 
00237     void critical(const char *format, ...);
00238 
00244     void notice(const char *format, ...);
00245 
00251     void info(const char *format, ...);
00252 #endif
00253 
00258     inline void level(Level enable)
00259         {_level = enable;};
00260 
00266     inline void clogEnable(bool f=true)
00267         {_clogEnable = f;};
00268 
00269     inline Slog &warn(void)
00270         {return operator()(Slog::levelWarning);};
00271 
00272     inline Slog &error(void)
00273         {return operator()(Slog::levelError);};
00274 
00275     inline Slog &debug(void)
00276         {return operator()(Slog::levelDebug);};
00277 
00278     inline Slog &emerg(void)
00279         {return operator()(Slog::levelEmergency);};
00280 
00281     inline Slog &alert(void)
00282         {return operator()(Slog::levelAlert);};
00283 
00284     inline Slog &critical(void)
00285         {return operator()(Slog::levelCritical);};
00286 
00287     inline Slog &notice(void)
00288         {return operator()(Slog::levelNotice);};
00289 
00290     inline Slog &info(void)
00291         {return operator()(Slog::levelInfo);};
00292 
00293 };
00294 
00295 //#ifdef    CYGWIN_IMPORTS
00296 //extern __declspec(dllimport) Slog slog;
00297 //#else
00298 extern __EXPORT Slog    slog;
00299 //#endif
00300 
00301 #ifdef  CCXX_NAMESPACES
00302 }
00303 #endif
00304 
00305 #endif
00306