home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / gizmo / gizmo_syslog.c < prev    next >
C/C++ Source or Header  |  1989-09-18  |  4KB  |  196 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. /*
  8.  * SYSLOG -- print message on log file
  9.  *
  10.  * This routine looks a lot like printf, except that it
  11.  * outputs to the log file instead of the standard output.
  12.  * Also:
  13.  *    adds a timestamp,
  14.  *    prints the module name in front of the message,
  15.  *    has some other formatting types (or will sometime),
  16.  *    adds a newline on the end of the message.
  17.  *
  18.  * The output of this routine is intended to be read by /etc/syslogd.
  19.  *
  20.  * Author: Eric Allman
  21.  * Modified to use UNIX domain IPC by Ralph Campbell
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <strings.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/file.h>
  29. #include <sys/time.h>
  30. #include <netinet/in.h>
  31. #include "gizmo_syslog.h"
  32.  
  33. /*
  34.  * Port number for syslogd.  For use by the gizmo's internal syslog
  35.  * routines.
  36.  */
  37. #define    IPPORT_SYSLOG        514
  38.  
  39. #define    MAXLINE    1024            /* max message size */
  40.  
  41. #define PRIMASK(p)    (1 << ((p) & LOG_PRIMASK))
  42. #define PRIFAC(p)    (((p) & LOG_FACMASK) >> 3)
  43. #define IMPORTANT     LOG_ERR
  44.  
  45. static int    LogFile = -1;        /* fd for log */
  46. static int    LogStat    = 0;        /* status bits, set by openlog() */
  47. static char    *LogTag = "syslog";    /* string to tag the entry with */
  48. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  49. static int    LogFacility = LOG_USER;    /* default facility code */
  50. static int    LogOpenCalled = 0;    /* set to 1 when we should be open */
  51.  
  52. static struct sockaddr_in SyslogAddr;    /* AF_INET address of local logger */
  53.  
  54. extern    int errno, sys_nerr;
  55. extern    char *sys_errlist[];
  56.  
  57. syslog(pri, fmt, p0, p1, p2, p3, p4)
  58.     int pri;
  59.     char *fmt;
  60. {
  61.     char buf[MAXLINE + 1], outline[MAXLINE + 1];
  62.     register char *b, *f, *o;
  63.     register int c;
  64.     char *termline;
  65.     struct timeval tv;
  66.     int olderrno = errno;
  67.  
  68.     /* see if we should just throw out this message */
  69.     if (pri <= 0 || PRIFAC(pri) >= LOG_NFACILITIES
  70.         || (PRIMASK(pri) & LogMask) == 0)
  71.         return;
  72.     if (LogFile < 0)
  73.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  74.  
  75.     /* set default facility if none specified */
  76.     if ((pri & LOG_FACMASK) == 0)
  77.         pri |= LogFacility;
  78.  
  79.     /* build the message */
  80.     o = outline;
  81.     sprintf(o, "<%d>", pri);
  82.     o += strlen(o);
  83.     termline = o;
  84.     gettimeofday(&tv, (struct timezone *)0);
  85.     sprintf(o, "%.15s ", ctime(&tv.tv_sec) + 4);
  86.     o += strlen(o);
  87.     if (LogTag) {
  88.         strcpy(o, LogTag);
  89.         o += strlen(o);
  90.     }
  91. #ifdef notdef
  92.     if (LogStat & LOG_PID) {
  93.         sprintf(o, "[%d]", getpid());
  94.         o += strlen(o);
  95.     }
  96. #endif
  97.     if (LogTag) {
  98.         strcpy(o, ": ");
  99.         o += 2;
  100.     }
  101.  
  102.     b = buf;
  103.     f = fmt;
  104.     while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
  105.         if (c != '%') {
  106.             *b++ = c;
  107.             continue;
  108.         }
  109.         if ((c = *f++) != 'm') {
  110.             *b++ = '%';
  111.             *b++ = c;
  112.             continue;
  113.         }
  114.         if ((unsigned)olderrno > sys_nerr)
  115.             sprintf(b, "error %d", olderrno);
  116.         else
  117.             strcpy(b, sys_errlist[olderrno]);
  118.         b += strlen(b);
  119.     }
  120.     *b++ = '\n';
  121.     *b = '\0';
  122.     sprintf(o, buf, p0, p1, p2, p3, p4);
  123.     c = strlen(outline);
  124.     if (c > MAXLINE)
  125.         c = MAXLINE;
  126.  
  127.     /* output the message to the local logger */
  128.     if (SyslogAddr.sin_addr.s_addr != INADDR_ANY &&
  129.         sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
  130.         return;
  131. #ifdef notdef
  132.     if (!(LogStat & LOG_CONS))
  133.         return;
  134. #endif
  135.     (void) write(2, termline, strlen(termline));
  136. }
  137.  
  138. /*
  139.  * OPENLOG -- open system log
  140.  */
  141.  
  142. openlog(ident, logstat, logfac)
  143.     char *ident;
  144.     int logstat, logfac;
  145. {
  146.     if (ident != NULL)
  147.         LogTag = ident;
  148.     LogStat = logstat;
  149.     if (logfac != 0)
  150.         LogFacility = logfac & LOG_FACMASK;
  151.     if (LogFile >= 0)
  152.         return;
  153.     if (LogStat & LOG_NDELAY) {
  154.         LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
  155.         fcntl(LogFile, F_SETFD, 1);
  156.     }
  157. }
  158.  
  159. /*
  160.  * CLOSELOG -- close the system log
  161.  */
  162.  
  163. closelog()
  164. {
  165.  
  166.     (void) close(LogFile);
  167.     LogFile = -1;
  168. }
  169.  
  170. /*
  171.  * SETLOGMASK -- set the log mask level
  172.  */
  173. setlogmask(pmask)
  174.     int pmask;
  175. {
  176.     int omask;
  177.  
  178.     omask = LogMask;
  179.     if (pmask != 0)
  180.         LogMask = pmask;
  181.     return (omask);
  182. }
  183.  
  184.  
  185. /*
  186.  * setloghost - set the address of the host to log to
  187.  */
  188. setloghost(addr)
  189.     struct sockaddr_in *addr;
  190. {
  191.     bzero(&SyslogAddr, sizeof SyslogAddr);
  192.     SyslogAddr.sin_family = AF_INET;
  193.     SyslogAddr.sin_port = htons(IPPORT_SYSLOG);
  194.     SyslogAddr.sin_addr = addr->sin_addr;
  195. }
  196.