|
||||
File indexing completed on 2025-01-18 10:15:42
0001 #ifndef __SYS_LOGGER_H__ 0002 #define __SYS_LOGGER_H__ 0003 /******************************************************************************/ 0004 /* */ 0005 /* X r d S y s L o g g e r . h h */ 0006 /* */ 0007 /*(c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 0008 /*Produced by Andrew Hanushevsky for Stanford University under contract */ 0009 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */ 0010 /* */ 0011 /* This file is part of the XRootD software suite. */ 0012 /* */ 0013 /* XRootD is free software: you can redistribute it and/or modify it under */ 0014 /* the terms of the GNU Lesser General Public License as published by the */ 0015 /* Free Software Foundation, either version 3 of the License, or (at your */ 0016 /* option) any later version. */ 0017 /* */ 0018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 0019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 0020 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 0021 /* License for more details. */ 0022 /* */ 0023 /* You should have received a copy of the GNU Lesser General Public License */ 0024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 0025 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 0026 /* */ 0027 /* The copyright holder's institutional names and contributor's names may not */ 0028 /* be used to endorse or promote products derived from this software without */ 0029 /* specific prior written permission of the institution or contributor. */ 0030 /******************************************************************************/ 0031 0032 #include <cstdlib> 0033 #ifndef WIN32 0034 #include <unistd.h> 0035 #include <cstring> 0036 #include <strings.h> 0037 #else 0038 #include <cstring> 0039 #include <io.h> 0040 #include "XrdSys/XrdWin32.hh" 0041 #endif 0042 0043 #include "XrdSys/XrdSysPthread.hh" 0044 0045 //----------------------------------------------------------------------------- 0046 //! XrdSysLogger is the object that is used to route messages to wherever they 0047 //! need to go and also handles log file rotation and trimming. 0048 //----------------------------------------------------------------------------- 0049 0050 class XrdOucTListFIFO; 0051 0052 class XrdSysLogger 0053 { 0054 public: 0055 0056 //----------------------------------------------------------------------------- 0057 //! Constructor 0058 //! 0059 //! @param ErrFD is the filedescriptor of where error messages normally 0060 //! go if this class is not used. Default is stderr. 0061 //! @param xrotate when not zero performs internal log rotatation. Otherwise, 0062 //! log rotation is suppressed. See also setRotate(). 0063 //----------------------------------------------------------------------------- 0064 0065 XrdSysLogger(int ErrFD=STDERR_FILENO, int xrotate=1); 0066 0067 //----------------------------------------------------------------------------- 0068 //! Destructor 0069 //----------------------------------------------------------------------------- 0070 0071 ~XrdSysLogger() 0072 { 0073 RmLogRotateLock(); 0074 if (ePath) 0075 free(ePath); 0076 } 0077 0078 //----------------------------------------------------------------------------- 0079 //! Add a message to be printed at midnight. 0080 //! 0081 //! @param msg The message to be printed. A copy of the message is saved. 0082 //----------------------------------------------------------------------------- 0083 0084 void AddMsg(const char *msg); 0085 0086 //----------------------------------------------------------------------------- 0087 //! Add a task to be run at midnight. Tasks are run sequentially lifo. 0088 //! 0089 //! @param mnTask Pointer to an instance of the task object below. 0090 //----------------------------------------------------------------------------- 0091 0092 class Task 0093 { 0094 public: 0095 friend class XrdSysLogger; 0096 0097 virtual void Ring() = 0; //!< This method gets called at midnight 0098 0099 inline Task *Next() {return next;} 0100 0101 Task() : next(0) {} 0102 virtual ~Task() {} 0103 0104 private: 0105 Task *next; 0106 }; 0107 0108 void AtMidnight(Task *mnTask); 0109 0110 //----------------------------------------------------------------------------- 0111 //! Bind allows you to bind the file descriptor passed at construction time to 0112 //! a file with an optional periodic closing and opening of the file. 0113 //! 0114 //! @param path The log file path. The file is created, if need be. 0115 //! If path is null, messages are routed to stderr and the 0116 //! lfh argument is ignored. 0117 //! @param lfh Log file handling: 0118 //! >0 file is to be closed and opened at midnight. 0119 //! This implies automatic log rotation. 0120 //! =0 file is to be left open all the time. 0121 //! This implies no log rotation. 0122 //! <0 file is to be closed and opened only on signal abs(lfh) 0123 //! unless the value equals onFifo. In this case a fifo is 0124 //! used to control log file rotation. The name of the fifo 0125 //! is path with the filename component prefixed by dot. 0126 //! This implies manual log rotation. Warning! Using 0127 //! signals requires that Bind() be called before starting 0128 //! any threads so that the signal is properly blocked. 0129 //! 0130 //! @return 0 Processing successful. 0131 //! @return <0 Unable to bind, returned value is -errno of the reason. 0132 //----------------------------------------------------------------------------- 0133 0134 static const int onFifo = (int)0x80000000; 0135 0136 int Bind(const char *path, int lfh=0); 0137 0138 //----------------------------------------------------------------------------- 0139 //! Capture allows you to capture all messages (they are not routed). This is 0140 //! a global setting so use with caution! 0141 //! 0142 //! @param tFIFO Pointer to the XrdOucTListFIFO where messages are saved. 0143 //! If the pointer is nil, capturing is turned off. 0144 //----------------------------------------------------------------------------- 0145 0146 void Capture(XrdOucTListFIFO *tFIFO); 0147 0148 //----------------------------------------------------------------------------- 0149 //! Flush any pending output 0150 //----------------------------------------------------------------------------- 0151 0152 void Flush() {fsync(eFD);} 0153 0154 //----------------------------------------------------------------------------- 0155 //! Get the file descriptor passed at construction time. 0156 //! 0157 //! @return the file descriptor passed to the constructor. 0158 //----------------------------------------------------------------------------- 0159 0160 int originalFD() {return baseFD;} 0161 0162 //----------------------------------------------------------------------------- 0163 //! Parse the keep option argument. 0164 //! 0165 //! @param arg Pointer to the argument. The argument syntax is: 0166 //! \<count\> | \<size\> | fifo | \<signame\> 0167 //! 0168 //! @return !0 Parsing succeeded. The return value is the argument that 0169 //! must be passed as the lfh parameter to Bind(). 0170 //! @return =0 Invalid keep argument. 0171 //----------------------------------------------------------------------------- 0172 0173 int ParseKeep(const char *arg); 0174 0175 //----------------------------------------------------------------------------- 0176 //! Output data and optionally prefix with date/time 0177 //! 0178 //! @param iovcnt The number of elements in iov vector. 0179 //! @param iov The vector describing what to print. If iov[0].iov_base 0180 //! is zero, the message is prefixed by date and time. 0181 //----------------------------------------------------------------------------- 0182 0183 void Put(int iovcnt, struct iovec *iov); 0184 0185 //----------------------------------------------------------------------------- 0186 //! Set call-out to logging plug-in on or off. 0187 //----------------------------------------------------------------------------- 0188 0189 static 0190 void setForwarding(bool onoff) {doForward = onoff;} 0191 0192 //----------------------------------------------------------------------------- 0193 //! Set log file timstamp to high resolution (hh:mm:ss.uuuu). 0194 //----------------------------------------------------------------------------- 0195 0196 void setHiRes() {hiRes = true;} 0197 0198 //----------------------------------------------------------------------------- 0199 //! Set log file keep value. 0200 //! 0201 //! @param knum The keep value. If knum < 0 then abs(knum) files are kept. 0202 //! Otherwise, only knum bytes of log files are kept. 0203 //----------------------------------------------------------------------------- 0204 0205 void setKeep(long long knum) {eKeep = knum;} 0206 0207 //----------------------------------------------------------------------------- 0208 //! Set log file rotation on/off. 0209 //! 0210 //! @param onoff When !0 turns on log file rotations. Otherwise, rotation 0211 //! is turned off. 0212 //----------------------------------------------------------------------------- 0213 0214 void setRotate(int onoff) {doLFR = onoff;} 0215 0216 //----------------------------------------------------------------------------- 0217 //! Start trace message serialization. This method must be followed by a call 0218 //! to traceEnd(). 0219 //! 0220 //! @return pointer to the time buffer to be used as the msg timestamp. 0221 //----------------------------------------------------------------------------- 0222 0223 char *traceBeg() {Logger_Mutex.Lock(); Time(TBuff); return TBuff;} 0224 0225 //----------------------------------------------------------------------------- 0226 //! Stop trace message serialization. This method must be preceeded by a call 0227 //! to traceBeg(). 0228 //! 0229 //! @return pointer to a new line character to terminate the message. 0230 //----------------------------------------------------------------------------- 0231 0232 char traceEnd() {Logger_Mutex.UnLock(); return '\n';} 0233 0234 //----------------------------------------------------------------------------- 0235 //! Get the log file routing. 0236 //! 0237 //! @return the filename of the log file or "stderr". 0238 //----------------------------------------------------------------------------- 0239 0240 const char *xlogFN() {return (ePath ? ePath : "stderr");} 0241 0242 //----------------------------------------------------------------------------- 0243 //! Internal method to handle the logfile. This is public because it needs to 0244 //! be called by an external thread. 0245 //----------------------------------------------------------------------------- 0246 0247 void zHandler(); 0248 0249 private: 0250 int FifoMake(); 0251 void FifoWait(); 0252 int Time(char *tbuff); 0253 static int TimeStamp(struct timeval &tVal, unsigned long tID, 0254 char *tbuff, int tbsz, bool hires); 0255 int HandleLogRotateLock( bool dorotate ); 0256 void RmLogRotateLock(); 0257 0258 struct mmMsg 0259 {mmMsg *next; 0260 int mlen; 0261 char *msg; 0262 }; 0263 mmMsg *msgList; 0264 Task *taskQ; 0265 XrdSysMutex Logger_Mutex; 0266 long long eKeep; 0267 char TBuff[32]; // Trace header buffer 0268 int eFD; 0269 int baseFD; 0270 char *ePath; 0271 char Filesfx[8]; 0272 int eInt; 0273 int reserved1; 0274 char *fifoFN; 0275 bool hiRes; 0276 bool doLFR; 0277 pthread_t lfhTID; 0278 0279 static bool doForward; 0280 0281 void putEmsg(char *msg, int msz); 0282 int ReBind(int dorename=1); 0283 void Trim(); 0284 }; 0285 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |