XRootD
Loading...
Searching...
No Matches
XrdSysLogger Class Reference

#include <XrdSysLogger.hh>

Collaboration diagram for XrdSysLogger:

Classes

class  Task

Public Member Functions

 XrdSysLogger (int ErrFD=STDERR_FILENO, int xrotate=1)
 ~XrdSysLogger ()
 Destructor.
void AddMsg (const char *msg)
void AtMidnight (Task *mnTask)
int Bind (const char *path, int lfh=0)
void Capture (XrdOucTListFIFO *tFIFO)
void Flush ()
 Flush any pending output.
int originalFD ()
int ParseKeep (const char *arg)
void Put (int iovcnt, struct iovec *iov)
void setHiRes ()
 Set log file timstamp to high resolution (hh:mm:ss.uuuu).
void setKeep (long long knum)
void setRotate (int onoff)
char * traceBeg ()
char traceEnd ()
const char * xlogFN ()
void zHandler ()

Static Public Member Functions

static void setForwarding (bool onoff)
 Set call-out to logging plug-in on or off.

Static Public Attributes

static const int onFifo = (int)0x80000000

Detailed Description

Definition at line 52 of file XrdSysLogger.hh.

Constructor & Destructor Documentation

◆ XrdSysLogger()

XrdSysLogger::XrdSysLogger ( int ErrFD = STDERR_FILENO,
int xrotate = 1 )

Constructor

Parameters
ErrFDis the filedescriptor of where error messages normally go if this class is not used. Default is stderr.
xrotatewhen not zero performs internal log rotatation. Otherwise, log rotation is suppressed. See also setRotate().

Definition at line 141 of file XrdSysLogger.cc.

142{
143 char * logFN;
144
145 ePath = 0;
146 eInt = 0;
147 eFD = ErrFD;
148 eKeep = 0;
149 doLFR = (dorotate != 0);
150 msgList = 0;
151 taskQ = 0;
152 lfhTID = 0;
153 hiRes = false;
154 fifoFN = 0;
155 reserved1 = 0;
156
157// Establish default log file name
158//
159 if (!(logFN = getenv("XrdSysLOGFILE"))) logFN = getenv("XrdOucLOGFILE");
160
161// Establish message routing
162//
163 if (ErrFD != STDERR_FILENO) baseFD = ErrFD;
164 else {baseFD = ErrFD;
165 if (logFN)
166 {baseFD = XrdSysFD_Dup(ErrFD);
167 Bind(logFN, 1);
168 }
169 }
170}
int Bind(const char *path, int lfh=0)

References Bind().

Here is the call graph for this function:

◆ ~XrdSysLogger()

XrdSysLogger::~XrdSysLogger ( )
inline

Destructor.

Definition at line 71 of file XrdSysLogger.hh.

72 {
73 RmLogRotateLock();
74 if (ePath)
75 free(ePath);
76 }

Member Function Documentation

◆ AddMsg()

void XrdSysLogger::AddMsg ( const char * msg)

Add a message to be printed at midnight.

Parameters
msgThe message to be printed. A copy of the message is saved.

Definition at line 176 of file XrdSysLogger.cc.

177{
178 mmMsg *tP, *nP = new mmMsg;
179
180// Fill out new message
181//
182 nP->next = 0;
183 nP->msg = strdup(msg);
184 nP->mlen = strlen(msg);
185
186// Add new line character if one is missing (we steal the null byte for this)
187//
188 if (nP->mlen > 1 && nP->msg[nP->mlen-1] != '\n')
189 {nP->msg[nP->mlen] = '\n'; nP->mlen += 1;}
190
191// Add this message to the end of the list
192//
193 Logger_Mutex.Lock();
194 if (!(tP = msgList)) msgList = nP;
195 else {while(tP->next) tP = tP->next;
196 tP->next = nP;
197 }
198 Logger_Mutex.UnLock();
199}

◆ AtMidnight()

void XrdSysLogger::AtMidnight ( XrdSysLogger::Task * mnTask)

Definition at line 205 of file XrdSysLogger.cc.

206{
207
208// Place this task on the task queue
209//
210 Logger_Mutex.Lock();
211 mnTask->next = taskQ;
212 taskQ = mnTask;
213 Logger_Mutex.UnLock();
214}

