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 * $Log: svcproc.h,v $ 00030 * Revision 1.24 2003/09/17 05:41:59 csoutheren 00031 * Removed recursive includes 00032 * 00033 * Revision 1.23 2003/09/17 01:18:02 csoutheren 00034 * Removed recursive include file system and removed all references 00035 * to deprecated coooperative threading support 00036 * 00037 * Revision 1.22 2002/10/22 07:42:52 robertj 00038 * Added extra debugging for file handle and thread leak detection. 00039 * 00040 * Revision 1.21 2002/09/16 01:08:59 robertj 00041 * Added #define so can select if #pragma interface/implementation is used on 00042 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00043 * 00044 * Revision 1.20 2002/01/26 23:55:55 craigs 00045 * Changed for GCC 3.0 compatibility, thanks to manty@manty.net 00046 * 00047 * Revision 1.19 2001/05/22 12:49:32 robertj 00048 * Did some seriously wierd rewrite of platform headers to eliminate the 00049 * stupid GNU compiler warning about braces not matching. 00050 * 00051 * Revision 1.18 1999/09/21 08:20:16 robertj 00052 * Fixed name space problem with PSYSTEMLOG() macro. 00053 * 00054 * Revision 1.17 1999/09/13 13:15:06 robertj 00055 * Changed PTRACE so will output to system log in PServiceProcess applications. 00056 * 00057 * Revision 1.16 1999/03/09 02:59:51 robertj 00058 * Changed comments to doc++ compatible documentation. 00059 * 00060 * Revision 1.15 1999/02/16 08:11:17 robertj 00061 * MSVC 6.0 compatibility changes. 00062 * 00063 * Revision 1.14 1998/10/13 14:06:15 robertj 00064 * Complete rewrite of memory leak detection code. 00065 * 00066 * Revision 1.13 1998/09/23 06:21:31 robertj 00067 * Added open source copyright license. 00068 * 00069 * Revision 1.12 1998/04/07 13:33:21 robertj 00070 * Changed startup code to support PApplication class. 00071 * 00072 * Revision 1.11 1998/03/29 06:16:50 robertj 00073 * Rearranged initialisation sequence so PProcess descendent constructors can do "things". 00074 * 00075 * Revision 1.10 1998/02/16 00:13:16 robertj 00076 * Added tray icon support. 00077 * 00078 * Revision 1.9 1998/02/03 06:19:14 robertj 00079 * Added extra log levels. 00080 * 00081 * Revision 1.8 1997/07/08 13:02:32 robertj 00082 * DLL support. 00083 * 00084 * Revision 1.7 1997/02/05 11:51:15 robertj 00085 * Changed current process function to return reference and validate objects descendancy. 00086 * 00087 * Revision 1.6 1996/08/19 13:39:20 robertj 00088 * Added "Debug" level to system log. 00089 * Moved PSYSTEMLOG macro to common code. 00090 * Changed PSYSTEMLOG macro so does not execute << expression if below debug level. 00091 * Fixed memory leak in PSystemLog stream buffer. 00092 * 00093 * Revision 1.5 1996/08/17 10:00:27 robertj 00094 * Changes for Windows DLL support. 00095 * 00096 * Revision 1.4 1996/08/09 11:16:53 robertj 00097 * Moved log macro to platform dependent header. 00098 * 00099 * Revision 1.3 1996/07/30 12:24:13 robertj 00100 * Added SYSTEMLOG macro for GNU compiler compatibility. 00101 * 00102 * Revision 1.2 1996/07/27 04:10:06 robertj 00103 * Changed SystemLog to be stream based rather than printf based. 00104 * 00105 * Revision 1.1 1995/12/23 03:47:25 robertj 00106 * Initial revision 00107 * 00108 * Revision 1.3 1995/12/10 11:50:05 robertj 00109 * Numerous fixes for WIN32 service processes. 00110 * 00111 * Revision 1.2 1995/07/02 01:23:27 robertj 00112 * Set up service process to be in subthread not main thread. 00113 * 00114 * Revision 1.1 1995/06/17 00:50:54 robertj 00115 * Initial revision 00116 * 00117 */ 00118 00119 #ifndef _PSERVICEPROCESS 00120 #define _PSERVICEPROCESS 00121 00122 #ifdef P_USE_PRAGMA 00123 #pragma interface 00124 #endif 00125 00130 class PSystemLog : public PObject, public iostream { 00131 PCLASSINFO(PSystemLog, PObject); 00132 00133 public: 00136 00137 enum Level { 00139 StdError = -1, 00141 Fatal, 00143 Error, 00145 Warning, 00147 Info, 00149 Debug, 00151 Debug2, 00153 Debug3, 00155 Debug4, 00157 Debug5, 00159 Debug6, 00160 00161 NumLogLevels 00162 }; 00163 00165 PSystemLog( 00166 Level level 00167 ) : iostream(cout.rdbuf()) { logLevel = level; buffer.log = this; init(&buffer); } 00168 00170 ~PSystemLog() { flush(); } 00172 00177 static void Output( 00178 Level level, 00179 const char * msg 00180 ); 00182 00188 void SetLevel( 00189 Level level 00190 ) { logLevel = level; } 00191 00197 Level GetLevel() const { return logLevel; } 00199 00200 private: 00201 PSystemLog(const PSystemLog &) : iostream(cout.rdbuf()) { } 00202 PSystemLog & operator=(const PSystemLog &) { return *this; } 00203 00204 class Buffer : public streambuf { 00205 public: 00206 virtual int overflow(int=EOF); 00207 virtual int underflow(); 00208 virtual int sync(); 00209 PSystemLog * log; 00210 PString string; 00211 } buffer; 00212 friend class Buffer; 00213 00214 Level logLevel; 00215 }; 00216 00217 00222 #define PSYSTEMLOG(level, variables) \ 00223 if (PServiceProcess::Current().GetLogLevel() >= PSystemLog::level) { \ 00224 PSystemLog P_systemlog(PSystemLog::level); \ 00225 P_systemlog << variables; \ 00226 } else (void)0 00227 00228 00229 00233 class PServiceProcess : public PProcess 00234 { 00235 PCLASSINFO(PServiceProcess, PProcess); 00236 00237 public: 00242 PServiceProcess( 00243 const char * manuf, 00244 const char * name, 00245 WORD majorVersion, 00246 WORD minorVersion, 00247 CodeStatus status, 00248 WORD buildNumber 00249 ); 00251 00261 virtual BOOL OnStart() = 0; 00262 00267 virtual void OnStop(); 00268 00277 virtual BOOL OnPause(); 00278 00281 virtual void OnContinue(); 00282 00285 virtual void OnControl() = 0; 00287 00295 static PServiceProcess & Current(); 00296 00297 00307 void SetLogLevel( 00308 PSystemLog::Level level 00309 ) { currentLogLevel = level; } 00310 00316 PSystemLog::Level GetLogLevel() const { return currentLogLevel; } 00318 00319 00320 /* Internal initialisation function called directly from 00321 #main()#. The user should never call this function. 00322 */ 00323 virtual int _main(void * arg = NULL); 00324 00325 00326 protected: 00327 // Member variables 00329 BOOL debugMode; 00330 00332 PSystemLog::Level currentLogLevel; 00333 00334 friend void PSystemLog::Output(PSystemLog::Level, const char *); 00335 00336 00337 // Include platform dependent part of class 00338 #ifdef _WIN32 00339 #include "msos/ptlib/svcproc.h" 00340 #else 00341 #include "unix/ptlib/svcproc.h" 00342 #endif 00343 }; 00344 00345 #endif 00346 00347 00348 // End Of File ///////////////////////////////////////////////////////////////