1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* 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>
*
* 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.
*
* 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.
*
* 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...
*/
#include "main.h"
#include "heap.h"
#include "log.h"
#include "utils.h"
#include "vector.h"
#include "conf.h"
static const char *syslog_level[] = {
NULL,
NULL,
"CRITICAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
"CONNECT"
};
#define TIME_LENGTH 16
#define STRING_LENGTH 800
/*
* Global file descriptor for the log file
*/
int log_file_fd = -1;
/*
* Store the log level setting.
*/
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.
*/
static vector_t log_message_storage;
static unsigned int logging_initialized = FALSE; /* boolean */
/*
* Open the log file and store the file descriptor in a global location.
*/
int open_log_file (const char *log_file_name)
{
log_file_fd = create_file_safely (log_file_name, FALSE);
return log_file_fd;
}
/*
* Close the log file
*/
void close_log_file (void)
{
close (log_file_fd);
}
/*
* Truncate log file to a zero length.
*/
void truncate_log_file (void)
{
lseek (log_file_fd, 0, SEEK_SET);
if (ftruncate (log_file_fd, 0) != 0) {
log_message (LOG_WARNING,
"Unable to truncate log file to zero length");
}
}
/*
* Set the log level for writing to the log file.
*/
void set_log_level (int level)
{
log_level = level;
}
/*
* This routine logs messages to either the log file or the syslog function.
*/
void log_message (int level, const char *fmt, ...)
{
va_list args;
time_t nowtime;
char time_string[TIME_LENGTH];
char str[STRING_LENGTH];
ssize_t ret;
#ifdef NDEBUG
/*
* Figure out if we should write the message or not.
*/
if (log_level == LOG_CONN) {
if (level == LOG_INFO)
return;
} else if (log_level == LOG_INFO) {
if (level > LOG_INFO && level != LOG_CONN)
return;
} else if (level > log_level)
return;
#endif
#ifdef HAVE_SYSLOG_H
if (config.syslog && level == LOG_CONN)
level = LOG_INFO;
#endif
va_start (args, fmt);
/*
* If the config file hasn't been processed, then we need to store
* the messages for later processing.
*/
if (!logging_initialized) {
char *entry_buffer;
if (!log_message_storage) {
log_message_storage = vector_create ();
if (!log_message_storage)
goto out;
}
vsnprintf (str, STRING_LENGTH, fmt, args);
entry_buffer = (char *) safemalloc (strlen (str) + 6);
if (!entry_buffer)
goto out;
sprintf (entry_buffer, "%d %s", level, str);
vector_append (log_message_storage, entry_buffer,
strlen (entry_buffer) + 1);
safefree (entry_buffer);
goto out;
}
#ifdef HAVE_SYSLOG_H
if (config.syslog) {
# ifdef HAVE_VSYSLOG_H
vsyslog (level, fmt, args);
# else
vsnprintf (str, STRING_LENGTH, fmt, args);
syslog (level, "%s", str);
# endif
} else {
#endif
nowtime = time (NULL);
/* Format is month day hour:minute:second (24 time) */
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime (&nowtime));
snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ",
syslog_level[level], time_string,
(long int) getpid ());
assert (log_file_fd >= 0);
ret = write (log_file_fd, str, strlen (str));
if (ret == -1) {
log_message (LOG_WARNING,
"Could not write to log file");
}
vsnprintf (str, STRING_LENGTH, fmt, args);
ret = write (log_file_fd, str, strlen (str));
if (ret == -1) {
log_message (LOG_WARNING,
"Could not write to log file");
}
ret = write (log_file_fd, "\n", 1);
if (ret == -1) {
log_message (LOG_WARNING,
"Could not write to log file");
}
fsync (log_file_fd);
#ifdef HAVE_SYSLOG_H
}
#endif
out:
va_end (args);
}
/*
* This needs to send any stored log messages.
*/
void send_stored_logs (void)
{
char *string;
char *ptr;
int level;
size_t i;
for (i = 0; (ssize_t) i != vector_length (log_message_storage); ++i) {
string =
(char *) vector_getentry (log_message_storage, i, NULL);
ptr = strchr (string, ' ') + 1;
level = atoi (string);
#ifdef NDEBUG
if (log_level == LOG_CONN && level == LOG_INFO)
continue;
else if (log_level == LOG_INFO) {
if (level > LOG_INFO && level != LOG_CONN)
continue;
} else if (level > log_level)
continue;
#endif
log_message (level, ptr);
}
vector_delete (log_message_storage);
log_message_storage = NULL;
}
/**
* Initialize the logging subsystem, based on the configuration.
* Returns 0 upon success, -1 upon failure.
*
* This function uses fprintf() instead of log_message(), since
* the logging is not yet set up...
*/
int setup_logging (void)
{
int ret = -1;
/* Write to a user supplied log file if it's defined. This will
* override using the syslog even if syslog is defined. */
if (config.logf_name) {
if (open_log_file (config.logf_name) < 0) {
fprintf (stderr,
"%s: Could not create log file.\n", PACKAGE);
goto done;
}
config.syslog = FALSE; /* disable syslog */
} else if (config.syslog) {
if (config.godaemon == TRUE)
openlog ("tinyproxy", LOG_PID, LOG_DAEMON);
else
openlog ("tinyproxy", LOG_PID, LOG_USER);
} else {
fprintf (stderr, "%s: Either define a logfile or "
"enable syslog logging.\n", PACKAGE);
goto done;
}
logging_initialized = TRUE;
send_stored_logs ();
ret = 0;
done:
return ret;
}
/**
* Stop the logging subsystem.
*/
void shutdown_logging (void)
{
if (config.logf_name) {
close_log_file ();
} else if (config.syslog) {
closelog ();
} else {
fprintf (stderr, "error - shutdown_logging called while "
" logging not configured yet\n");
}
logging_initialized = FALSE;
}
|