nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

splogger.c (1623B)


      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,NULL);
     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 int main(int argc, char **argv)
     49 {
     50   char ch;
     51 
     52   if (argc > 1)
     53     if (argc > 2) {
     54       unsigned long facility;
     55       scan_ulong(argv[2],&facility);
     56       openlog(argv[1],0,facility << 3);
     57     }
     58     else
     59       openlog(argv[1],0,LOG_MAIL);
     60   else
     61     openlog("splogger",0,LOG_MAIL);
     62 
     63   for (;;) {
     64     if (substdio_get(subfdin,&ch,1) < 1) _exit(0);
     65     if (ch == '\n') { flush(); flagcont = 0; continue; }
     66     if (bufpos == sizeof(buf) - 1) flush();
     67     if ((ch < 32) || (ch > 126)) ch = '?'; /* logger truncates at 0; GPACIC */
     68     buf[bufpos++] = ch;
     69   }
     70 }