1: #ifndef BSD43
   2: #ifndef lint
   3: static char SccsId[] =  "@(#)syslog.c	4.1 (Berkeley) 5/27/83";
   4: #endif
   5: 
   6: /*
   7:  * SYSLOG -- print message on log file
   8:  *
   9:  * This routine looks a lot like printf, except that it
  10:  * outputs to the log file instead of the standard output.
  11:  * Also, it prints the module name in front of lines,
  12:  * and has some other formatting types (or will sometime).
  13:  * Also, it adds a newline on the end of messages.
  14:  *
  15:  * The output of this routine is intended to be read by
  16:  * /etc/syslog, which will add timestamps.
  17:  */
  18: #include <sys/types.h>
  19: #include <sys/socket.h>
  20: #include <netinet/in.h>
  21: 
  22: #include <syslog.h>
  23: #include <netdb.h>
  24: 
  25: #define MAXLINE 1024        /* max message size */
  26: #define BUFSLOP 20      /* space to allow for "extra stuff" */
  27: #define NULL    0       /* manifest */
  28: 
  29: int LogFile = -1;       /* fd for log */
  30: int LogStat = 0;        /* status bits, set by initlog */
  31: char    *LogTag = NULL;     /* string to tag the entry with */
  32: int LogMask = LOG_DEBUG;    /* lowest priority to be logged */
  33: 
  34: struct sockaddr_in SyslogAddr;
  35: static char *SyslogHost = LOG_HOST;
  36: 
  37: extern  int errno, sys_nerr;
  38: extern  char *sys_errlist[];
  39: 
  40: syslog(pri, fmt, p0, p1, p2, p3, p4)
  41:     int pri;
  42:     char *fmt;
  43: {
  44:     char buf[MAXLINE+BUFSLOP], outline[MAXLINE + 1];
  45:     register char *b, *f;
  46: 
  47:     if (LogFile < 0)
  48:         openlog(0, 0);
  49:     /* see if we should just throw out this message */
  50:     if (pri > LogMask)
  51:         return;
  52:     for (b = buf, f = fmt; f && *f; b = buf) {
  53:         register char c;
  54: 
  55:         if (pri > 0 && (LogStat & LOG_COOLIT) == 0) {
  56:             sprintf(b, "<%d>", pri);
  57:             b += strlen(b);
  58:         }
  59:         if (LogStat & LOG_PID) {
  60:             sprintf(b, "%d ", getpid());
  61:             b += strlen(b);
  62:         }
  63:         if (LogTag) {
  64:             sprintf(b, "%s: ", LogTag);
  65:             b += strlen(b);
  66:         }
  67:         while ((c = *f++) != '\0' && c != '\n' && b < buf + MAXLINE) {
  68:             if (c != '%') {
  69:                 *b++ = c;
  70:                 continue;
  71:             }
  72:             c = *f++;
  73:             if (c != 'm') {
  74: #ifndef UCI
  75:                 *b++ = '%', *b++ = c, *b++ = '\0';
  76: #else   UCI
  77:                 *b++ = '%', *b++ = c;
  78: #endif	UCI
  79:                 continue;
  80:             }
  81:             if ((unsigned)errno > sys_nerr)
  82:                 sprintf(b, "error %d", errno);
  83:             else
  84:                 strcat(b, sys_errlist[errno]);
  85:             b += strlen(b);
  86:         }
  87:         if (c == '\0')
  88:             f--;
  89:         *b++ = '\n', *b = '\0';
  90:         sprintf(outline, buf, p0, p1, p2, p3, p4);
  91:         errno = 0;
  92:         if (LogStat & LOG_DGRAM)
  93:             (void) sendto(LogFile, outline, strlen(outline), 0,
  94:                    &SyslogAddr, sizeof SyslogAddr);
  95:         else
  96:             (void) write(LogFile, outline, strlen(outline));
  97:         if (errno)
  98:             perror("syslog: sendto");
  99:     }
 100: }
 101: 
 102: /*
 103:  * OPENLOG -- open system log
 104:  */
 105: openlog(ident, logstat)
 106:     char *ident;
 107:     int logstat;
 108: {
 109:     struct servent *sp;
 110:     struct hostent *hp;
 111: 
 112:     LogTag = ident;
 113:     LogStat = logstat;
 114:     if (LogFile >= 0)
 115:         return;
 116:     sp = getservbyname("syslog", "udp");
 117:     hp = gethostbyname(SyslogHost);
 118:     if (sp == NULL || hp == NULL)
 119:         goto bad;
 120:     LogFile = socket(AF_INET, SOCK_DGRAM, 0);
 121:     if (LogFile < 0) {
 122:         perror("syslog: socket");
 123:         goto bad;
 124:     }
 125:     bzero(&SyslogAddr, sizeof SyslogAddr);
 126:     SyslogAddr.sin_family = hp->h_addrtype;
 127:     bcopy(hp->h_addr, (char *)&SyslogAddr.sin_addr, hp->h_length);
 128:     SyslogAddr.sin_port = sp->s_port;
 129:     LogStat |= LOG_DGRAM;
 130:     return;
 131: bad:
 132:     LogStat |= LOG_COOLIT;
 133:     LogStat &= ~LOG_DGRAM;
 134:     LogMask = LOG_CRIT;
 135:     LogFile = open("/dev/console", 1);
 136:     if (LogFile < 0) {
 137:         perror("syslog: /dev/console");
 138:         LogFile = 2;
 139:     }
 140: }
 141: 
 142: /*
 143:  * CLOSELOG -- close the system log
 144:  */
 145: closelog()
 146: {
 147: 
 148:     (void) close(LogFile);
 149:     LogFile = -1;
 150: }
 151: #endif	not BSD43

Defined functions

openlog defined in line 105; used 7 times
syslog defined in line 40; used 5 times

Defined variables

LogFile defined in line 29; used 11 times
LogMask defined in line 32; used 2 times
LogStat defined in line 30; used 7 times
LogTag defined in line 31; used 3 times
SccsId defined in line 3; never used
SyslogAddr defined in line 34; used 7 times
SyslogHost defined in line 35; used 1 times

Defined macros

BUFSLOP defined in line 26; used 1 times
  • in line 44
MAXLINE defined in line 25; used 3 times
NULL defined in line 27; used 3 times
Last modified: 1986-04-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1139
Valid CSS Valid XHTML 1.0 Strict