Cleaned up the logging format, and also included logging levels (which are

similar to the syslogd format.)
This commit is contained in:
Robert James Kaes 2000-09-11 23:47:52 +00:00
parent 5d7063a79e
commit 4f4f91f76f
2 changed files with 99 additions and 41 deletions

View File

@ -1,4 +1,4 @@
/* $Id: log.c,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $ /* $Id: log.c,v 1.2 2000-09-11 23:47:52 rjkaes Exp $
* *
* Logs the various messages which tinyproxy produces to either a log file or * Logs the various messages which tinyproxy produces to either a log file or
* the syslog daemon. Not much to it... * the syslog daemon. Not much to it...
@ -15,74 +15,68 @@
* WITHOUT ANY WARRANTY; without even the implied warranty of * WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details. * General Public License for more details.
*
* log.c - For the manipulation of log files.
*/ */
#ifdef HAVE_CONFIG_H
#include <defines.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include "tinyproxy.h" #include "tinyproxy.h"
#include <stdarg.h>
#include "log.h" #include "log.h"
#define LENGTH 16 static char *syslog_level[] = {
NULL,
NULL,
"CRITICAL",
"ERROR",
"WARNING",
NULL,
"INFO",
"DEBUG"
};
#define TIME_LENGTH 16
#define STRING_LENGTH 800
/* /*
* This routine logs messages to either the log file or the syslog function. * This routine logs messages to either the log file or the syslog function.
*/ */
void log(char *fmt, ...) void log(short int level, char *fmt, ...)
{ {
va_list args; va_list args;
time_t nowtime; time_t nowtime;
FILE *cf; FILE *cf;
char time_string[TIME_LENGTH];
#if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H) #if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
static char str[800]; char str[STRING_LENGTH];
#endif #endif
static char time_string[LENGTH];
assert(fmt);
va_start(args, fmt); va_start(args, fmt);
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
if (config.syslog == FALSE) { if (config.syslog) {
# ifdef HAVE_VSYSLOG_H
vsyslog(level, fmt, args);
# else
vsnprintf(str, STRING_LENGTH, fmt, args);
syslog(level, str);
# endif
} else {
#endif #endif
nowtime = time(NULL); nowtime = time(NULL);
/* Format is month day hour:minute:second (24 time) */ /* Format is month day hour:minute:second (24 time) */
strftime(time_string, LENGTH, "%b %d %H:%M:%S", strftime(time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime(&nowtime)); localtime(&nowtime));
if (!(cf = config.logf)) if (!(cf = config.logf))
cf = stderr; cf = stderr;
fprintf(cf, "%s [%d]: ", time_string, getpid()); fprintf(cf, "%-9s %s [%d]: ", syslog_level[level],
time_string, getpid());
vfprintf(cf, fmt, args); vfprintf(cf, fmt, args);
fprintf(cf, "\n"); fprintf(cf, "\n");
fflush(cf); fflush(cf);
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
} else {
# ifdef HAVE_VSYSLOG_H
vsyslog(LOG_INFO, fmt, args);
# else
# ifdef HAVE_VSNPRINTF
vsnprintf(str, 800, fmt, args);
# else
# ifdef HAVE_VPRINTF
vsprintf(str, fmt, args);
# endif
# endif
syslog(LOG_INFO, str);
# endif
} }
#endif #endif

View File

@ -1,4 +1,4 @@
/* $Id: log.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $ /* $Id: log.h,v 1.2 2000-09-11 23:47:52 rjkaes Exp $
* *
* See 'log.c' for a detailed description. * See 'log.c' for a detailed description.
* *
@ -16,9 +16,73 @@
* General Public License for more details. * General Public License for more details.
*/ */
#ifndef _LOG_H_ #ifndef _TINYPROXY_LOG_H_
#define _LOG_H_ 1 #define _TINYPROXY_LOG_H_
extern void log(char *fmt, ...); #ifdef HAVE_CONFIG_H
# include "../config.h"
#endif
/*
* Okay, I have modelled the levels for logging off the syslog() interface.
* However, I would really prefer if only five of the levels are used. You
* can see them below and I'll describe what each level should be for.
* Hopefully tinyproxy will remain consistent with these levels.
* -- rjkaes
*
* Level Description
* ----- -----------
* LOG_CRIT This is catastrophic. Basically, tinyproxy can not recover
* from this and will either close the thread (if we're lucky),
* or the entire daemon. I would relegate this to conditions
* like unable to create the listening socket, or unable to
* create a thread. If you're going to log at this level provide
* as much information as possible.
*
* LOG_ERR Okay, something bad happened. We can recover from this, but
* the connection will be terminated. This should be for things
* like when we cannot create a socket, or out of memory.
* Basically, the connection will not work, but it's not enough
* to bring the whole daemon down.
*
* LOG_WARNING There is condition which will change the behaviour of
* tinyproxy from what is expected. For example, somebody did
* not specify a port. tinyproxy will handle this (by using
* it's default port), but it's a _higher_ level situation
* which the admin should be aware of.
*
* LOG_INFO Everything else ends up here. Logging for incoming
* connections, denying due to filtering rules, unable to
* connect to remote server, etc.
*
* LOG_DEBUG Don't use this level. :) Use the two DEBUG?() macros
* instead since they can remain in the source if needed. (I
* don't advocate this, but it could be useful at times.)
*/
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#else
# define LOG_CRIT 2
# define LOG_ERR 3
# define LOG_WARNING 4
# define LOG_INFO 6
# define LOG_DEBUG 7
#endif
/*
* Use this for debugging. The format is specific:
* DEBUG1("There was a major problem");
* DEBUG2("There was a big problem: %s in connptr %p", "hello", connptr);
*/
#ifndef NDEBUG
# define DEBUG1(x) log(LOG_DEBUG, "[%s:%d] " x, __FILE__, __LINE__)
# define DEBUG2(x, y...) log(LOG_DEBUG, "[%s:%d] " x, __FILE__, __LINE__, ## y)
#else
# define DEBUG1(x) do { } while(0)
# define DEBUG2(x, y...) do { } while(0)
#endif
extern void log(short int level, char *fmt, ...);
#endif #endif