1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*
* Copyright (C) 1999-2011 by CERN/IT/PDP/DM
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Clogit.c$ $Revision$ $Date$ CERN IT-GT/DMS Jean-Philippe Baud";
#endif /* not lint */
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <stdarg.h>
#include "Castor_limits.h"
#include "Cglobals.h"
#include "Clogit.h"
#include "Csnprintf.h"
#include "serrno.h"
#define LOGBUFSZ CA_MAXLINELEN+1
static int jid;
static char logfile[CA_MAXPATHLEN+1];
static int loglevel = -1;
static int syslogflag;
int
Cinitlog (char *cmd, char *logfilename)
{
char *p;
jid = getpid();
if (loglevel < 0) {
if ((p = getenv ("LOG_PRIORITY")) != NULL)
loglevel = atoi (p);
else
loglevel = LOG_INFO;
}
if (! logfilename) {
serrno = EFAULT;
return (-1);
}
if (strcmp (logfilename, "syslog") == 0) {
syslogflag = 1;
openlog (cmd, 0, LOG_USER);
} else if (strlen (logfilename) > CA_MAXPATHLEN) {
serrno = ENAMETOOLONG;
return (-1);
} else
strcpy (logfile, logfilename);
return (0);
}
int
Clogit(int level, char *func, char *msg, ...)
{
va_list args;
va_start (args, msg);
Cvlogit (level, func, msg, args);
va_end (args);
return (0);
}
int
Cvlogit(int level, char *func, char *msg, va_list args)
{
int l;
char prtbuf[LOGBUFSZ];
int save_errno;
int Tid = 0;
struct tm *tm;
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
struct tm tmstruc;
#endif
struct timeval tv;
int fd_log;
if (level > loglevel)
return (0);
save_errno = errno;
if (! syslogflag) {
(void) gettimeofday (&tv, NULL);
#if (defined(_REENTRANT) || defined(_THREAD_SAFE)) && !defined(_WIN32)
(void) localtime_r (&tv.tv_sec, &tmstruc);
tm = &tmstruc;
#else
tm = localtime (&tv.tv_sec);
#endif
Csnprintf (prtbuf, LOGBUFSZ, "%02d/%02d %02d:%02d:%02d.%03d ",
tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
tv.tv_usec / 1000);
l = 19;
} else {
prtbuf[0] = '\0';
l = 0;
}
Cglobals_getTid (&Tid);
if (Tid < 0) { /* main thread */
if (! syslogflag)
Csnprintf (prtbuf + l, LOGBUFSZ - l, "%5d %s: ",
jid, func);
else
Csnprintf (prtbuf + l, LOGBUFSZ - l, "[%d] %s: ",
jid, func);
} else {
if (! syslogflag)
Csnprintf (prtbuf + l, LOGBUFSZ - l, "%5d,%d %s: ",
jid, Tid, func);
else
Csnprintf (prtbuf + l, LOGBUFSZ - l, "[%d,%d] %s: ",
jid, Tid, func);
}
l = strlen (prtbuf);
Cvsnprintf (prtbuf + l, LOGBUFSZ - l, msg, args);
if (prtbuf[LOGBUFSZ-2] != '\n') {
prtbuf[LOGBUFSZ-2] = '\n';
prtbuf[LOGBUFSZ-1] = '\0';
}
if (! syslogflag) {
#ifdef O_LARGEFILE
fd_log = open (logfile, O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE, 0664);
#else
fd_log = open (logfile, O_WRONLY | O_CREAT | O_APPEND, 0664);
#endif
write (fd_log, prtbuf, strlen(prtbuf));
close (fd_log);
} else {
syslog (level, "%s", prtbuf);
}
errno = save_errno;
return (0);
}
|