svcproc.h

Go to the documentation of this file.
00001 /*
00002  * svcproc.h
00003  *
00004  * Service Process (daemon) class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 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  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 22378 $
00030  * $Author: rjongbloed $
00031  * $Date: 2009-04-06 22:20:13 -0500 (Mon, 06 Apr 2009) $
00032  */
00033 
00034 #ifndef PTLIB_SERVICEPROCESS_H
00035 #define PTLIB_SERVICEPROCESS_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/pprocess.h>
00042 
00047 class PSystemLog : public PObject, public iostream {
00048   PCLASSINFO(PSystemLog, PObject);
00049 
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     ) : iostream(cout.rdbuf())
00085       , logLevel(level)
00086     { 
00087       buffer.log = this; 
00088       init(&buffer); 
00089     }
00090 
00092     ~PSystemLog() { flush(); }
00094 
00099     static void Output(
00100       Level level,      
00101       const char * msg  
00102     );
00104 
00110     void SetLevel(
00111       Level level  
00112     ) { logLevel = level; }
00113 
00119     Level GetLevel() const { return logLevel; }
00121 
00122   private:
00123     PSystemLog(const PSystemLog & other) : PObject(other), iostream(cout.rdbuf()) { }
00124     PSystemLog & operator=(const PSystemLog &) { return *this; }
00125 
00126     class Buffer : public streambuf {
00127       public:
00128         virtual int_type overflow(int=EOF);
00129         virtual int_type underflow();
00130         virtual int sync();
00131         PSystemLog * log;
00132         PString string;
00133     } buffer;
00134     friend class Buffer;
00135 
00136     Level logLevel;
00137 };
00138 
00139 
00144 #define PSYSTEMLOG(level, variables) \
00145   if (PServiceProcess::Current().GetLogLevel() >= PSystemLog::level) { \
00146     PSystemLog P_systemlog(PSystemLog::level); \
00147     P_systemlog << variables; \
00148   } else (void)0
00149 
00150 
00151 
00155 class PServiceProcess : public PProcess
00156 {
00157   PCLASSINFO(PServiceProcess, PProcess);
00158 
00159   public:
00164     PServiceProcess(
00165       const char * manuf,   
00166       const char * name,    
00167       WORD majorVersion,    
00168       WORD minorVersion,    
00169       CodeStatus status,    
00170       WORD buildNumber      
00171     );
00173 
00183     virtual PBoolean OnStart() = 0;
00184 
00189     virtual void OnStop();
00190 
00199     virtual PBoolean OnPause();
00200 
00203     virtual void OnContinue();
00204 
00207     virtual void OnControl() = 0;
00209 
00217     static PServiceProcess & Current();
00218 
00219 
00229     void SetLogLevel(
00230       PSystemLog::Level level  
00231     ) { currentLogLevel = level; }
00232 
00238     PSystemLog::Level GetLogLevel() const { return currentLogLevel; }
00240 
00241 
00242     /* Internal initialisation function called directly from
00243        #main()#. The user should never call this function.
00244      */
00245     virtual int InternalMain(void * arg = NULL);
00246 
00247 
00248   protected:
00249   // Member variables
00251     PBoolean debugMode;
00252 
00254     PSystemLog::Level currentLogLevel;
00255 
00256     friend void PSystemLog::Output(PSystemLog::Level, const char *);
00257 
00258 
00259 // Include platform dependent part of class
00260 #ifdef _WIN32
00261 #include "msos/ptlib/svcproc.h"
00262 #else
00263 #include "unix/ptlib/svcproc.h"
00264 #endif
00265 };
00266 
00267 
00268 #endif // PTLIB_SERVICEPROCESS_H
00269 
00270 
00271 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:48 2010 for PTLib by  doxygen 1.4.7