home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / syslog.zip / syslog.c < prev    next >
Text File  |  1994-11-28  |  6KB  |  188 lines

  1. /*  
  2. **  Modified by Jochen Friedrich <jochen@audio.ruessel.sub.org> for OS/2.
  3. */
  4. /*
  5.  * Copyright (c) 1983, 1988 Regents of the University of California.
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted provided
  9.  * that: (1) source distributions retain this entire copyright notice and
  10.  * comment, and (2) distributions including binaries display the following
  11.  * acknowledgement:  ``This product includes software developed by the
  12.  * University of California, Berkeley and its contributors'' in the
  13.  * documentation or other materials provided with the distribution and in
  14.  * all advertising materials mentioning features or use of this software.
  15.  * Neither the name of the University nor the names of its contributors may
  16.  * be used to endorse or promote products derived from this software without
  17.  * specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. /*
  24.  * SYSLOG -- print message on log file
  25.  *
  26.  * This routine looks a lot like printf, except that it outputs to the
  27.  * log file instead of the standard output.  Also:
  28.  *      adds a timestamp,
  29.  *      prints the module name in front of the message,
  30.  *      has some other formatting types (or will sometime),
  31.  *      adds a newline on the end of the message.
  32.  *
  33.  * The output of this routine is intended to be read by syslogd(8).
  34.  *
  35.  * Author: Eric Allman
  36.  * Modified to use UNIX domain IPC by Ralph Campbell
  37.  */
  38.  
  39. #include <os2.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <fcntl.h>
  43. #include <netinet/in.h>
  44. #include <stdarg.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include <string.h>
  48. #include <time.h>
  49. #include <process.h>
  50. #include <errno.h>
  51. #include <utils.h>
  52. #include "syslog.h"
  53.  
  54. static int      LogFile = -1;           /* fd for log */
  55. static int      connected;              /* have done connect */
  56. static int      LogStat = 0;            /* status bits, set by openlog() */
  57. static char     *LogTag = "syslog";     /* string to tag the entry with */
  58. static int      LogFacility = LOG_USER; /* default facility code */
  59.  
  60. void syslog(int pri, char *fmt, ...)
  61. {
  62.         va_list ap;
  63.  
  64.         va_start(ap,fmt);
  65.         vsyslog(pri, fmt, ap);
  66.         va_end(ap);
  67. }
  68.  
  69. void vsyslog(int pri, char *fmt, va_list ap)
  70. {
  71.         extern int errno;
  72.         register int cnt;
  73.         register char *p;
  74.         time_t now;
  75.         int fd, saved_errno;
  76.         char tbuf[2048], fmt_cpy[1024], *stdp;
  77.  
  78.         saved_errno = errno;
  79.  
  80.         /* see if we should just throw out this message */
  81.         if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  82.                 return;
  83.         if (LogFile < 0 || !connected)
  84.                 openlog(LogTag, LogStat | LOG_NDELAY, 0);
  85.  
  86.         /* set default facility if none specified */
  87.         if ((pri & LOG_FACMASK) == 0)
  88.                 pri |= LogFacility;
  89.  
  90.         /* build the message */
  91.         (void)time(&now);
  92.         (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  93.         for (p = tbuf; *p; ++p);
  94.         if (LogStat & LOG_PERROR)
  95.                 stdp = p;
  96.         if (LogTag) {
  97.                 (void)strcpy(p, LogTag);
  98.                 for (; *p; ++p);
  99.         }
  100.         if (LogStat & LOG_PID) {
  101.                 (void)sprintf(p, "[%d]", getpid() );
  102.                 for (; *p; ++p);
  103.         }
  104.         if (LogTag) {
  105.                 *p++ = ':';
  106.                 *p++ = ' ';
  107.         }
  108.  
  109.         /* substitute error message for %m */
  110.         {
  111.                 register char ch, *t1, *t2;
  112.  
  113.                 for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  114.                         if (ch == '%' && fmt[1] == 'm') {
  115.                                 ++fmt;
  116.                                 for (t2 = strerror(saved_errno);
  117.                                     *t1 = *t2++; ++t1);
  118.                         }
  119.                         else
  120.                                 *t1++ = ch;
  121.                 *t1 = '\0';
  122.         }
  123.  
  124.         (void)vsprintf(p, fmt_cpy, ap);
  125.  
  126.         cnt = strlen(tbuf);
  127.  
  128.         /* output to stderr if requested */
  129.         if (LogStat & LOG_PERROR) {
  130.                 write(2, tbuf,cnt);
  131.                 write(2,"\r\n",2);
  132.         }
  133.  
  134.         /* output the message to the local logger */
  135.         if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS))
  136.                 return;
  137.  
  138. }
  139.  
  140. static struct sockaddr_in SyslogAddr;   /* AF_INET address of local logger */
  141. /*
  142.  * OPENLOG -- open system log
  143.  */
  144. void openlog(char *ident, int logstat, int logfac)
  145. {
  146.         sock_init();
  147.         if (ident != NULL)
  148.                 LogTag = ident;
  149.         LogStat = logstat;
  150.         if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  151.                 LogFacility = logfac;
  152.         if (LogFile == -1) {
  153.                 SyslogAddr.sin_family = AF_INET;
  154.                 SyslogAddr.sin_port = htons(514);
  155.                 SyslogAddr.sin_addr.s_addr = INADDR_ANY;
  156.                 if (LogStat & LOG_NDELAY) {
  157.                         LogFile = socket(AF_INET, SOCK_DGRAM, 0);
  158.                 }
  159.         }
  160.         if (LogFile != -1 && !connected &&
  161.             connect(LogFile, (struct sockaddr*) &SyslogAddr, sizeof(SyslogAddr)) != -1)
  162.                 connected = 1;
  163. }
  164.  
  165. /*
  166.  * CLOSELOG -- close the system log
  167.  */
  168. void closelog(void)
  169. {
  170.         (void) soclose(LogFile);
  171.         LogFile = -1;
  172.         connected = 0;
  173. }
  174.  
  175. static int      LogMask = 0xff;         /* mask of priorities to be logged */
  176. /*
  177.  * SETLOGMASK -- set the log mask level
  178.  */
  179. int setlogmask(int pmask)
  180. {
  181.         int omask;
  182.  
  183.         omask = LogMask;
  184.         if (pmask != 0)
  185.                 LogMask = pmask;
  186.         return (omask);
  187. }
  188.