-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.c
220 lines (188 loc) · 3.29 KB
/
logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* $Id$
*
* Logging and error handling for distribute+archive
*
* Shigeya Suzuki, Dec 1993, October 1997
* Copyright(c)1993-1997 Shigeya Suzuki
*/
#include <config.h>
#include <stdio.h>
#include <sysexits.h>
#include <stdlib.h>
#ifdef DEBUGLOG
#include <sys/types.h>
#include <unistd.h>
#endif
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include "cdefs.h"
#ifdef USE_SYSLOG
# ifdef HAVE_SYSLOG_H
# include <syslog.h>
# endif
#else
# define LOG_ERR 1
# define LOG_WARNING 2
# define LOG_INFO 3
#endif
#define LOGBUFSIZE 1024 /* for safety */
#include "logging.h"
#include "memory.h"
/* Global
*/
static int print_error = 0;
static char* logbuf = NULL;
static int logbuf_size = LOGBUFSIZE;
/* Initialization
*/
void
init_log(tag)
char* tag;
{
#ifdef DEBUGLOG
char logfile[80];
extern FILE *debuglog;
#endif
#ifdef USE_SYSLOG
openlog(tag, LOG_PID, SYSLOG_FACILITY);
#endif
#ifdef DEBUGLOG
sprintf(logfile, "/tmp/%s.log", tag);
debuglog = fopen(logfile, "a");
if (debuglog == NULL) {
logandexit(EX_UNAVAILABLE, "can't open debug log", logfile);
}
fprintf(debuglog, "---\n");
fprintf(debuglog, "invoked: pid=%ld\n", (long)getpid());
fflush(debuglog);
#endif
logbuf = xmalloc(LOGBUFSIZE);
logbuf_size = LOGBUFSIZE;
}
/* Toggle print error mode
*/
void
logging_setprinterror(flag)
int flag;
{
print_error = flag;
}
/* Several error handler
*/
void
logerror_buf(pri, prefix)
int pri;
char* prefix;
{
if (print_error) {
if (prefix != NULL)
fputs(prefix, stderr);
fputs(logbuf, stderr);
fputc('\n', stderr);
}
#ifdef LOGDEBUG
{
FILE* fp = fopen("/tmp/distribute.log", "a");
if (fp != NULL) {
if (prefix != NULL)
fputs(prefix, fp);
fputs(logbuf, fp);
fputc('\n', fp);
fclose(fp);
}
}
#endif
#ifdef USE_SYSLOG
syslog(pri, logbuf);
#endif
}
void
#ifdef __STDC__
logerror(char* fmt, ...)
#else
logerror(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_VSNPRINTF
vsnprintf(logbuf, logbuf_size, fmt, ap);
#else
vsprintf(logbuf, fmt, ap);
#endif
va_end(ap);
logerror_buf(LOG_ERR, "Error: ");
}
void
#ifdef __STDC__
logwarn(char* fmt, ...)
#else
logwarn(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_VSNPRINTF
vsnprintf(logbuf, logbuf_size, fmt, ap);
#else
vsprintf(logbuf, fmt, ap);
#endif
va_end(ap);
logerror_buf(LOG_WARNING, "Warning: ");
}
void
#ifdef __STDC__
loginfo(char* fmt, ...)
#else
loginfo(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_VSNPRINTF
vsnprintf(logbuf, logbuf_size, fmt, ap);
#else
vsprintf(logbuf, fmt, ap);
#endif
va_end(ap);
logerror_buf(LOG_INFO, "Info: ");
}
/* log it then exit with error code
*/
void
#ifdef __STDC__
logandexit(int exitcode, char* fmt, ...)
#else
logandexit(exitcode, fmt, va_alist)
int exitcode;
char *fmt;
va_dcl
#endif
{
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_VSNPRINTF
vsnprintf(logbuf, logbuf_size, fmt, ap);
#else
vsprintf(logbuf, fmt, ap);
#endif
va_end(ap);
logerror_buf(NULL);
exit(exitcode);
}
/* Abort with program error
*/
void
programerror()
{
logandexit(EX_UNAVAILABLE, "program error");
}