2 Copyright (c) 1995-1998 by Cisco systems, Inc.
4 Permission to use, copy, modify, and distribute this software for
5 any purpose and without fee is hereby granted, provided that this
6 copyright and permission notice appear on all copies of the
7 software and supporting documentation, the name of Cisco Systems,
8 Inc. not be used in advertising or publicity pertaining to
9 distribution of the program without specific prior permission, and
10 notice be given in supporting documentation that modification,
11 copying and distribution is by permission of Cisco Systems, Inc.
13 Cisco Systems, Inc. makes no representations about the suitability
14 of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
15 IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16 WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/types.h>
30 #include <stdarg.h> /* ANSI C, variable length args */
32 #include <varargs.h> /* has 'vararg' definitions */
37 char *logfile = LOGFILE_DEFAULT;
41 * This routine reports errors and such via stderr and syslog() if
42 * appopriate. It just helps avoid a lot of if-else in the code.
44 * LOG_DEBUG messages are ignored unless debugging is on.
45 * All other priorities are always logged to syslog.
50 report(int priority, char *fmt,...)
54 report(priority, fmt, va_alist)
57 va_dcl /* no terminating semi-colon */
60 char msg[255]; /* temporary string */
61 char *fp, *bufp, *charp;
72 /* ensure that msg is never overwritten */
97 charp = va_arg(ap, char *);
103 i = va_arg(ap, uint);
104 sprintf(digits, "%u", i);
110 i = va_arg(ap, uint);
111 sprintf(digits, "%x", i);
118 sprintf(digits, "%d", i);
124 if ((len + m + 1) >= n) {
128 memcpy(bufp, charp, m);
136 /* check we never overwrote the end of the buffer */
148 ostream = fopen("/dev/console", "w");
151 if (priority == LOG_ERR)
152 fprintf(ostream, "Error ");
153 fprintf(ostream, "%s\n", msg);
156 syslog(LOG_ERR, "Cannot open /dev/console errno=%d", errno);
162 logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0640);
165 time_t t = time(NULL);
166 char *ct = ctime(&t);
169 tac_lockfd(logfile, logfd);
170 sprintf(buf, "%s [%d]: ", ct, getpid());
171 write(logfd, buf, strlen(buf));
172 if (priority == LOG_ERR)
173 write(logfd, "Error ", 6);
174 write(logfd, msg, strlen(msg));
175 write(logfd, "\n", 1);
181 fprintf(stderr, "%s\n", msg);
184 if (priority == LOG_DEBUG)
187 if (priority == LOG_ERR)
188 syslog(priority, "Error %s", msg);
190 syslog(priority, "%s", msg);
193 /* format a hex dump for syslog */
195 report_hex(priority, p, len)
209 for (i = 0; i < len && i < 255; i++, p++) {
211 sprintf(digit, "0x%x ", *p);
213 buflen += strlen(digit);
216 report(priority, "%s", buf);
223 report(priority, "%s", buf);
228 /* format a non-null terminated string for syslog */
230 report_string(priority, p, len)
241 for (i = 0; i < len && i < 255; i++) {
242 if (32 <= *p && *p <= 126) {
245 sprintf(bufp, " 0x%x ", *p);
246 bufp += strlen(bufp);
251 report(priority, "%s", buf);
258 report(LOG_ERR, "in regular expression %s", s);