◆ Bind()

int XrdSysLogger::Bind ( const char * path,
int lfh = 0 )

Definition at line 220 of file XrdSysLogger.cc.

221{
222 XrdSysLoggerRP rtParms(this);
223 int rc;
224
225// Kill logfile handler thread if parameters will be changing
226//
227 if (lfh > 0) lfh = 1;
228 if (lfhTID && (eInt != lfh || !path))
229 {XrdSysThread::Kill(lfhTID);
230 lfhTID = 0;
231 }
232
233// Bind to stderr if no path specified
234//
235 if (ePath) free(ePath);
236 eInt = 0;
237 ePath = 0;
238 if (fifoFN) free(fifoFN);
239 fifoFN = 0; doLFR = false;
240 if (!path) return 0;
241
242// Bind to a log file
243//
244 eInt = lfh;
245 ePath = strdup(path);
246 doLFR = (lfh > 0);
247 if ((rc = ReBind(0))) return rc;
248
249// Lock the logs if XRootD is suppose to handle log rotation itself
250//
251 rc = HandleLogRotateLock( doLFR );
252 if( rc )
253 return -rc;
254
255// Handle specifics of lofile rotation
256//
257 if (eInt == onFifo) {if ((rc = FifoMake())) return -rc;}
258 else if (eInt < 0 && !XrdSysUtils::SigBlock(-eInt))
259 {rc = errno;
260 BLAB("Unable to block logfile signal " <<-eInt <<"; "
261 <<XrdSysE2T(rc));
262 eInt = 0;
263 return -rc;
264 }
265
266// Start a log rotation thread
267//
268 rc = XrdSysThread::Run(&lfhTID, XrdSysLoggerRT, (void *)&rtParms, 0,
269 "Logfile handler");
270 if (!rc) rtParms.active.Wait();
271 return (rc > 0 ? -rc : rc);
272}
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
#define BLAB(x)
void * XrdSysLoggerRT(void *carg)
static const int onFifo
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static int Kill(pthread_t tid)
static bool SigBlock()

References XrdSysLoggerRP::active, BLAB, XrdSysThread::Kill(), onFifo, XrdSysThread::Run(), XrdSysUtils::SigBlock(), XrdSysSemaphore::Wait(), XrdSysE2T(), and XrdSysLoggerRT().

Referenced by XrdSysLogger(), and XrdSysLogging::Configure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Capture()

void XrdSysLogger::Capture ( XrdOucTListFIFO * tFIFO)

Capture allows you to capture all messages (they are not routed). This is a global setting so use with caution!

Parameters
tFIFOPointer to the XrdOucTListFIFO where messages are saved. If the pointer is nil, capturing is turned off.

Definition at line 278 of file XrdSysLogger.cc.

279{
280
281// Obtain the serailization mutex
282//
283 Logger_Mutex.Lock();
284
285// Set the base for capturing messages
286//
287 tFifo = tFIFO;
288
289// Release the serailization mutex
290//
291 Logger_Mutex.UnLock();
292}

◆ Flush()

void XrdSysLogger::Flush ( )
inline

Flush any pending output.

Definition at line 152 of file XrdSysLogger.hh.

152{fsync(eFD);}
#define fsync(a)
Definition XrdPosix.hh:64

References fsync.

◆ originalFD()

int XrdSysLogger::originalFD ( )
inline

Get the file descriptor passed at construction time.

Returns
the file descriptor passed to the constructor.

Definition at line 160 of file XrdSysLogger.hh.

160{return baseFD;}

◆ ParseKeep()

int XrdSysLogger::ParseKeep ( const char * arg)

Parse the keep option argument.

Parameters
argPointer to the argument. The argument syntax is: <count> | <size> | fifo | <signame>
Returns
!0 Parsing succeeded. The return value is the argument that must be passed as the lfh parameter to Bind().
=0 Invalid keep argument.

Definition at line 298 of file XrdSysLogger.cc.

