1 #if !defined (BSD43) && !defined(hpux)
3 static char SccsId[] = "@(#)syslog.c 4.1 (Berkeley) 5/27/83";
6 static char ident[] = "@(#)$Id: syslog.c,v 1.9 1993/08/25 17:23:42 jromine Exp $";
10 * SYSLOG -- print message on log file
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.
18 * The output of this routine is intended to be read by
19 * /etc/syslog, which will add timestamps.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
28 #define MAXLINE 1024 /* max message size */
29 #define BUFSLOP 20 /* space to allow for "extra stuff" */
31 #define NULL 0 /* manifest */
34 #define LOG_COOLIT LOG_LOCAL0 /* local syslog code */
35 #define LOG_DGRAM LOG_LOCAL1 /* idem */
38 #define LOG_HOST "localhost" /* host where syslogd is running */
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 */
46 struct sockaddr_in SyslogAddr;
47 static char *SyslogHost = LOG_HOST;
52 extern char *sys_errlist[];
55 syslog(pri, fmt, p0, p1, p2, p3, p4)
59 char buf[MAXLINE+BUFSLOP], outline[MAXLINE + 1];
64 /* see if we should just throw out this message */
67 for (b = buf, f = fmt; f && *f; b = buf) {
70 if (pri > 0 && (LogStat & LOG_COOLIT) == 0) {
71 sprintf(b, "<%d>", pri);
74 if (LogStat & LOG_PID) {
75 sprintf(b, "%d ", getpid());
79 sprintf(b, "%s: ", LogTag);
82 while ((c = *f++) != '\0' && c != '\n' && b < buf + MAXLINE) {
90 *b++ = '%', *b++ = c, *b++ = '\0';
96 if ((unsigned)errno > sys_nerr)
97 sprintf(b, "error %d", errno);
99 strcat(b, sys_errlist[errno]);
104 *b++ = '\n', *b = '\0';
105 sprintf(outline, buf, p0, p1, p2, p3, p4);
107 if (LogStat & LOG_DGRAM)
108 (void) sendto(LogFile, outline, strlen(outline), 0,
109 &SyslogAddr, sizeof SyslogAddr);
111 (void) write(LogFile, outline, strlen(outline));
113 perror("syslog: sendto");
118 * OPENLOG -- open system log
120 openlog(ident, logstat)
131 sp = getservbyname("syslog", "udp");
132 hp = gethostbyname(SyslogHost);
133 if (sp == NULL || hp == NULL)
135 LogFile = socket(AF_INET, SOCK_DGRAM, 0);
137 perror("syslog: socket");
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;
147 LogStat |= LOG_COOLIT;
148 LogStat &= ~LOG_DGRAM;
150 LogFile = open("/dev/console", 1);
152 perror("syslog: /dev/console");
158 * CLOSELOG -- close the system log
163 (void) close(LogFile);
166 #endif /* not BSD43 */