e6539cf1b2e946751a06ae315d5f822f984290b4
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 #define NO_MEMORY_ALLOCATION
22 /* because the memory allocation functions report errors */
38 void (*fn
)(int pri
, const char *msg
, void *user
);
42 void (*exitfn
)(int) attribute((noreturn
)) = exit
;
45 const char *debug_filename
;
47 struct log_output
*log_default
= &log_stderr
;
49 static const char *debug_only
;
51 /* we might be receiving things in any old encoding, or binary rubbish in no
52 * encoding at all, so escape anything we don't like the look of */
53 static void format(char buffer
[], size_t bufsize
, const char *fmt
, va_list ap
) {
59 if(byte_vsnprintf(t
, sizeof t
, fmt
, ap
) < 0)
60 strcpy(t
, "[byte_vsnprintf failed]");
62 while((ch
= (unsigned char)*p
++)) {
63 if(ch
>= ' ' && ch
<= 126) {
64 if(n
< bufsize
) buffer
[n
++] = ch
;
66 if(n
< bufsize
) buffer
[n
++] = '\\';
67 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 6) & 7);
68 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 3) & 7);
69 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 0) & 7);
78 static void logfp(int pri
, const char *msg
, void *user
) {
80 FILE *fp
= user ? user
: stderr
;
81 /* ...because stderr is not a constant so we can't initialize log_stderr
86 fprintf(fp
, "%s: ", progname
);
89 else if(pri
< LOG_DEBUG
)
93 if(!(debug_only
= getenv("DISORDER_DEBUG_ONLY")))
98 while(!strncmp(p
, "../", 3)) p
+= 3;
99 if(*debug_only
&& strcmp(p
, debug_only
))
101 fprintf(fp
, "%llu.%06lu: %s:%d: ",
102 (unsigned long long)tv
.tv_sec
, (unsigned long)tv
.tv_usec
,
110 static void logsyslog(int pri
, const char *msg
,
111 void attribute((unused
)) *user
) {
113 syslog(pri
, "%s", msg
);
115 syslog(pri
, "%s:%d: %s", debug_filename
, debug_lineno
, msg
);
118 struct log_output log_stderr
= { logfp
, 0 };
119 struct log_output log_syslog
= { logsyslog
, 0 };
121 /* log to all log outputs */
122 static void vlogger(int pri
, const char *fmt
, va_list ap
) {
125 format(buffer
, sizeof buffer
, fmt
, ap
);
126 log_default
->fn(pri
, buffer
, log_default
->user
);
129 /* wrapper for vlogger */
130 static void logger(int pri
, const char *fmt
, ...) {
134 vlogger(pri
, fmt
, ap
);
138 /* internals of fatal/error/info */
139 void elog(int pri
, int errno_value
, const char *fmt
, va_list ap
) {
143 vlogger(pri
, fmt
, ap
);
145 memset(buffer
, 0, sizeof buffer
);
146 byte_vsnprintf(buffer
, sizeof buffer
, fmt
, ap
);
147 buffer
[sizeof buffer
- 1] = 0;
148 logger(pri
, "%s: %s", buffer
, strerror(errno_value
));
152 #define disorder_fatal fatal
153 #define disorder_error error
154 #define disorder_info info
156 /* shared implementation of vararg functions */
157 #include "log-impl.h"
159 void debug(const char *msg
, ...) {
163 vlogger(LOG_DEBUG
, msg
, ap
);
167 void set_progname(char **argv
) {
168 if((progname
= strrchr(argv
[0], '/')))
180 /* arch-tag:78385d5240eab4439cb7eca7dad5154d */