2
* log.c - Logging routines
4
* This file is part of tcpspy, a TCP/IP connection monitor.
6
* Copyright (c) 2000, 2001 Tim J. Robbins.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* 3. The name of the author may not be used to endorse or promote products
18
* derived from this software without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
* $Id: log.c,v 1.5 2001/01/05 14:05:47 fyre Stab $
44
RCSID("$Id: log.c,v 1.5 2001/01/05 14:05:47 fyre Stab $");
49
void log_set_syslog (void);
50
void log_set_stdout (void);
51
void logmsg (const char *fmt, ...);
52
void vlogmsg (const char *fmt, va_list ap);
53
void panic (const char *fmt, ...);
54
void vpanic (const char *fmt, va_list ap);
57
* Internal helper functions.
59
static void vlogmsg_i (int panic, const char *fmt, va_list ap);
62
* Methods of logging, set via log_set_foo () family of functions. LM_SYSLOG
65
#define LM_SYSLOG 0 /* via syslog(3) */
66
#define LM_STDOUT 1 /* standard output */
67
static int logmethod = LM_SYSLOG;
72
* Log via syslog(3). openlog () should be called before any of the logging
73
* routines to set the ident and facility.
75
void log_set_syslog (void)
77
logmethod = LM_SYSLOG;
83
* Log to standard output.
85
void log_set_stdout (void)
87
logmethod = LM_STDOUT;
93
* printf-style function to log messages of normal priority.
95
void logmsg (const char *fmt, ...)
107
* vprintf-style function to log messages of normal priority.
109
void vlogmsg (const char *fmt, va_list ap)
111
vlogmsg_i (0, fmt, ap);
117
* printf-style function to log critical errors and terminate.
119
void panic (const char *fmt, ...)
125
/* Not reached, vpanic () exits */
132
* vprintf-style function to log critical errors and terminate.
134
void vpanic (const char *fmt, va_list ap)
136
vlogmsg_i (1, fmt, ap);
143
* Internal routine called by logmsg (), vlogmsg (), panic () and vpanic ().
144
* Logs message by specified method.
146
static void vlogmsg_i (int panic, const char *fmt, va_list ap)
150
vsyslog (panic == 0 ? LOG_NOTICE : LOG_ERR, fmt, ap);
161
* Timestamp the message. If an error occurs, work
162
* around it so messages aren't silently lost.
164
if ((now = time (NULL)) == (time_t) -1)
165
snprintf (timebuf, sizeof (timebuf),
167
else if ((t = localtime (&now)) == NULL)
168
snprintf (timebuf, sizeof (timebuf), "EPOCH+%u",
170
else if (strftime (timebuf, sizeof (timebuf),
171
"%b %d %H:%M:%S", t) == 0)
172
snprintf (timebuf, sizeof (timebuf), "%u",
175
* Normal messages go to stdout, panic messages go
178
fp = (panic == 0) ? stdout : stderr;
180
fprintf (fp, "%s: ", timebuf);
182
vfprintf (fp, fmt, ap);