2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 1998 Steven Young <sdyoung@miranda.org>
|
|
|
|
* Copyright (C) 1999 Robert James Kaes <rjkaes@users.sourceforge.net>
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* 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-02-17 01:32:49 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Logs the various messages which tinyproxy produces to either a log file
|
|
|
|
* or the syslog daemon. Not much to it...
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
|
|
|
|
2000-09-12 07:47:52 +08:00
|
|
|
#include "tinyproxy.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2002-05-24 02:20:27 +08:00
|
|
|
#include "heap.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
#include "log.h"
|
2002-06-16 01:37:11 +08:00
|
|
|
#include "utils.h"
|
2002-10-04 04:49:57 +08:00
|
|
|
#include "vector.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:47:52 +08:00
|
|
|
static char *syslog_level[] = {
|
2008-12-01 23:01:11 +08:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"CRITICAL",
|
|
|
|
"ERROR",
|
|
|
|
"WARNING",
|
|
|
|
"NOTICE",
|
|
|
|
"INFO",
|
|
|
|
"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
|
|
|
|
2002-10-04 04:49:57 +08:00
|
|
|
/*
|
|
|
|
* Global file descriptor for the log file
|
|
|
|
*/
|
|
|
|
int log_file_fd = -1;
|
|
|
|
|
2001-06-02 11:09:27 +08:00
|
|
|
/*
|
|
|
|
* Store the log level setting.
|
|
|
|
*/
|
2002-04-23 03:34:20 +08:00
|
|
|
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.
|
|
|
|
*/
|
2002-10-04 04:49:57 +08:00
|
|
|
static vector_t log_message_storage;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the log file and store the file descriptor in a global location.
|
|
|
|
*/
|
|
|
|
int
|
2008-12-01 23:01:11 +08:00
|
|
|
open_log_file (const char *log_file_name)
|
2002-10-04 04:49:57 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
log_file_fd = create_file_safely (log_file_name, FALSE);
|
|
|
|
return log_file_fd;
|
2002-10-04 04:49:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the log file
|
|
|
|
*/
|
|
|
|
void
|
2008-12-01 23:01:11 +08:00
|
|
|
close_log_file (void)
|
2002-10-04 04:49:57 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
close (log_file_fd);
|
2002-10-04 04:49:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Truncate log file to a zero length.
|
|
|
|
*/
|
|
|
|
void
|
2008-12-01 23:01:11 +08:00
|
|
|
truncate_log_file (void)
|
2002-10-04 04:49:57 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
lseek (log_file_fd, 0, SEEK_SET);
|
|
|
|
ftruncate (log_file_fd, 0);
|
2002-10-04 04:49:57 +08:00
|
|
|
}
|
2001-06-02 11:09:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the log level for writing to the log file.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
2008-12-01 23:01:11 +08:00
|
|
|
set_log_level (int level)
|
2001-06-02 11:09:27 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
log_level = level;
|
2001-06-02 11:09:27 +08:00
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/*
|
|
|
|
* This routine logs messages to either the log file or the syslog function.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
2008-12-01 23:01:11 +08:00
|
|
|
log_message (int level, char *fmt, ...)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
va_list args;
|
|
|
|
time_t nowtime;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
char time_string[TIME_LENGTH];
|
|
|
|
char str[STRING_LENGTH];
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-08-29 11:59:12 +08:00
|
|
|
#ifdef NDEBUG
|
2008-12-01 23:01:11 +08:00
|
|
|
/*
|
|
|
|
* Figure out if we should write the message or not.
|
|
|
|
*/
|
|
|
|
if (log_level == LOG_CONN)
|
|
|
|
{
|
|
|
|
if (level == LOG_INFO)
|
2008-12-08 21:39:44 +08:00
|
|
|
return;
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
|
|
|
else if (log_level == LOG_INFO)
|
|
|
|
{
|
|
|
|
if (level > LOG_INFO && level != LOG_CONN)
|
2008-12-08 21:39:44 +08:00
|
|
|
return;
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
|
|
|
else if (level > log_level)
|
|
|
|
return;
|
2001-08-29 11:59:12 +08:00
|
|
|
#endif
|
2001-08-28 23:51:58 +08:00
|
|
|
|
2001-08-27 05:10:04 +08:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2008-12-01 23:01:11 +08:00
|
|
|
if (config.syslog && level == LOG_CONN)
|
|
|
|
level = LOG_INFO;
|
2001-08-27 05:10:04 +08:00
|
|
|
#endif
|
2001-06-02 11:09:27 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
va_start (args, fmt);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
/*
|
|
|
|
* If the config file hasn't been processed, then we need to store
|
|
|
|
* the messages for later processing.
|
|
|
|
*/
|
|
|
|
if (!processed_config_file)
|
|
|
|
{
|
|
|
|
char *entry_buffer;
|
2002-04-23 03:34:20 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!log_message_storage)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
log_message_storage = vector_create ();
|
|
|
|
if (!log_message_storage)
|
|
|
|
goto out;
|
|
|
|
}
|
2002-04-23 03:34:20 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
vsnprintf (str, STRING_LENGTH, fmt, args);
|
2002-04-23 03:34:20 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
entry_buffer = safemalloc (strlen (str) + 6);
|
|
|
|
if (!entry_buffer)
|
2008-12-08 21:39:44 +08:00
|
|
|
goto out;
|
2002-04-23 03:34:20 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
sprintf (entry_buffer, "%d %s", level, str);
|
|
|
|
vector_append (log_message_storage, entry_buffer,
|
2008-12-08 21:39:44 +08:00
|
|
|
strlen (entry_buffer) + 1);
|
2008-03-31 08:51:51 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
safefree (entry_buffer);
|
|
|
|
goto out;
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2008-12-01 23:01:11 +08:00
|
|
|
if (config.syslog)
|
|
|
|
{
|
2000-09-12 07:47:52 +08:00
|
|
|
# ifdef HAVE_VSYSLOG_H
|
2008-12-01 23:01:11 +08:00
|
|
|
vsyslog (level, fmt, args);
|
2000-09-12 07:47:52 +08:00
|
|
|
# else
|
2008-12-01 23:01:11 +08:00
|
|
|
vsnprintf (str, STRING_LENGTH, fmt, args);
|
|
|
|
syslog (level, "%s", str);
|
2000-09-12 07:47:52 +08:00
|
|
|
# endif
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif
|
2008-12-01 23:01:11 +08:00
|
|
|
nowtime = time (NULL);
|
|
|
|
/* Format is month day hour:minute:second (24 time) */
|
|
|
|
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
|
2008-12-08 21:39:44 +08:00
|
|
|
localtime (&nowtime));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ",
|
2008-12-08 21:39:44 +08:00
|
|
|
syslog_level[level], time_string, (long int) getpid ());
|
2002-10-04 04:49:57 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
assert (log_file_fd >= 0);
|
2002-10-04 04:49:57 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
write (log_file_fd, str, strlen (str));
|
|
|
|
vsnprintf (str, STRING_LENGTH, fmt, args);
|
|
|
|
write (log_file_fd, str, strlen (str));
|
|
|
|
write (log_file_fd, "\n", 1);
|
|
|
|
fsync (log_file_fd);
|
2002-06-16 01:37:11 +08:00
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif
|
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
out:
|
|
|
|
va_end (args);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
2002-04-23 03:34:20 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This needs to send any stored log messages.
|
|
|
|
*/
|
|
|
|
void
|
2008-12-01 23:01:11 +08:00
|
|
|
send_stored_logs (void)
|
2002-04-23 03:34:20 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
char *string;
|
|
|
|
char *ptr;
|
2002-10-04 04:49:57 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
int level;
|
2002-06-07 04:24:21 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
size_t i;
|
2002-06-07 04:24:21 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
for (i = 0; i != vector_length (log_message_storage); ++i)
|
|
|
|
{
|
|
|
|
string = vector_getentry (log_message_storage, i, NULL);
|
2002-10-04 04:49:57 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = strchr (string, ' ') + 1;
|
|
|
|
level = atoi (string);
|
2002-06-07 04:24:21 +08:00
|
|
|
|
2003-08-01 07:38:28 +08:00
|
|
|
#ifdef NDEBUG
|
2008-12-01 23:01:11 +08:00
|
|
|
if (log_level == LOG_CONN && level == LOG_INFO)
|
2008-12-08 21:39:44 +08:00
|
|
|
continue;
|
2008-12-01 23:01:11 +08:00
|
|
|
else if (log_level == LOG_INFO)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
if (level > LOG_INFO && level != LOG_CONN)
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
else if (level > log_level)
|
2008-12-08 21:39:44 +08:00
|
|
|
continue;
|
2002-06-07 04:24:21 +08:00
|
|
|
#endif
|
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
log_message (level, ptr);
|
|
|
|
}
|
2002-04-26 02:56:43 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
vector_delete (log_message_storage);
|
|
|
|
log_message_storage = NULL;
|
2002-04-23 03:34:20 +08:00
|
|
|
}
|