00001 #ifndef __SYS_LOGGER_H__ 00002 #define __SYS_LOGGER_H__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S y s L o g g e r . h h */ 00006 /* */ 00007 /*(c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /*Produced by Andrew Hanushevsky for Stanford University under contract */ 00009 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */ 00010 /* */ 00011 /* This file is part of the XRootD software suite. */ 00012 /* */ 00013 /* XRootD is free software: you can redistribute it and/or modify it under */ 00014 /* the terms of the GNU Lesser General Public License as published by the */ 00015 /* Free Software Foundation, either version 3 of the License, or (at your */ 00016 /* option) any later version. */ 00017 /* */ 00018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00020 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00021 /* License for more details. */ 00022 /* */ 00023 /* You should have received a copy of the GNU Lesser General Public License */ 00024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00025 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00026 /* */ 00027 /* The copyright holder's institutional names and contributor's names may not */ 00028 /* be used to endorse or promote products derived from this software without */ 00029 /* specific prior written permission of the institution or contributor. */ 00030 /******************************************************************************/ 00031 00032 #include <stdlib.h> 00033 #ifndef WIN32 00034 #include <unistd.h> 00035 #include <string.h> 00036 #include <strings.h> 00037 #else 00038 #include <string.h> 00039 #include <io.h> 00040 #include "XrdSys/XrdWin32.hh" 00041 #endif 00042 00043 #include "XrdSys/XrdSysPthread.hh" 00044 00045 //----------------------------------------------------------------------------- 00048 //----------------------------------------------------------------------------- 00049 00050 class XrdOucTListFIFO; 00051 00052 class XrdSysLogger 00053 { 00054 public: 00055 00056 //----------------------------------------------------------------------------- 00063 //----------------------------------------------------------------------------- 00064 00065 XrdSysLogger(int ErrFD=STDERR_FILENO, int xrotate=1); 00066 00067 //----------------------------------------------------------------------------- 00069 //----------------------------------------------------------------------------- 00070 00071 ~XrdSysLogger() 00072 { 00073 RmLogRotateLock(); 00074 if (ePath) 00075 free(ePath); 00076 } 00077 00078 //----------------------------------------------------------------------------- 00082 //----------------------------------------------------------------------------- 00083 00084 void AddMsg(const char *msg); 00085 00086 //----------------------------------------------------------------------------- 00090 //----------------------------------------------------------------------------- 00091 00092 class Task 00093 { 00094 public: 00095 friend class XrdSysLogger; 00096 00097 virtual void Ring() = 0; 00098 00099 inline Task *Next() {return next;} 00100 00101 Task() : next(0) {} 00102 virtual ~Task() {} 00103 00104 private: 00105 Task *next; 00106 }; 00107 00108 void AtMidnight(Task *mnTask); 00109 00110 //----------------------------------------------------------------------------- 00132 //----------------------------------------------------------------------------- 00133 00134 static const int onFifo = (int)0x80000000; 00135 00136 int Bind(const char *path, int lfh=0); 00137 00138 //----------------------------------------------------------------------------- 00144 //----------------------------------------------------------------------------- 00145 00146 void Capture(XrdOucTListFIFO *tFIFO); 00147 00148 //----------------------------------------------------------------------------- 00150 //----------------------------------------------------------------------------- 00151 00152 void Flush() {fsync(eFD);} 00153 00154 //----------------------------------------------------------------------------- 00158 //----------------------------------------------------------------------------- 00159 00160 int originalFD() {return baseFD;} 00161 00162 //----------------------------------------------------------------------------- 00171 //----------------------------------------------------------------------------- 00172 00173 int ParseKeep(const char *arg); 00174 00175 //----------------------------------------------------------------------------- 00181 //----------------------------------------------------------------------------- 00182 00183 void Put(int iovcnt, struct iovec *iov); 00184 00185 //----------------------------------------------------------------------------- 00187 //----------------------------------------------------------------------------- 00188 00189 static 00190 void setForwarding(bool onoff) {doForward = onoff;} 00191 00192 //----------------------------------------------------------------------------- 00194 //----------------------------------------------------------------------------- 00195 00196 void setHiRes() {hiRes = true;} 00197 00198 //----------------------------------------------------------------------------- 00203 //----------------------------------------------------------------------------- 00204 00205 void setKeep(long long knum) {eKeep = knum;} 00206 00207 //----------------------------------------------------------------------------- 00212 //----------------------------------------------------------------------------- 00213 00214 void setRotate(int onoff) {doLFR = onoff;} 00215 00216 //----------------------------------------------------------------------------- 00221 //----------------------------------------------------------------------------- 00222 00223 char *traceBeg() {Logger_Mutex.Lock(); Time(TBuff); return TBuff;} 00224 00225 //----------------------------------------------------------------------------- 00230 //----------------------------------------------------------------------------- 00231 00232 char traceEnd() {Logger_Mutex.UnLock(); return '\n';} 00233 00234 //----------------------------------------------------------------------------- 00238 //----------------------------------------------------------------------------- 00239 00240 const char *xlogFN() {return (ePath ? ePath : "stderr");} 00241 00242 //----------------------------------------------------------------------------- 00245 //----------------------------------------------------------------------------- 00246 00247 void zHandler(); 00248 00249 private: 00250 int FifoMake(); 00251 void FifoWait(); 00252 int Time(char *tbuff); 00253 static int TimeStamp(struct timeval &tVal, unsigned long tID, 00254 char *tbuff, int tbsz, bool hires); 00255 int HandleLogRotateLock( bool dorotate ); 00256 void RmLogRotateLock(); 00257 00258 struct mmMsg 00259 {mmMsg *next; 00260 int mlen; 00261 char *msg; 00262 }; 00263 mmMsg *msgList; 00264 Task *taskQ; 00265 XrdSysMutex Logger_Mutex; 00266 long long eKeep; 00267 char TBuff[32]; // Trace header buffer 00268 int eFD; 00269 int baseFD; 00270 char *ePath; 00271 char Filesfx[8]; 00272 int eInt; 00273 int reserved1; 00274 char *fifoFN; 00275 bool hiRes; 00276 bool doLFR; 00277 pthread_t lfhTID; 00278 00279 static bool doForward; 00280 00281 void putEmsg(char *msg, int msz); 00282 int ReBind(int dorename=1); 00283 void Trim(); 00284 }; 00285 #endif