syslog.h

Go to the documentation of this file.
00001 /*
00002  * syslog.h
00003  *
00004  * System Logging class.
00005  *
00006  * Portable Tools Library
00007  *
00008  * Copyright (c) 2009 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Revision: 21583 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-11-20 23:56:31 +1100 (Thu, 20 Nov 2008) $
00029  */
00030 
00031 #ifndef _PSYSTEMLOG
00032 #define _PSYSTEMLOG
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #include "ptlib/udpsock.h"
00039 
00040 class PSystemLogTarget;
00041 
00042 
00047 class PSystemLog : public PObject, public iostream
00048 {
00049     PCLASSINFO(PSystemLog, PObject);
00050   public:
00053 
00054     enum Level {
00056       StdError = -1,
00058       Fatal,   
00060       Error,    
00062       Warning,  
00064       Info,     
00066       Debug,    
00068       Debug2,   
00070       Debug3,   
00072       Debug4,   
00074       Debug5,   
00076       Debug6,   
00077 
00078       NumLogLevels
00079     };
00080 
00082     PSystemLog(
00083      Level level   
00084     );
00085 
00087     ~PSystemLog() { flush(); }
00089 
00094     static PSystemLogTarget & GetTarget();
00095 
00098     static void SetTarget(
00099       PSystemLogTarget * target,  
00100       bool autoDelete = true      
00101     );
00103 
00104   private:
00105     PSystemLog(const PSystemLog & other);
00106     PSystemLog & operator=(const PSystemLog &);
00107 
00108     class Buffer : public streambuf {
00109       public:
00110         Buffer();
00111         virtual int_type overflow(int_type=EOF);
00112         virtual int_type underflow();
00113         virtual int sync();
00114         PSystemLog * m_log;
00115         PString      m_string;
00116     } m_buffer;
00117     friend class Buffer;
00118 
00119     Level m_logLevel;
00120 
00121   friend class PSystemLogTarget;
00122 };
00123 
00124 
00125 class PSystemLogTarget : public PObject
00126 {
00127     PCLASSINFO(PSystemLogTarget, PObject);
00128   public:
00131     PSystemLogTarget();
00133 
00139     void SetThresholdLevel(
00140       PSystemLog::Level level  
00141     ) { m_thresholdLevel = level; }
00142 
00148     PSystemLog::Level GetThresholdLevel() const { return m_thresholdLevel; }
00150 
00151   protected:
00156     virtual void Output(
00157       PSystemLog::Level level,  
00158       const char * msg          
00159     ) = 0;
00160 
00163     void OutputToStream(
00164       ostream & strm,           
00165       PSystemLog::Level level,  
00166       const char * msg          
00167     );
00169 
00170   protected:
00171     PSystemLog::Level m_thresholdLevel;
00172 
00173   private:
00174     PSystemLogTarget(const PSystemLogTarget & other);
00175     PSystemLogTarget & operator=(const PSystemLogTarget &);
00176 
00177   friend class PSystemLog::Buffer;
00178 };
00179 
00180 
00183 class PSystemLogToNowhere : public PSystemLogTarget
00184 {
00185     PCLASSINFO(PSystemLogToNowhere, PSystemLogTarget);
00186   public:
00187     virtual void Output(PSystemLog::Level, const char *)
00188     {
00189     }
00190 };
00191 
00192 
00195 class PSystemLogToStderr : public PSystemLogTarget
00196 {
00197     PCLASSINFO(PSystemLogToStderr, PSystemLogTarget);
00198   public:
00203     virtual void Output(
00204       PSystemLog::Level level,  
00205       const char * msg          
00206     );
00208 };
00209 
00210 
00213 class PSystemLogToFile : public PSystemLogTarget
00214 {
00215     PCLASSINFO(PSystemLogToFile, PSystemLogTarget);
00216   public:
00219     PSystemLogToFile(
00220       const PString & filename
00221     );
00223 
00228     virtual void Output(
00229       PSystemLog::Level level,  
00230       const char * msg          
00231     );
00233 
00238     const PFilePath & GetFilePath() const { return m_file.GetFilePath(); }
00240 
00241   protected:
00242     PTextFile m_file;
00243 };
00244 
00245 
00248 class PSystemLogToNetwork : public PSystemLogTarget
00249 {
00250     PCLASSINFO(PSystemLogToNetwork, PSystemLogTarget);
00251   public:
00252     enum { RFC3164_Port = 514 };
00253 
00256     PSystemLogToNetwork(
00257       const PIPSocket::Address & address, 
00258       WORD port = RFC3164_Port,           
00259       unsigned facility = 16              
00260     );
00261     PSystemLogToNetwork(
00262       const PString & hostname, 
00263       WORD port = RFC3164_Port,           
00264       unsigned facility = 16              
00265     );
00267 
00272     virtual void Output(
00273       PSystemLog::Level level,  
00274       const char * msg          
00275     );
00277 
00278   protected:
00279     PIPSocket::Address m_host;
00280     WORD               m_port;
00281     unsigned           m_facility;
00282     PUDPSocket         m_socket;
00283 };
00284 
00285 
00286 #ifdef WIN32
00287 
00289 class PSystemLogToDebug : public PSystemLogTarget
00290 {
00291     PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
00292   public:
00297     virtual void Output(
00298       PSystemLog::Level level,  
00299       const char * msg          
00300     );
00302 };
00303 #elif !defined(P_VXWORKS)
00304 
00306 class PSystemLogToSyslog : public PSystemLogTarget
00307 {
00308     PCLASSINFO(PSystemLogToSyslog, PSystemLogTarget);
00309   public:
00312     PSystemLogToSyslog();
00313     ~PSystemLogToSyslog();
00315 
00320     virtual void Output(
00321       PSystemLog::Level level,  
00322       const char * msg          
00323     );
00325 };
00326 #endif
00327 
00328 
00333 #define PSYSTEMLOG(level, variables) \
00334   if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
00335     PSystemLog P_systemlog(PSystemLog::level); \
00336     P_systemlog << variables; \
00337   } else (void)0
00338 
00339 
00340 #endif
00341 
00342 
00343 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7