Use shorts for chunk sizes when possible.
[polipo.git] / log.c
blob0a591b42ea7bb35132317debd977ba05ccf548e1
1 /*
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
20 THE SOFTWARE.
23 #include "polipo.h"
25 int logLevel = LOGGING_DEFAULT;
26 AtomPtr logFile = NULL;
27 FILE *logF;
29 #define STR(x) XSTR(x)
30 #define XSTR(x) #x
32 void
33 preinitLog()
35 CONFIG_VARIABLE_SETTABLE(logLevel, CONFIG_HEX, configIntSetter,
36 "Logging level (max = " STR(LOGGING_MAX) ").");
37 CONFIG_VARIABLE(logFile, CONFIG_ATOM, "Log file (stderr if empty).");
38 logF = stderr;
41 void
42 initLog()
44 if(daemonise && logFile == NULL)
45 logFile = internAtom("/var/log/polipo");
47 if(logFile != NULL && logFile->length > 0) {
48 FILE *f;
49 f = fopen(logFile->string, "a");
50 if(f == NULL) {
51 do_log_error(L_ERROR, errno, "Couldn't open log file %s",
52 logFile->string);
53 exit(1);
55 setvbuf(f, NULL, _IOLBF, 0);
56 logF = f;
60 void
61 reopenLog()
63 if(logFile) {
64 FILE *f;
65 f = fopen(logFile->string, "a");
66 if(f == NULL) {
67 do_log_error(L_ERROR, errno, "Couldn't reopen log file %s",
68 logFile->string);
69 exit(1);
71 setvbuf(f, NULL, _IOLBF, 0);
72 fclose(logF);
73 logF = f;
77 void
78 really_do_log(int type, const char *f, ...)
80 va_list args;
82 va_start(args, f);
83 if(type & LOGGING_MAX & logLevel)
84 really_do_log_v(type, f, args);
85 va_end(args);
88 void
89 really_do_log_v(int type, const char *f, va_list args)
91 if(type & LOGGING_MAX & logLevel)
92 vfprintf(logF, f, args);
95 void
96 really_do_log_error(int type, int e, const char *f, ...)
98 va_list args;
99 va_start(args, f);
100 if(type & LOGGING_MAX & logLevel)
101 really_do_log_error_v(type, e, f, args);
102 va_end(args);
105 void
106 really_do_log_error_v(int type, int e, const char *f, va_list args)
108 if((type & LOGGING_MAX & logLevel) != 0) {
109 char *es = pstrerror(e);
110 if(es == NULL)
111 es = "Unknown error";
112 vfprintf(logF, f, args);
113 fprintf(logF, ": %s\n", es);
117 void
118 really_do_log_n(int type, const char *s, int n)
120 if((type & LOGGING_MAX & logLevel) != 0) {
121 fwrite(s, n, 1, logF);