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