File: log.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (418 lines) | stat: -rw-r--r-- 7,505 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: log.c,v 2.3 1997/05/06 15:38:53 papowell Exp papowell $
 */

#include "portable.h"
#include "common.h"

#if !defined(ultrix) && defined(HAVE_SYSLOG_H)
# include <syslog.h>
#endif
#if defined(HAVE_SYS_SYSLOG_H)
# include <sys/syslog.h>
#endif

#define PAIR(X) { #X, X }

struct keys {
	char *name;
	int value;
} log_level[] = {
{"EMERG",LOG_EMERG},
{"ALERT",LOG_ALERT},
{"CRIT",LOG_CRIT},
{"ERR",LOG_ERR},
{"WARNING",LOG_WARNING},
{"NOTICE",LOG_NOTICE},
{"INFO",LOG_INFO},
{"DEBUG",LOG_DEBUG},
{0,0}
};

static char msg_buf[1024];
extern int show_ctrl;

void init_pid()
{
	int len;
	plp_snprintf(msg_buf,sizeof(msg_buf), "%s ", name );
	if( debug > 2 ){
		len = strlen( msg_buf );
		plp_snprintf(msg_buf+len,sizeof(msg_buf)-len,
			"pid [%d] ", (int)(getpid()) );
	}
}

static void logbackend()
{
	int len, original_len;
	int i;
	char *s;
	original_len = len = strlen( msg_buf );
	len = strlen( msg_buf );
	(void)plp_snprintf(msg_buf+len, sizeof(msg_buf)-len,
		" at %s\n", Time_str());
	(void)fputs(msg_buf, stderr );
	(void)fflush(stderr);
	setstatus(msg_buf);

	/* find log level */
	for( i = 0; (s = log_level[i].name) && strstr(msg_buf, s ) == 0; ++i );
	if( s ){
		msg_buf[original_len] = 0;
		if( printer && *printer ){
			(void)plp_snprintf( msg_buf+len, sizeof(msg_buf)-len,
				" PRINTER=%s\n", printer );
		}
		syslog(log_level[i].value,"%s",msg_buf);
	}
}


/* VARARGS2 */
#ifdef HAVE_STDARGS
void log (int lvl, char *msg,...)
#else
void log (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
    int lvl;
    char *msg;
#endif
	int len;
    VA_LOCAL_DECL
    VA_START (msg);
    VA_SHIFT (lvl, int);
    VA_SHIFT (msg, char *);

	if ( lvl > debug )	return;
	lvl = show_ctrl;
	show_ctrl = 1;
	init_pid();
	len = strlen(msg_buf);
	(void)vplp_snprintf(msg_buf+len, sizeof(msg_buf)-len, msg, ap );
	logbackend();
	show_ctrl = lvl;
	VA_END;
}

/* VARARGS1 */
#ifdef HAVE_STDARGS
void fatal ( char *msg,...)
#else
void fatal (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
    int lvl;
    char *msg;
#endif
	int len;
	int lvl;
    VA_LOCAL_DECL
    VA_START (msg);
    VA_SHIFT (msg, char *);

	lvl = show_ctrl;
	show_ctrl = 1;
	init_pid();
	len = strlen(msg_buf);
	(void)vplp_snprintf(msg_buf+len, sizeof(msg_buf)-len, msg, ap );
	logbackend();
	show_ctrl = lvl;
	VA_END;
	fexit(errorcode);
}


/* VARARGS2 */
#ifdef HAVE_STDARGS
void logerr (int lvl, char *msg,...)
#else
void logerr (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
    int lvl;
    char *msg;
#endif
    VA_LOCAL_DECL
	int err = errno;
	int len;

    VA_START (msg);
    VA_SHIFT (lvl, int);
    VA_SHIFT (msg, char *);

	if ( lvl > debug )	return;
	lvl = show_ctrl;
	show_ctrl = 1;

	init_pid();
	len = strlen(msg_buf);
	(void)vplp_snprintf(msg_buf+len, sizeof(msg_buf)-len, msg, ap );
	len = strlen(msg_buf);
	plp_snprintf( msg_buf+len, sizeof(msg_buf)-len, " - %s", Errormsg( err ) );
	logbackend();
	show_ctrl = lvl;
	VA_END;
}


/* VARARGS2 */
#ifdef HAVE_STDARGS
void logerr_die (int lvl, char *msg,...)
#else
void logerr_die (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
    int lvl;
    char *msg;
#endif
    VA_LOCAL_DECL
    int err = errno;
    int len;

    VA_START (msg);
    VA_SHIFT (lvl, int);
    VA_SHIFT (msg, char *);

    if ( lvl > debug )  return;
	lvl = show_ctrl;
	show_ctrl = 1;

	init_pid();
	len = strlen(msg_buf);
	(void)vplp_snprintf(msg_buf+len, sizeof(msg_buf)-len, msg, ap );
	len = strlen(msg_buf);
	plp_snprintf( msg_buf+len, sizeof(msg_buf)-len, " - %s", Errormsg( err ) );
	logbackend();
    VA_END;
	show_ctrl = lvl;
	fexit(errorcode);
}


char * Time_str()
{
	time_t tvec;		/* time */
	char *ctime();
	static char s[40];

	(void)time(&tvec);
	(void)strcpy(s,ctime(&tvec));
	(void)strcpy(s+20,s+24);
	s[strlen(s)-1] = 0;
	return( s+4 );
}

