2001-10-26 01:27:39 +08:00
|
|
|
/* $Id: log.c,v 1.15 2001-10-25 17:27:39 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* Logs the various messages which tinyproxy produces to either a log file or
|
|
|
|
* the syslog daemon. Not much to it...
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Steven Young
|
|
|
|
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2000-09-12 07:47:52 +08:00
|
|
|
#include "tinyproxy.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
2000-09-12 07:47:52 +08:00
|
|
|
static char *syslog_level[] = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"CRITICAL",
|
|
|
|
"ERROR",
|
|
|
|
"WARNING",
|
2000-09-22 00:53:51 +08:00
|
|
|
"NOTICE",
|
2000-09-12 07:47:52 +08:00
|
|
|
"INFO",
|
2001-08-27 05:10:04 +08:00
|
|
|
"DEBUG",
|
|
|
|
"CONNECT"
|
2000-09-12 07:47:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TIME_LENGTH 16
|
|
|
|
#define STRING_LENGTH 800
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-06-02 11:09:27 +08:00
|
|
|
/*
|
|
|
|
* Store the log level setting.
|
|
|
|
*/
|
|
|
|
static short int log_level = LOG_ERR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the log level for writing to the log file.
|
|
|
|
*/
|
|
|
|
void set_log_level(short int level)
|
|
|
|
{
|
|
|
|
log_level = level;
|
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/*
|
|
|
|
* This routine logs messages to either the log file or the syslog function.
|
|
|
|
*/
|
2001-05-27 10:26:11 +08:00
|
|
|
void log_message(short int level, char *fmt, ...)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
time_t nowtime;
|
|
|
|
FILE *cf;
|
|
|
|
|
2000-09-12 07:47:52 +08:00
|
|
|
char time_string[TIME_LENGTH];
|
2000-02-17 01:32:49 +08:00
|
|
|
#if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
|
2000-09-12 07:47:52 +08:00
|
|
|
char str[STRING_LENGTH];
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif
|
|
|
|
|
2001-08-29 11:59:12 +08:00
|
|
|
#ifdef NDEBUG
|
2001-06-02 11:09:27 +08:00
|
|
|
/*
|
|
|
|
* Figure out if we should write the message or not.
|
|
|
|
*/
|
2001-08-28 23:51:58 +08:00
|
|
|
if (log_level == LOG_CONN) {
|
|
|
|
if (level == LOG_INFO)
|
|
|
|
return;
|
|
|
|
} else if (log_level == LOG_INFO) {
|
|
|
|
if (level > LOG_INFO && level != LOG_CONN)
|
|
|
|
return;
|
|
|
|
} else if (level > log_level)
|
2001-08-29 11:59:12 +08:00
|
|
|
return;
|
|
|
|
#endif
|
2001-08-28 23:51:58 +08:00
|
|
|
|
|
|
|
|
2001-08-27 05:10:04 +08:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
if (config.syslog && level == LOG_CONN)
|
|
|
|
level = LOG_INFO;
|
|
|
|
#endif
|
2001-06-02 11:09:27 +08:00
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
2000-09-12 07:47:52 +08:00
|
|
|
if (config.syslog) {
|
|
|
|
# ifdef HAVE_VSYSLOG_H
|
|
|
|
vsyslog(level, fmt, args);
|
|
|
|
# else
|
|
|
|
vsnprintf(str, STRING_LENGTH, fmt, args);
|
2001-09-05 00:50:22 +08:00
|
|
|
syslog(level, "%s", str);
|
2000-09-12 07:47:52 +08:00
|
|
|
# endif
|
|
|
|
} else {
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif
|
|
|
|
nowtime = time(NULL);
|
|
|
|
/* Format is month day hour:minute:second (24 time) */
|
2000-09-12 07:47:52 +08:00
|
|
|
strftime(time_string, TIME_LENGTH, "%b %d %H:%M:%S",
|
2000-02-17 01:32:49 +08:00
|
|
|
localtime(&nowtime));
|
|
|
|
|
|
|
|
if (!(cf = config.logf))
|
|
|
|
cf = stderr;
|
|
|
|
|
2001-06-07 03:32:51 +08:00
|
|
|
fprintf(cf, "%-9s %s [%ld]: ", syslog_level[level],
|
|
|
|
time_string, (long int)getpid());
|
2000-02-17 01:32:49 +08:00
|
|
|
vfprintf(cf, fmt, args);
|
|
|
|
fprintf(cf, "\n");
|
|
|
|
fflush(cf);
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|