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 XrdSysLogger 00051 { 00052 public: 00053 00054 //----------------------------------------------------------------------------- 00061 //----------------------------------------------------------------------------- 00062 00063 XrdSysLogger(int ErrFD=STDERR_FILENO, int xrotate=1); 00064 00065 //----------------------------------------------------------------------------- 00067 //----------------------------------------------------------------------------- 00068 00069 ~XrdSysLogger() 00070 { 00071 RmLogRotateLock(); 00072 if (ePath) 00073 free(ePath); 00074 } 00075 00076 //----------------------------------------------------------------------------- 00080 //----------------------------------------------------------------------------- 00081 00082 void AddMsg(const char *msg); 00083 00084 //----------------------------------------------------------------------------- 00088 //----------------------------------------------------------------------------- 00089 00090 class Task 00091 { 00092 public: 00093 friend class XrdSysLogger; 00094 00095 virtual void Ring() = 0; 00096 00097 inline Task *Next() {return next;} 00098 00099 Task() : next(0) {} 00100 virtual ~Task() {} 00101 00102 private: 00103 Task *next; 00104 }; 00105 00106 void AtMidnight(Task *mnTask); 00107 00108 //----------------------------------------------------------------------------- 00130 //----------------------------------------------------------------------------- 00131 00132 static const int onFifo = (int)0x80000000; 00133 00134 int Bind(const char *path, int lfh=0); 00135 00136 //----------------------------------------------------------------------------- 00138 //----------------------------------------------------------------------------- 00139 00140 void Flush() {fsync(eFD);} 00141 00142 //----------------------------------------------------------------------------- 00146 //----------------------------------------------------------------------------- 00147 00148 int originalFD() {return baseFD;} 00149 00150 //----------------------------------------------------------------------------- 00159 //----------------------------------------------------------------------------- 00160 00161 int ParseKeep(const char *arg); 00162 00163 //----------------------------------------------------------------------------- 00169 //----------------------------------------------------------------------------- 00170 00171 void Put(int iovcnt, struct iovec *iov); 00172 00173 //----------------------------------------------------------------------------- 00175 //----------------------------------------------------------------------------- 00176 00177 static 00178 void setForwarding(bool onoff) {doForward = onoff;} 00179 00180 //----------------------------------------------------------------------------- 00182 //----------------------------------------------------------------------------- 00183 00184 void setHiRes() {hiRes = true;} 00185 00186 //----------------------------------------------------------------------------- 00191 //----------------------------------------------------------------------------- 00192 00193 void setKeep(long long knum) {eKeep = knum;} 00194 00195 //----------------------------------------------------------------------------- 00200 //----------------------------------------------------------------------------- 00201 00202 void setRotate(int onoff) {doLFR = onoff;} 00203 00204 //----------------------------------------------------------------------------- 00209 //----------------------------------------------------------------------------- 00210 00211 char *traceBeg() {Logger_Mutex.Lock(); Time(TBuff); return TBuff;} 00212 00213 //----------------------------------------------------------------------------- 00218 //----------------------------------------------------------------------------- 00219 00220 char traceEnd() {Logger_Mutex.UnLock(); return '\n';} 00221 00222 //----------------------------------------------------------------------------- 00226 //----------------------------------------------------------------------------- 00227 00228 const char *xlogFN() {return (ePath ? ePath : "stderr");} 00229 00230 //----------------------------------------------------------------------------- 00233 //----------------------------------------------------------------------------- 00234 00235 void zHandler(); 00236 00237 private: 00238 int FifoMake(); 00239 void FifoWait(); 00240 int Time(char *tbuff); 00241 static int TimeStamp(struct timeval &tVal, unsigned long tID, 00242 char *tbuff, int tbsz, bool hires); 00243 int HandleLogRotateLock( bool dorotate ); 00244 void RmLogRotateLock(); 00245 00246 struct mmMsg 00247 {mmMsg *next; 00248 int mlen; 00249 char *msg; 00250 }; 00251 mmMsg *msgList; 00252 Task *taskQ; 00253 XrdSysMutex Logger_Mutex; 00254 long long eKeep; 00255 char TBuff[32]; // Trace header buffer 00256 int eFD; 00257 int baseFD; 00258 char *ePath; 00259 char Filesfx[8]; 00260 int eInt; 00261 int reserved1; 00262 char *fifoFN; 00263 bool hiRes; 00264 bool doLFR; 00265 pthread_t lfhTID; 00266 00267 static bool doForward; 00268 00269 void putEmsg(char *msg, int msz); 00270 int ReBind(int dorename=1); 00271 void Trim(); 00272 }; 00273 #endif