/****************************************************************************
 * char *Errormsg( int err )
 *  returns a printable form of the
 *  errormessage corresponding to the valie of err.
 *  This is the poor man's version of sperror(), not available on all systems
 *  Patrick Powell Tue Apr 11 08:05:05 PDT 1995
 ****************************************************************************/
/****************************************************************************/
#if !defined(HAVE_STRERROR)

# if defined(HAVE_SYS_NERR)
#  if !defined(HAVE_SYS_NERR_DEF)
     extern int sys_nerr;
#  endif
#  define num_errors    (sys_nerr)
# else
#  define num_errors    (-1)            /* always use "errno=%d" */
# endif

# if defined(HAVE_SYS_ERRLIST)
#  if !defined(HAVE_SYS_ERRLIST_DEF)
     extern const char *const sys_errlist[];
#  endif
# else
#  undef  num_errors
#  define num_errors   (-1)            /* always use "errno=%d" */
# endif

#endif

const char * Errormsg ( int err )
{
    const char *cp;

#if defined(HAVE_STRERROR)
	cp = strerror(err);
#else
# if defined(HAVE_SYS_ERRLIST)
    if (err >= 0 && err < num_errors) {
		cp = sys_errlist[err];
    } else
# endif
	{
		static char msgbuf[32];     /* holds "errno=%d". */
		(void) plp_snprintf (msgbuf, sizeof(msgbuf), "errno=%d", err);
		cp = msgbuf;
    }
#endif
    return (cp);
}

/***************************************************************************
 * char *Sigstr(n)
 * Return a printable form the the signal
 ***************************************************************************/

#ifndef HAVE_SYS_SIGLIST

#define PAIR(X) { #X , X }

static struct signame {
    char *str;
    int value;
} signals[] = {
{ "NO SIGNAL", 0 },
#ifdef SIGHUP
PAIR(SIGHUP),
#endif
#ifdef SIGINT
PAIR(SIGINT),
#endif
#ifdef SIGQUIT
PAIR(SIGQUIT),
#endif
#ifdef SIGILL
PAIR(SIGILL),
#endif
#ifdef SIGTRAP
PAIR(SIGTRAP),
#endif
#ifdef SIGIOT
PAIR(SIGIOT),
#endif
#ifdef SIGABRT
PAIR(SIGABRT),
#endif
#ifdef SIGEMT
PAIR(SIGEMT),
#endif
#ifdef SIGFPE
PAIR(SIGFPE),
#endif
#ifdef SIGKILL
PAIR(SIGKILL),
#endif
#ifdef SIGBUS
PAIR(SIGBUS),
#endif
#ifdef SIGSEGV
PAIR(SIGSEGV),
#endif
#ifdef SIGSYS
PAIR(SIGSYS),
#endif
#ifdef SIGPIPE
PAIR(SIGPIPE),
#endif
#ifdef SIGALRM
PAIR(SIGALRM),
#endif
#ifdef SIGTERM
PAIR(SIGTERM),
#endif
#ifdef SIGURG
PAIR(SIGURG),
#endif
#ifdef SIGSTOP
PAIR(SIGSTOP),
#endif
#ifdef SIGTSTP
PAIR(SIGTSTP),
#endif
#ifdef SIGCONT
PAIR(SIGCONT),
#endif
#ifdef SIGCHLD
PAIR(SIGCHLD),
#endif
#ifdef SIGCLD
PAIR(SIGCLD),
#endif
#ifdef SIGTTIN
PAIR(SIGTTIN),
#endif
#ifdef SIGTTOU
PAIR(SIGTTOU),
#endif
#ifdef SIGIO
PAIR(SIGIO),
#endif
#ifdef SIGPOLL
PAIR(SIGPOLL),
#endif
#ifdef SIGXCPU
PAIR(SIGXCPU),
#endif
#ifdef SIGXFSZ
PAIR(SIGXFSZ),
#endif
#ifdef SIGVTALRM
PAIR(SIGVTALRM),
#endif
#ifdef SIGPROF
PAIR(SIGPROF),
#endif
#ifdef SIGWINCH
PAIR(SIGWINCH),
#endif
#ifdef SIGLOST
PAIR(SIGLOST),
#endif
#ifdef SIGUSR1
PAIR(SIGUSR1),
#endif
#ifdef SIGUSR2
PAIR(SIGUSR2),
#endif
{0,0}
    /* that's all */
};

#else /* HAVE_SYS_SIGLIST */
# ifndef HAVE_SYS_SIGLIST_DEF
   extern const char *sys_siglist[];
# endif
#endif

#ifndef NSIG
# define  NSIG 32
#endif

const char *Sigstr (int n)
{
    static char buf[40];
	const char *s = 0;

#ifdef HAVE_SYS_SIGLIST
    if (n < NSIG && n >= 0) {
		s = sys_siglist[n];
	}
#else
	int i;

	for( i = 0; signals[i].str && signals[i].value != n; ++i );
	s = signals[i].str;
#endif
	if( s == 0 ){
		s = buf;
		(void) plp_snprintf (buf, sizeof(buf), "signal %d", n);
	}
    return(s);
}