#ifndef XLOG_H
#define XLOG_H
+/* These are logged always. L_FATAL also does exit(1) */
#define L_FATAL 0x0100
#define L_ERROR 0x0200
#define L_WARNING 0x0400
#define L_NOTICE 0x0800
#define L_ALL 0xFF00
+/* These are logged if enabled with xlog_[s]config */
+/* NB: code does not expect ORing together D_ and L_ */
#define D_GENERAL 0x0001 /* general debug info */
#define D_CALL 0x0002
#define D_AUTH 0x0004
};
void xlog_open(char *progname);
-void xlog_background(void);
+void xlog_stderr(int on);
+void xlog_syslog(int on);
void xlog_config(int fac, int on);
void xlog_sconfig(char *, int on);
int xlog_enabled(int fac);
#undef VERBOSE_PRINTF
-static int foreground = 1; /* not a daemon initially */
+static int log_stderr = 1;
+static int log_syslog = 1;
static int logging = 0; /* enable/disable DEBUG logs */
static int logmask = 0; /* What will be logged */
static char log_name[256]; /* name of this program */
static int log_pid = -1; /* PID of this program */
-static FILE *log_fp = (FILE *)NULL; /* fp for the log file */
static void xlog_toggle(int sig);
static struct xlog_debugfac debugnames[] = {
xlog_open(char *progname)
{
openlog(progname, LOG_PID, LOG_DAEMON);
- if (foreground) {
- log_fp = stderr;
- if (log_fp != NULL)
- setbuf(log_fp, NULL);
- }
strncpy(log_name, progname, sizeof (log_name) - 1);
log_name [sizeof (log_name) - 1] = '\0';
}
void
-xlog_background(void)
+xlog_stderr(int on)
+{
+ log_stderr = on;
+}
+
+void
+xlog_syslog(int on)
{
- foreground = 0;
+ log_syslog = on;
}
static void
}
-/* Write something to the system logfile. */
+/* Write something to the system logfile and/or stderr */
void
xlog(int kind, const char *fmt, ...)
{
char buff[1024];
va_list args;
- int logged = 1, n;
-#ifdef VERBOSE_PRINTF
- time_t now;
- struct tm *tm;
-#endif
+ int n;
if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
return;
if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
buff[--n] = '\0';
- switch (kind) {
- case L_FATAL:
- syslog(LOG_ERR, "%s", buff);
- break;
- case L_ERROR:
- syslog(LOG_ERR, "%s", buff);
- break;
- case L_WARNING:
- syslog(LOG_WARNING, "%s", buff);
- break;
- case L_NOTICE:
- syslog(LOG_NOTICE, "%s", buff);
- break;
- default:
- logged = 0;
- break;
+ if (log_syslog) {
+ switch (kind) {
+ case L_FATAL:
+ syslog(LOG_ERR, "%s", buff);
+ break;
+ case L_ERROR:
+ syslog(LOG_ERR, "%s", buff);
+ break;
+ case L_WARNING:
+ syslog(LOG_WARNING, "%s", buff);
+ break;
+ case L_NOTICE:
+ syslog(LOG_NOTICE, "%s", buff);
+ break;
+ default:
+ if (!log_stderr)
+ syslog(LOG_DEBUG, "%s", buff);
+ break;
+ }
}
- if (!logged || foreground) {
- if (!logged && log_fp == NULL) {
- syslog(LOG_DEBUG, "%s", buff);
- } else if (log_fp != NULL) {
+
+ if (log_stderr) {
#ifdef VERBOSE_PRINTF
- time(&now);
- tm = localtime(&now);
- fprintf(log_fp, "%s[%d] %02d/%02d/%02d %02d:%02d %s\n",
- log_name, log_pid,
- tm->tm_mon + 1, tm->tm_mday,
- tm->tm_year, tm->tm_hour, tm->tm_min,
- buff);
+ time_t now;
+ struct tm *tm;
+
+ time(&now);
+ tm = localtime(&now);
+ fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
+ log_name, log_pid,
+ tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec,
+ buff);
#else
- fprintf(log_fp, "%s: %s\n", log_name, buff);
+ fprintf(stderr, "%s: %s\n", log_name, buff);
#endif
- }
}
+
if (kind == L_FATAL)
exit(1);
}