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 = XrdSysFD_Dup(ErrFD);
165 Bind(logFN, 1);
166 }
167}
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 173 of file XrdSysLogger.cc.

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

◆ AtMidnight()

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

Definition at line 202 of file XrdSysLogger.cc.

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

◆ Bind()

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

Definition at line 217 of file XrdSysLogger.cc.

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

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

◆ 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 295 of file XrdSysLogger.cc.

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

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

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