chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / splogger.c
1 #include <sys/types.h>
2 #include <sys/time.h>
3 #include <syslog.h>
4 #include "error.h"
5 #include "substdio.h"
6 #include "subfd.h"
7 #include "exit.h"
8 #include "str.h"
9 #include "scan.h"
10 #include "fmt.h"
11
12 char buf[800]; /* syslog truncates long lines (or crashes); GPACIC */
13 int bufpos = 0; /* 0 <= bufpos < sizeof(buf) */
14 int flagcont = 0;
15 int priority; /* defined if flagcont */
16 char stamp[FMT_ULONG + FMT_ULONG + 3]; /* defined if flagcont */
17
18 void stamp_make()
19 {
20   struct timeval tv;
21   char *s;
22   gettimeofday(&tv,(struct timezone *) 0);
23   s = stamp;
24   s += fmt_ulong(s,(unsigned long) tv.tv_sec);
25   *s++ = '.';
26   s += fmt_uint0(s,(unsigned int) tv.tv_usec,6);
27   *s = 0;
28 }
29
30 void flush()
31 {
32   if (bufpos) {
33     buf[bufpos] = 0;
34     if (flagcont)
35       syslog(priority,"%s+%s",stamp,buf); /* logger folds invisibly; GPACIC */
36     else {
37       stamp_make();
38       priority = LOG_INFO;
39       if (str_start(buf,"warning:")) priority = LOG_WARNING;
40       if (str_start(buf,"alert:")) priority = LOG_ALERT;
41       syslog(priority,"%s %s",stamp,buf);
42       flagcont = 1;
43     }
44   }
45   bufpos = 0;
46 }
47
48 void main(argc,argv)
49 int argc;
50 char **argv;
51 {
52   char ch;
53
54   if (argv[1])
55     if (argv[2]) {
56       unsigned long facility;
57       scan_ulong(argv[2],&facility);
58       openlog(argv[1],0,facility << 3);
59     }
60     else
61       openlog(argv[1],0,LOG_MAIL);
62   else
63     openlog("splogger",0,LOG_MAIL);
64
65   for (;;) {
66     if (substdio_get(subfdin,&ch,1) < 1) _exit(0);
67     if (ch == '\n') { flush(); flagcont = 0; continue; }
68     if (bufpos == sizeof(buf) - 1) flush();
69     if ((ch < 32) || (ch > 126)) ch = '?'; /* logger truncates at 0; GPACIC */
70     buf[bufpos++] = ch;
71   }
72 }