tinyproxy/src/log.c

174 lines
3.6 KiB
C
Raw Normal View History

/* $Id: log.c,v 1.17 2002-04-22 19:34:19 rjkaes Exp $
*
* 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.
*/
#include "tinyproxy.h"
#include "hashmap.h"
#include "log.h"
static char *syslog_level[] = {
NULL,
NULL,
"CRITICAL",
"ERROR",
"WARNING",
2000-09-22 00:53:51 +08:00
"NOTICE",
"INFO",
2001-08-27 05:10:04 +08:00
"DEBUG",
"CONNECT"
};
#define TIME_LENGTH 16
#define STRING_LENGTH 800
/*
* Store the log level setting.
*/
static int log_level = LOG_INFO;
/*
* Hold a listing of log messages which need to be sent once the log
* file has been established.
* The key is the actual messages (already filled in full), and the value
* is the log level.
*/
static hashmap_t log_message_storage;
/*
* Set the log level for writing to the log file.
*/
2001-11-22 08:31:10 +08:00
void
set_log_level(int level)
{
log_level = level;
}
/*
* This routine logs messages to either the log file or the syslog function.
*/
2001-11-22 08:31:10 +08:00
void
log_message(int level, char *fmt, ...)
{
va_list args;
time_t nowtime;
FILE *cf;
char time_string[TIME_LENGTH];
#if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
char str[STRING_LENGTH];
#endif
#ifdef NDEBUG
/*
* Figure out if we should write the message or not.
*/
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-11-22 08:31:10 +08:00
return;
#endif
2001-08-27 05:10:04 +08:00
#ifdef HAVE_SYSLOG_H
2001-11-22 08:31:10 +08:00
if (config.syslog && level == LOG_CONN)
2001-08-27 05:10:04 +08:00
level = LOG_INFO;
#endif
va_start(args, fmt);
/*
* If the config file hasn't been processed, then we need to store
* the messages for later processing.
*/
if (!processed_config_file) {
char string_level[4];
if (!log_message_storage) {
log_message_storage = hashmap_create(1);
if (!log_message_storage)
return;
}
vsnprintf(str, STRING_LENGTH, fmt, args);
snprintf(string_level, 4, "%d", level);
hashmap_insert(log_message_storage, str,
string_level, strlen(string_level) + 1);
va_end(args);
return;
}
#ifdef HAVE_SYSLOG_H
if (config.syslog) {
# ifdef HAVE_VSYSLOG_H
vsyslog(level, fmt, args);
# else
vsnprintf(str, STRING_LENGTH, fmt, args);
syslog(level, "%s", str);
# endif
} else {
#endif
nowtime = time(NULL);
/* Format is month day hour:minute:second (24 time) */
strftime(time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime(&nowtime));
if (!(cf = config.logf))
cf = stderr;
fprintf(cf, "%-9s %s [%ld]: ", syslog_level[level],
2001-11-22 08:31:10 +08:00
time_string, (long int) getpid());
vfprintf(cf, fmt, args);
fprintf(cf, "\n");
fflush(cf);
#ifdef HAVE_SYSLOG_H
}
#endif
va_end(args);
}
/*
* This needs to send any stored log messages.
*/
void
send_stored_logs(void)
{
vector_t messages;
char *level;
char *string;
int i;
messages = hashmap_keys(log_message_storage);
for (i = 0; i < vector_length(messages); i++) {
vector_getentry(messages, i, (void **)&string);
hashmap_search(log_message_storage, string, (void **)&level);
log_message(atoi(level), string);
}
vector_delete(messages);
hashmap_delete(log_message_storage);
log_message_storage = NULL;
}