chiark / gitweb /
Remove arch tags throughout
[disorder] / lib / log.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21#define NO_MEMORY_ALLOCATION
22/* because the memory allocation functions report errors */
23
24#include <config.h>
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <syslog.h>
31#include <sys/time.h>
32
33#include "log.h"
34#include "disorder.h"
35#include "printf.h"
36
37struct log_output {
38 void (*fn)(int pri, const char *msg, void *user);
39 void *user;
40};
41
42void (*exitfn)(int) attribute((noreturn)) = exit;
43int debugging;
44const char *progname;
45const char *debug_filename;
46int debug_lineno;
47struct log_output *log_default = &log_stderr;
48
49static const char *debug_only;
50
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 */
53static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
54 char t[1024];
55 const char *p;
56 int ch;
57 size_t n = 0;
58
59 if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0)
60 strcpy(t, "[byte_vsnprintf failed]");
61 p = t;
62 while((ch = (unsigned char)*p++)) {
63 if(ch >= ' ' && ch <= 126) {
64 if(n < bufsize) buffer[n++] = ch;
65 } else {
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);
70 }
71 }
72 if(n >= bufsize)
73 n = bufsize - 1;
74 buffer[n] = 0;
75}
76
77/* log to a file */
78static void logfp(int pri, const char *msg, void *user) {
79 struct timeval tv;
80 FILE *fp = user ? user : stderr;
81 /* ...because stderr is not a constant so we can't initialize log_stderr
82 * sanely */
83 const char *p;
84
85 if(progname)
86 fprintf(fp, "%s: ", progname);
87 if(pri <= LOG_ERR)
88 fputs("ERROR: ", fp);
89 else if(pri < LOG_DEBUG)
90 fputs("INFO: ", fp);
91 else {
92 if(!debug_only) {
93 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
94 debug_only = "";
95 }
96 gettimeofday(&tv, 0);
97 p = debug_filename;
98 while(!strncmp(p, "../", 3)) p += 3;
99 if(*debug_only && strcmp(p, debug_only))
100 return;
101 fprintf(fp, "%llu.%06lu: %s:%d: ",
102 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
103 p, debug_lineno);
104 }
105 fputs(msg, fp);
106 fputc('\n', fp);
107}
108
109/* log to syslog */
110static void logsyslog(int pri, const char *msg,
111 void attribute((unused)) *user) {
112 if(pri < LOG_DEBUG)
113 syslog(pri, "%s", msg);
114 else
115 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
116}
117
118struct log_output log_stderr = { logfp, 0 };
119struct log_output log_syslog = { logsyslog, 0 };
120
121/* log to all log outputs */
122static void vlogger(int pri, const char *fmt, va_list ap) {
123 char buffer[1024];
124
125 format(buffer, sizeof buffer, fmt, ap);
126 log_default->fn(pri, buffer, log_default->user);
127}
128
129/* wrapper for vlogger */
130static void logger(int pri, const char *fmt, ...) {
131 va_list ap;
132
133 va_start(ap, fmt);
134 vlogger(pri, fmt, ap);
135 va_end(ap);
136}
137
138/* internals of fatal/error/info */
139void elog(int pri, int errno_value, const char *fmt, va_list ap) {
140 char buffer[1024];
141
142 if(errno_value == 0)
143 vlogger(pri, fmt, ap);
144 else {
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));
149 }
150}
151
152#define disorder_fatal fatal
153#define disorder_error error
154#define disorder_info info
155
156/* shared implementation of vararg functions */
157#include "log-impl.h"
158
159void debug(const char *msg, ...) {
160 va_list ap;
161
162 va_start(ap, msg);
163 vlogger(LOG_DEBUG, msg, ap);
164 va_end(ap);
165}
166
167void set_progname(char **argv) {
168 if((progname = strrchr(argv[0], '/')))
169 ++progname;
170 else
171 progname = argv[0];
172}
173
174/*
175Local Variables:
176c-basic-offset:2
177comment-column:40
178End:
179*/