File: fakelog.c

package info (click to toggle)
ndtpd 3.1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,524 kB
  • ctags: 690
  • sloc: ansic: 8,565; sh: 7,275; perl: 655; makefile: 175
file content (315 lines) | stat: -rw-r--r-- 7,098 bytes parent folder | download | duplicates (5)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 1997, 98, 99, 2000, 01  
 *    Motoyuki Kasahara
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/*
 * This program requires the following Autoconf macros:
 *   AC_C_CONST
 *   AC_TYPE_SIZE_T
 *   AC_HEADER_STDC
 *   AC_CHECK_HEADERS(string.h, memory.h, syslog.h)
 *   AC_CHECK_FUNCS(vsyslog, strerror, syslog)
 *   AC_FUNC_VPRINTF
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <errno.h>

#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
#ifdef __STDC__
#include <stdarg.h>
#else /* not __STDC__ */
#include <varargs.h>
#endif /* not __STDC__ */
#endif /* (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */

#ifndef HAVE_STRERROR
#ifdef __STDC__
char *strerror(int);
#else /* not __STDC__ */
char *strerror();
#endif /* not __STDC__ */
#endif /* HAVE_STRERROR */

#include "fakelog.h"

#undef syslog

/*
 * Internal log priorities.
 */
#define FAKELOG_QUIET		0
#define FAKELOG_EMERG		1
#define FAKELOG_ALERT		2
#define FAKELOG_CRIT		3
#define FAKELOG_ERR		4
#define FAKELOG_WARNING		5
#define FAKELOG_NOTICE		6
#define FAKELOG_INFO		7
#define FAKELOG_DEBUG		8
#define FAKELOG_UNKNOWN		9

/*
 * Log name, mode and level.
 */
static char *log_name = NULL;
static char log_name_buffer[FAKELOG_MAX_LOG_NAME_LENGTH + 1];
static int log_mode = FAKELOG_TO_SYSLOG;
static int log_level = FAKELOG_ERR;

/*
 * Set log name.
 */
void
set_fakelog_name(name)
    const char *name;
{
    if (name == NULL)
        log_name = NULL;
    else {
        strncpy(log_name_buffer, name, FAKELOG_MAX_LOG_NAME_LENGTH);
        log_name_buffer[FAKELOG_MAX_LOG_NAME_LENGTH] = '\0';
        log_name = log_name_buffer;
    }
}


/*
 * Set log level.
 */
void
set_fakelog_mode(mode)
    int mode;
{
    log_mode = mode;
}


/*
 * Set log level.
 */
void
set_fakelog_level(level)
    int level;
{
    /*
     * Convert a syslog priority to a fakelog priority.
     */
    switch (level) {
#ifdef LOG_EMERG
    case LOG_EMERG:
	log_level = FAKELOG_EMERG;
	break;
#endif
#ifdef LOG_ALERT
    case LOG_ALERT:
	log_level = FAKELOG_ALERT;
	break;
#endif
#ifdef LOG_CRIT
    case LOG_CRIT:
	log_level = FAKELOG_CRIT;
	break;
#endif
#ifdef LOG_ERR
    case LOG_ERR:
	log_level = FAKELOG_ERR;
	break;
#endif
#ifdef LOG_WARNING
    case LOG_WARNING:
	log_level = FAKELOG_WARNING;
	break;
#endif
#ifdef LOG_NOTICE
    case LOG_NOTICE:
	log_level = FAKELOG_NOTICE;
	break;
#endif
#ifdef LOG_INFO
    case LOG_INFO:
	log_level = FAKELOG_INFO;
	break;
#endif
#ifdef LOG_DEBUG
    case LOG_DEBUG:
	log_level = FAKELOG_DEBUG;
	break;
#endif
    }
}


/*
 * The function fakes syslog(), and output a message to standard 
 * error, instead of syslog.
 *
 * To use the function, you can fake your sources by the following
 * trick:
 *
 *     #define syslog fakelog
 *
 * Note that this function doesn't support `%m' that syslog() expands
 * into the current error message.
 */
#define BUFFER_SIZE    1024

#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
#ifdef __STDC__
void
fakelog(int priority, const char *message, ...)
#else /* not __STDC__ */
void
fakelog(priority, message, va_alist)
    int priority;
    char *message;
    va_dcl 
#endif /* not __STDC__ */
#else /* not (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */
void
fakelog(priority, message, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
    int priority;
    char *message;
    char *a0, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8, *a9;
#endif /* not (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */
{

#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
    va_list ap;
#endif
    int log_flag;

    /*
     * Convert `priority'.
     */
    switch (priority) {
#ifdef LOG_EMERG
    case LOG_EMERG:
	log_flag = (FAKELOG_EMERG <= log_level);
	break;
#endif
#ifdef LOG_ALERT
    case LOG_ALERT:
	log_flag = (FAKELOG_ALERT <= log_level);
	break;
#endif
#ifdef LOG_CRIT
    case LOG_CRIT:
	log_flag = (FAKELOG_CRIT <= log_level);
	break;
#endif
#ifdef LOG_ERR
    case LOG_ERR:
	log_flag = (FAKELOG_ERR <= log_level);
	break;
#endif
#ifdef LOG_WARNING
    case LOG_WARNING:
	log_flag = (FAKELOG_WARNING <= log_level);
	break;
#endif
#ifdef LOG_NOTICE
    case LOG_NOTICE:
	log_flag = (FAKELOG_NOTICE <= log_level);
	break;
#endif
#ifdef LOG_INFO
    case LOG_INFO:
	log_flag = (FAKELOG_INFO <= log_level);
	break;
#endif
#ifdef LOG_DEBUG
    case LOG_DEBUG:
	log_flag = (FAKELOG_DEBUG <= log_level);
	break;
#endif
    default:
	log_flag = 0;
    }

    /*
     * Start to use the variable length arguments.
     */
#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
#ifdef __STDC__
    va_start(ap, message);
#else /* not __STDC__ */
    va_start(ap);
#endif /* not __STDC__ */
#endif /* (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */

    /*
     * Output the message to syslog.
     */
    if (log_mode == FAKELOG_TO_SYSLOG || log_mode == FAKELOG_TO_BOTH) {
#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
	vsyslog(priority, message, ap);
#else /* not (defined(HAVE_VPRINTF) || ... */
#ifdef HAVE_SYSLOG
	syslog(priority, message, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif /* HAVE_SYSLOG */
#endif /* not (defined(HAVE_VPRINTF) || ... */
    }

    /*
     * Output the message to standard error.
     */
    if ((log_mode == FAKELOG_TO_STDERR || log_mode == FAKELOG_TO_BOTH)
	&& log_flag) {
	if (log_name != NULL)
	    fprintf(stderr, "%s: ", log_name);

#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
#ifdef HAVE_VPRINTF
	vfprintf(stderr, message, ap);
#else /* not HAVE_VPRINTF */
#ifdef HAVE_DOPRNT
	_doprnt(message, &ap, stderr);
#endif /* not HAVE_DOPRNT */
#endif /* not HAVE_VPRINTF */
#else /* not (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */
	fprintf(stderr, message, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif /* not (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */

	fputc('\n', stderr);
	fflush(stderr);
    }

    /*
     * End to use the variable length arguments.
     */
#if (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && defined(HAVE_VSYSLOG)
    va_end(ap);
#endif /* not (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)) && ... */
}