299{
300 char *eP;
301
302// First check to see if this is a sig type
303//
304 eKeep = 0;
305 if (isalpha(*arg))
306 {if (!strcmp(arg, "fifo")) return onFifo;
307 return -XrdSysUtils::GetSigNum(arg);
308 }
309
310// Process an actual keep count
311//
312 eKeep = strtoll(arg, &eP, 10);
313 if (!(*eP) || eKeep < 0) {eKeep = -eKeep; return 1;}
314
315// Process an actual keep size
316//
317 if (*(eP+1)) return 0;
318 if (*eP == 'k' || *eP == 'K') eKeep *= 1024LL;
319 else if (*eP == 'm' || *eP == 'M') eKeep *= 1024LL*1024LL;
320 else if (*eP == 'g' || *eP == 'G') eKeep *= 1024LL*1024LL*1024LL;
321 else if (*eP == 't' || *eP == 'T') eKeep *= 1024LL*1024LL*1024LL*1024LL;
322 else return 0;
323
324// All done
325//
326 return 1;
327}
static int GetSigNum(const char *sname)

References XrdSysUtils::GetSigNum(), and onFifo.

Here is the call graph for this function:

◆ Put()

void XrdSysLogger::Put ( int iovcnt,
struct iovec * iov )

Output data and optionally prefix with date/time

Parameters
iovcntThe number of elements in iov vector.
iovThe vector describing what to print. If iov[0].iov_base is zero, the message is prefixed by date and time.

Definition at line 333 of file XrdSysLogger.cc.

334{
335 struct timeval tVal;
336 unsigned long tID = XrdSysThread::Num();
337 int retc;
338 char tbuff[32];
339
340// Get current time
341//
342 gettimeofday(&tVal, 0);
343
344// Forward the message if there is a plugin involved here
345//
346 if (doForward)
347 {bool xEnd;
348 if (iov[0].iov_base) xEnd = XrdSysLogging::Forward(tVal,tID,iov,iovcnt);
349 else xEnd = XrdSysLogging::Forward(tVal, tID, &iov[1], iovcnt-1);
350 if (xEnd) return;
351 }
352
353// Prefix message with time if calle wants it so
354//
355 if (!iov[0].iov_base)
356 {iov[0].iov_base = tbuff;
357 iov[0].iov_len = TimeStamp(tVal, tID, tbuff, sizeof(tbuff), hiRes);
358 }
359
360// Obtain the serailization mutex if need be
361//
362 Logger_Mutex.Lock();
363
364// If we are capturing messages, do so now
365//
366 if (tFifo)
367 {Snatch(iov, iovcnt);
368 Logger_Mutex.UnLock();
369 return;
370 }
371
372// In theory, writev may write out a partial list. This rarely happens in
373// practice and so we ignore that possibility (recovery is pretty tough).
374//
375 do { retc = writev(eFD, (const struct iovec *)iov, iovcnt);}
376 while (retc < 0 && errno == EINTR);
377
378// Release the serailization mutex if need be
379//
380 Logger_Mutex.UnLock();
381}
#define writev(a, b, c)
Definition XrdPosix.hh:117
static bool Forward(struct timeval mtime, unsigned long tID, struct iovec *iov, int iovcnt)
static unsigned long Num(void)

References XrdSysLogging::Forward(), XrdSysThread::Num(), and writev.

Referenced by XrdSysTrace::operator<<().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setForwarding()

void XrdSysLogger::setForwarding ( bool onoff)
inlinestatic

Set call-out to logging plug-in on or off.

Definition at line 190 of file XrdSysLogger.hh.

190{doForward = onoff;}

Referenced by XrdSysLogging::Configure().

Here is the caller graph for this function:

◆ setHiRes()

void XrdSysLogger::setHiRes ( )
inline

Set log file timstamp to high resolution (hh:mm:ss.uuuu).

Definition at line 196 of file XrdSysLogger.hh.

196{hiRes = true;}

Referenced by XrdSysLogging::Configure().

Here is the caller graph for this function:

◆ setKeep()

void XrdSysLogger::setKeep ( long long knum)
inline

Set log file keep value.

Parameters
knumThe keep value. If knum < 0 then abs(knum) files are kept. Otherwise, only knum bytes of log files are kept.

Definition at line 205 of file XrdSysLogger.hh.

205{eKeep = knum;}

◆ setRotate()

void XrdSysLogger::setRotate ( int onoff)
inline

Set log file rotation on/off.

Parameters
onoffWhen !0 turns on log file rotations. Otherwise, rotation is turned off.

