1 /* $Id: log.c,v 1.18 2002-04-25 18:56:43 rjkaes Exp $
3 * Logs the various messages which tinyproxy produces to either a log file or
4 * the syslog daemon. Not much to it...
6 * Copyright (C) 1998 Steven Young
7 * Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
20 #include "tinyproxy.h"
25 static char *syslog_level[] = {
37 #define TIME_LENGTH 16
38 #define STRING_LENGTH 800
41 * Store the log level setting.
43 static int log_level = LOG_INFO;
46 * Hold a listing of log messages which need to be sent once the log
47 * file has been established.
48 * The key is the actual messages (already filled in full), and the value
51 static hashmap_t log_message_storage;
54 * Set the log level for writing to the log file.
57 set_log_level(int level)
63 * This routine logs messages to either the log file or the syslog function.
66 log_message(int level, char *fmt, ...)
72 char time_string[TIME_LENGTH];
73 #if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
74 char str[STRING_LENGTH];
79 * Figure out if we should write the message or not.
81 if (log_level == LOG_CONN) {
82 if (level == LOG_INFO)
84 } else if (log_level == LOG_INFO) {
85 if (level > LOG_INFO && level != LOG_CONN)
87 } else if (level > log_level)
92 if (config.syslog && level == LOG_CONN)
99 * If the config file hasn't been processed, then we need to store
100 * the messages for later processing.
102 if (!processed_config_file) {
103 char string_level[4];
105 if (!log_message_storage) {
106 log_message_storage = hashmap_create(1);
107 if (!log_message_storage)
111 vsnprintf(str, STRING_LENGTH, fmt, args);
112 snprintf(string_level, 4, "%d", level);
114 hashmap_insert(log_message_storage, str,
115 string_level, strlen(string_level) + 1);
124 # ifdef HAVE_VSYSLOG_H
125 vsyslog(level, fmt, args);
127 vsnprintf(str, STRING_LENGTH, fmt, args);
128 syslog(level, "%s", str);
132 nowtime = time(NULL);
133 /* Format is month day hour:minute:second (24 time) */
134 strftime(time_string, TIME_LENGTH, "%b %d %H:%M:%S",
135 localtime(&nowtime));
137 if (!(cf = config.logf))
140 fprintf(cf, "%-9s %s [%ld]: ", syslog_level[level],
141 time_string, (long int) getpid());
142 vfprintf(cf, fmt, args);
153 * This needs to send any stored log messages.
156 send_stored_logs(void)
162 for (iter = hashmap_first(log_message_storage);
163 iter != hashmap_is_end(log_message_storage, iter);
165 hashmap_return_entry(log_message_storage,
171 hashmap_delete(log_message_storage);
172 log_message_storage = NULL;