~ubuntu-branches/ubuntu/edgy/tcpspy/edgy

« back to all changes in this revision

Viewing changes to log.c

  • Committer: Bazaar Package Importer
  • Author(s): Pablo Lorenzzoni
  • Date: 2002-01-25 00:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20020125000000-tb043mswgs8cgpdx
Tags: upstream-1.7d
ImportĀ upstreamĀ versionĀ 1.7d

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * log.c - Logging routines
 
3
 *
 
4
 * This file is part of tcpspy, a TCP/IP connection monitor. 
 
5
 *
 
6
 * Copyright (c) 2000, 2001 Tim J. Robbins. 
 
7
 * All rights reserved.
 
8
 *
 
9
 * Redistribution and use in source and binary forms, with or without
 
10
 * modification, are permitted provided that the following conditions
 
11
 * are met:
 
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.
 
19
 *
 
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.
 
30
 *
 
31
 * $Id: log.c,v 1.5 2001/01/05 14:05:47 fyre Stab $
 
32
 */
 
33
 
 
34
#include <assert.h>
 
35
#include <stdarg.h>
 
36
#include <stdio.h>
 
37
#include <stdlib.h>
 
38
#include <syslog.h>
 
39
#include <time.h>
 
40
#include <unistd.h>
 
41
 
 
42
#include "rcsid.h"
 
43
 
 
44
RCSID("$Id: log.c,v 1.5 2001/01/05 14:05:47 fyre Stab $");
 
45
 
 
46
/*
 
47
 * Exported functions.
 
48
 */
 
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);
 
55
 
 
56
/*
 
57
 * Internal helper functions.
 
58
 */
 
59
static void vlogmsg_i (int panic, const char *fmt, va_list ap);
 
60
 
 
61
/*
 
62
 * Methods of logging, set via log_set_foo () family of functions. LM_SYSLOG
 
63
 * is the default.
 
64
 */
 
65
#define LM_SYSLOG       0       /* via syslog(3) */
 
66
#define LM_STDOUT       1       /* standard output */
 
67
static int logmethod = LM_SYSLOG;
 
68
 
 
69
/*
 
70
 * log_set_syslog ()
 
71
 *
 
72
 * Log via syslog(3). openlog () should be called before any of the logging
 
73
 * routines to set the ident and facility.
 
74
 */
 
75
void log_set_syslog (void)
 
76
{
 
77
        logmethod = LM_SYSLOG;
 
78
}
 
79
 
 
80
/*
 
81
 * log_set_stdout ()
 
82
 *
 
83
 * Log to standard output.
 
84
 */
 
85
void log_set_stdout (void)
 
86
{
 
87
        logmethod = LM_STDOUT;
 
88
}
 
89
 
 
90
/*
 
91
 * logmsg ()
 
92
 * 
 
93
 * printf-style function to log messages of normal priority.
 
94
 */
 
95
void logmsg (const char *fmt, ...)
 
96
{
 
97
        va_list ap;
 
98
 
 
99
        va_start (ap, fmt);
 
100
        vlogmsg (fmt, ap);
 
101
        va_end (ap);
 
102
}
 
103
 
 
104
/*
 
105
 * vlogmsg ()
 
106
 *
 
107
 * vprintf-style function to log messages of normal priority.
 
108
 */
 
109
void vlogmsg (const char *fmt, va_list ap)
 
110
{
 
111
        vlogmsg_i (0, fmt, ap); 
 
112
}
 
113
 
 
114
/*
 
115
 * panic ()
 
116
 *
 
117
 * printf-style function to log critical errors and terminate.
 
118
 */
 
119
void panic (const char *fmt, ...)
 
120
{
 
121
        va_list ap;
 
122
 
 
123
        va_start (ap, fmt);
 
124
        vpanic (fmt, ap);
 
125
        /* Not reached, vpanic () exits */
 
126
        va_end (ap);
 
127
}
 
128
 
 
129
/*
 
130
 * vpanic ()
 
131
 *
 
132
 * vprintf-style function to log critical errors and terminate.
 
133
 */
 
134
void vpanic (const char *fmt, va_list ap)
 
135
{
 
136
        vlogmsg_i (1, fmt, ap);
 
137
        exit (EXIT_FAILURE);
 
138
}
 
139
 
 
140
/*
 
141
 * vlogmsg_i ()
 
142
 *
 
143
 * Internal routine called by logmsg (), vlogmsg (), panic () and vpanic ().
 
144
 * Logs message by specified method.
 
145
 */
 
146
static void vlogmsg_i (int panic, const char *fmt, va_list ap)
 
147
{
 
148
        switch (logmethod) {
 
149
                case LM_SYSLOG:
 
150
                        vsyslog (panic == 0 ? LOG_NOTICE : LOG_ERR, fmt, ap);
 
151
                        break;
 
152
 
 
153
                case LM_STDOUT: 
 
154
                        {
 
155
                        char timebuf[100];
 
156
                        time_t now;
 
157
                        struct tm *t;
 
158
                        FILE *fp;
 
159
 
 
160
                        /*
 
161
                         * Timestamp the message. If an error occurs, work
 
162
                         * around it so messages aren't silently lost.
 
163
                         */
 
164
                        if ((now = time (NULL)) == (time_t) -1)
 
165
                                snprintf (timebuf, sizeof (timebuf), 
 
166
                                                "<unknown>");
 
167
                        else if ((t = localtime (&now)) == NULL)
 
168
                                snprintf (timebuf, sizeof (timebuf), "EPOCH+%u",
 
169
                                                (unsigned int) now);
 
170
                        else if (strftime (timebuf, sizeof (timebuf), 
 
171
                                                "%b %d %H:%M:%S", t) == 0)
 
172
                                snprintf (timebuf, sizeof (timebuf), "%u", 
 
173
                                                (unsigned int) now);
 
174
                        /*
 
175
                         * Normal messages go to stdout, panic messages go
 
176
                         * to stderr.
 
177
                         */
 
178
                        fp = (panic == 0) ? stdout : stderr;
 
179
                        
 
180
                        fprintf (fp, "%s: ", timebuf);
 
181
 
 
182
                        vfprintf (fp, fmt, ap);
 
183
                        fputc ('\n', fp);
 
184
                        fflush (fp);
 
185
                        }
 
186
                        break;
 
187
                default:
 
188
                        assert (0);
 
189
        }
 
190
}