Definition at line 214 of file XrdSysLogger.hh.

214{doLFR = onoff;}

◆ traceBeg()

char * XrdSysLogger::traceBeg ( )
inline

Start trace message serialization. This method must be followed by a call to traceEnd().

Returns
pointer to the time buffer to be used as the msg timestamp.

Definition at line 223 of file XrdSysLogger.hh.

223{Logger_Mutex.Lock(); Time(TBuff); return TBuff;}

◆ traceEnd()

char XrdSysLogger::traceEnd ( )
inline

Stop trace message serialization. This method must be preceeded by a call to traceBeg().

Returns
pointer to a new line character to terminate the message.

Definition at line 232 of file XrdSysLogger.hh.

232{Logger_Mutex.UnLock(); return '\n';}

◆ xlogFN()

const char * XrdSysLogger::xlogFN ( )
inline

Get the log file routing.

Returns
the filename of the log file or "stderr".

Definition at line 240 of file XrdSysLogger.hh.

240{return (ePath ? ePath : "stderr");}

◆ zHandler()

void XrdSysLogger::zHandler ( )

Internal method to handle the logfile. This is public because it needs to be called by an external thread.

Definition at line 809 of file XrdSysLogger.cc.

810{
811 mmMsg *mP;
812 sigset_t sigset;
813 pthread_t tid;
814 int signo, rc;
815 Task *tP;
816
817// If we will be handling via signals, set it up now
818//
819 if (eInt < 0 && !fifoFN)
820 {signo = -eInt;
821 if ((sigemptyset(&sigset) == -1)
822 || (sigaddset(&sigset,signo) == -1))
823 {rc = errno;
824 BLAB("Unable to use logfile signal " <<signo <<"; " <<XrdSysE2T(rc));
825 eInt = 0;
826 }
827 }
828
829// This is a perpetual loop to handle the log file
830//
831 while(1)
832 { if (fifoFN) FifoWait();
833 else if (eInt >= 0) XrdSysTimer::Wait4Midnight();
834 else if ((sigwait(&sigset, &signo) == -1))
835 {rc = errno;
836 BLAB("Unable to wait on logfile signal " <<signo
837 <<"; " <<XrdSysE2T(rc));
838 eInt = 0;
839 continue;
840 }
841
842 Logger_Mutex.Lock();
843 ReBind();
844
845 mP = msgList;
846 while(mP)
847 {putEmsg(mP->msg, mP->mlen);
848 mP = mP->next;
849 }
850 tP = taskQ;
851 Logger_Mutex.UnLock();
852
853 if (tP)
854 {if (XrdSysThread::Run(&tid, XrdSysLoggerMN, (void *)tP, 0,
855 "Midnight Ringer Task"))
856 {char eBuff[256];
857 rc = sprintf(eBuff, "Error %d (%s) running ringer task.\n",
858 errno, XrdSysE2T(errno));
859 putEmsg(eBuff, rc);
860 }
861 }
862 }
863}
void * XrdSysLoggerMN(void *carg)
static void Wait4Midnight()

References BLAB, XrdSysThread::Run(), XrdSysTimer::Wait4Midnight(), XrdSysE2T(), and XrdSysLoggerMN().

Referenced by XrdSysLoggerRT().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ onFifo

const int XrdSysLogger::onFifo = (int)0x80000000
static

Bind allows you to bind the file descriptor passed at construction time to a file with an optional periodic closing and opening of the file.

Parameters
pathThe log file path. The file is created, if need be. If path is null, messages are routed to stderr and the lfh argument is ignored.
lfhLog file handling: >0 file is to be closed and opened at midnight. This implies automatic log rotation. =0 file is to be left open all the time. This implies no log rotation. <0 file is to be closed and opened only on signal abs(lfh) unless the value equals onFifo. In this case a fifo is used to control log file rotation. The name of the fifo is path with the filename component prefixed by dot. This implies manual log rotation. Warning! Using signals requires that Bind() be called before starting any threads so that the signal is properly blocked.
Returns
0 Processing successful.
<0 Unable to bind, returned value is -errno of the reason.

Definition at line 134 of file XrdSysLogger.hh.

Referenced by Bind(), and ParseKeep().


The documentation for this class was generated from the following files: