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() {if (ePath) free(ePath);} 00070 00071 //----------------------------------------------------------------------------- 00075 //----------------------------------------------------------------------------- 00076 00077 void AddMsg(const char *msg); 00078 00079 //----------------------------------------------------------------------------- 00083 //----------------------------------------------------------------------------- 00084 00085 class Task 00086 { 00087 public: 00088 friend class XrdSysLogger; 00089 00090 virtual void Ring() = 0; 00091 00092 inline Task *Next() {return next;} 00093 00094 Task() : next(0) {} 00095 virtual ~Task() {} 00096 00097 private: 00098 Task *next; 00099 }; 00100 00101 void AtMidnight(Task *mnTask); 00102 00103 //----------------------------------------------------------------------------- 00125 //----------------------------------------------------------------------------- 00126 00127 static const int onFifo = (int)0x80000000; 00128 00129 int Bind(const char *path, int lfh=0); 00130 00131 //----------------------------------------------------------------------------- 00133 //----------------------------------------------------------------------------- 00134 00135 void Flush() {fsync(eFD);} 00136 00137 //----------------------------------------------------------------------------- 00141 //----------------------------------------------------------------------------- 00142 00143 int originalFD() {return baseFD;} 00144 00145 //----------------------------------------------------------------------------- 00154 //----------------------------------------------------------------------------- 00155 00156 int ParseKeep(const char *arg); 00157 00158 //----------------------------------------------------------------------------- 00164 //----------------------------------------------------------------------------- 00165 00166 void Put(int iovcnt, struct iovec *iov); 00167 00168 //----------------------------------------------------------------------------- 00170 //----------------------------------------------------------------------------- 00171 00172 void setHiRes() {hiRes = true;} 00173 00174 //----------------------------------------------------------------------------- 00179 //----------------------------------------------------------------------------- 00180 00181 void setKeep(long long knum) {eKeep = knum;} 00182 00183 //----------------------------------------------------------------------------- 00188 //----------------------------------------------------------------------------- 00189 00190 void setRotate(int onoff) {doLFR = onoff;} 00191 00192 //----------------------------------------------------------------------------- 00197 //----------------------------------------------------------------------------- 00198 00199 char *traceBeg() {Logger_Mutex.Lock(); Time(TBuff); return TBuff;} 00200 00201 //----------------------------------------------------------------------------- 00206 //----------------------------------------------------------------------------- 00207 00208 char traceEnd() {Logger_Mutex.UnLock(); return '\n';} 00209 00210 //----------------------------------------------------------------------------- 00214 //----------------------------------------------------------------------------- 00215 00216 const char *xlogFN() {return (ePath ? ePath : "stderr");} 00217 00218 //----------------------------------------------------------------------------- 00221 //----------------------------------------------------------------------------- 00222 00223 void zHandler(); 00224 00225 private: 00226 int FifoMake(); 00227 void FifoWait(); 00228 int Time(char *tbuff); 00229 00230 struct mmMsg 00231 {mmMsg *next; 00232 int mlen; 00233 char *msg; 00234 }; 00235 mmMsg *msgList; 00236 Task *taskQ; 00237 XrdSysMutex Logger_Mutex; 00238 long long eKeep; 00239 char TBuff[32]; // Trace header buffer 00240 int eFD; 00241 int baseFD; 00242 char *ePath; 00243 char Filesfx[8]; 00244 int eInt; 00245 int reserved1; 00246 char *fifoFN; 00247 bool hiRes; 00248 bool doLFR; 00249 pthread_t lfhTID; 00250 00251 void putEmsg(char *msg, int msz); 00252 int ReBind(int dorename=1); 00253 void Trim(); 00254 }; 00255 #endif