2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 static int logLevel
= LOGGING_DEFAULT
;
30 static int logSyslog
= 0;
31 static AtomPtr logFile
= NULL
;
34 static AtomPtr logFacility
= NULL
;
38 #define STR(x) XSTR(x)
41 static void initSyslog(void);
44 static char *syslogBuf
;
45 static ssize_t syslogBufSize
;
46 static size_t syslogBufLength
;
48 static int translateFacility(AtomPtr facility
);
49 static int translatePriority(int type
);
50 static void accumulateSyslog(int type
, const char *msg
, int len
);
56 CONFIG_VARIABLE_SETTABLE(logLevel
, CONFIG_HEX
, configIntSetter
,
57 "Logging level (max = " STR(LOGGING_MAX
) ").");
58 CONFIG_VARIABLE(logFile
, CONFIG_ATOM
, "Log file (stderr if empty and logSyslog is unset).");
61 CONFIG_VARIABLE(logSyslog
, CONFIG_BOOLEAN
, "Log to syslog.");
62 CONFIG_VARIABLE(logFacility
, CONFIG_ATOM
, "Facility to use.");
69 loggingToStderr(void) {
70 return(logF
== stderr
);
76 if(daemonise
&& logFile
== NULL
&& !logSyslog
)
77 logFile
= internAtom("/var/log/polipo");
79 if(logFile
!= NULL
&& logFile
->length
> 0) {
81 f
= fopen(logFile
->string
, "a");
83 do_log_error(L_ERROR
, errno
, "Couldn't open log file %s",
87 setvbuf(f
, NULL
, _IOLBF
, 0);
105 facility
= translateFacility(logFacility
);
107 openlog("polipo", LOG_PID
, facility
);
110 syslogBuf
= strdup("");
116 /* Map a user-provided name to a syslog facility.
118 This is rendered quite ugly because POSIX hardly defines any, but we
119 should allow any the local system knows about. */
122 translateFacility(AtomPtr facility
)
130 /* List of all known valid syslog facilities.
132 This list is terminated by a NULL facility name. */
134 FacilitiesRec facilities
[] = {
135 /* These are all the facilities found in glibc 2.5. */
137 { "auth", LOG_AUTH
},
140 { "authpriv", LOG_AUTHPRIV
},
143 { "cron", LOG_CRON
},
146 { "daemon", LOG_DAEMON
},
152 { "kern", LOG_KERN
},
158 { "mail", LOG_MAIL
},
161 { "news", LOG_NEWS
},
164 { "syslog", LOG_SYSLOG
},
167 { "uucp", LOG_UUCP
},
169 /* These are required by POSIX. */
170 { "user", LOG_USER
},
171 { "local0", LOG_LOCAL0
},
172 { "local1", LOG_LOCAL1
},
173 { "local2", LOG_LOCAL2
},
174 { "local3", LOG_LOCAL3
},
175 { "local4", LOG_LOCAL4
},
176 { "local5", LOG_LOCAL5
},
177 { "local6", LOG_LOCAL6
},
178 { "local7", LOG_LOCAL7
},
181 FacilitiesRec
*current
;
183 /* It would be more fitting to return LOG_DAEMON, but POSIX does not
184 guarantee the existence of that facility. */
190 current
= facilities
;
191 while(current
->name
) {
192 if(!strcmp(current
->name
, atomString(facility
))) {
193 return current
->facility
;
198 /* This will go to stderr because syslog is not yet initialized. */
199 do_log(L_ERROR
, "Specified logFacility %s nonexistent on this system.",
200 atomString(facility
));
205 /* Translate a Polipo error type into a syslog priority. */
208 translatePriority(int type
)
216 /* The list is terminated with a type of zero. */
218 PrioritiesRec priorities
[] = {{ L_ERROR
, LOG_ERR
},
219 { L_WARN
, LOG_WARNING
},
220 { L_INFO
, LOG_NOTICE
},
221 { L_FORBIDDEN
, LOG_DEBUG
},
222 { L_UNCACHEABLE
, LOG_DEBUG
},
223 { L_SUPERSEDED
, LOG_DEBUG
},
224 { L_VARY
, LOG_DEBUG
},
226 PrioritiesRec
*current
;
228 current
= priorities
;
229 while(current
->type
) {
230 if(current
->type
== type
) {
231 return current
->priority
;
239 /* Accumulate a message for syslogging, emitting pieces ending in linefeeds.
241 Argument formatting is done by the caller. */
244 accumulateSyslog(int type
, const char *msg
, int len
)
246 ssize_t new_syslogBufSize
;
249 /* First, expand (possibly) and concatenate. */
251 new_syslogBufSize
= syslogBufLength
+ len
;
253 if(new_syslogBufSize
>= syslogBufSize
) {
256 new_syslogBuf
= realloc(syslogBuf
, new_syslogBufSize
);
260 syslogBuf
= new_syslogBuf
;
261 syslogBufSize
= new_syslogBufSize
;
264 memcpy(syslogBuf
+ syslogBufLength
, msg
, len
);
265 syslogBufLength
+= len
;
267 /* Now look for a linefeed and flush as long as one is found. */
269 while((linefeed
= memchr(syslogBuf
, '\n', syslogBufLength
)) != NULL
) {
271 syslog(translatePriority(type
), "%s", syslogBuf
);
274 /* Some is printed: flush it. */
276 syslogBufLength
-= linefeed
- syslogBuf
;
277 memmove(syslogBuf
, linefeed
, syslogBufLength
);
288 /* Flush any messages waiting to be logged. */
295 /* There shouldn't really be anything here, but let's be paranoid.
296 We can't pick a good value for `type', so just invent one. */
297 if(logSyslog
&& syslogBuf
[0] != '\0') {
298 accumulateSyslog(L_INFO
, "\n", 1);
301 assert(syslogBufLength
== 0);
310 f
= fopen(logFile
->string
, "a");
312 do_log_error(L_ERROR
, errno
, "Couldn't reopen log file %s",
316 setvbuf(f
, NULL
, _IOLBF
, 0);
327 really_do_log(int type
, const char *f
, ...)
332 if(type
& LOGGING_MAX
& logLevel
)
333 really_do_log_v(type
, f
, args
);
338 really_do_log_v(int type
, const char *f
, va_list args
)
340 if(type
& LOGGING_MAX
& logLevel
) {
342 vfprintf(logF
, f
, args
);
347 n
= vsnprintf(msg
, 256, f
, args
);
348 if(n
>= 0 && n
< 255)
349 accumulateSyslog(type
, msg
, n
);
351 accumulateSyslog(LOG_ERR
, "Syslog message lost.\n", 21);
358 really_do_log_error(int type
, int e
, const char *f
, ...)
362 if(type
& LOGGING_MAX
& logLevel
)
363 really_do_log_error_v(type
, e
, f
, args
);
368 really_do_log_error_v(int type
, int e
, const char *f
, va_list args
)
370 if((type
& LOGGING_MAX
& logLevel
) != 0) {
371 char *es
= pstrerror(e
);
373 es
= "Unknown error";
376 vfprintf(logF
, f
, args
);
377 fprintf(logF
, ": %s\n", es
);
384 n
= snnvprintf(msg
, n
, 256, f
, args
);
385 n
= snnprintf(msg
, n
, 256, ": ");
386 n
= snnprint_n(msg
, n
, 256, es
, strlen (es
));
387 n
= snnprintf(msg
, n
, 256, "\n");
388 /* Overflow? Vanishingly unlikely; truncate at 255. */
389 if(n
< 0 || n
> 256) {
396 accumulateSyslog(type
, msg
, n
);
403 really_do_log_n(int type
, const char *s
, int n
)
405 if((type
& LOGGING_MAX
& logLevel
) != 0) {
407 fwrite(s
, n
, 1, logF
);
411 accumulateSyslog(type
, s
, n
);