2 Copyright (c) 2003, 2004 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
25 int logLevel
= LOGGING_DEFAULT
;
26 AtomPtr logFile
= NULL
;
29 #define STR(x) XSTR(x)
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).");
44 if(daemonise
&& logFile
== NULL
)
45 logFile
= internAtom("/var/log/polipo");
47 if(logFile
!= NULL
&& logFile
->length
> 0) {
49 f
= fopen(logFile
->string
, "a");
51 do_log_error(L_ERROR
, errno
, "Couldn't open log file %s",
55 setvbuf(f
, NULL
, _IOLBF
, 0);
65 f
= fopen(logFile
->string
, "a");
67 do_log_error(L_ERROR
, errno
, "Couldn't reopen log file %s",
71 setvbuf(f
, NULL
, _IOLBF
, 0);
78 really_do_log(int type
, const char *f
, ...)
83 if(type
& LOGGING_MAX
& logLevel
)
84 really_do_log_v(type
, f
, args
);
89 really_do_log_v(int type
, const char *f
, va_list args
)
91 if(type
& LOGGING_MAX
& logLevel
)
92 vfprintf(logF
, f
, args
);
96 really_do_log_error(int type
, int e
, const char *f
, ...)
100 if(type
& LOGGING_MAX
& logLevel
)
101 really_do_log_error_v(type
, e
, f
, args
);
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
);
111 es
= "Unknown error";
112 vfprintf(logF
, f
, args
);
113 fprintf(logF
, ": %s (%d)\n", es
, e
);
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
);