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>
37 #ifdef HAVE_SYS_SYSLOG_H
38 #include <sys/syslog.h>
49 #define LOGFILE_DEFAULT "/var/log/tac_plus.log"
54 char *logfile = LOGFILE_DEFAULT;
58 * This routine reports errors and such via stderr and syslog() if
59 * appopriate. It just helps avoid a lot of if-else in the code.
61 * LOG_DEBUG messages are ignored unless debugging is on.
62 * All other priorities are always logged to syslog.
65 void report TAC_ARGS((int priority, const char *fmt, ...)) G_GNUC_PRINTF(2, 3);
69 #include <stdarg.h> /* ANSI C, variable length args */
71 report(int priority, const char *fmt,...)
75 #include <varargs.h> /* has 'vararg' definitions */
78 report(priority, fmt, va_alist)
81 va_dcl /* no terminating semi-colon */
85 char msg[255]; /* temporary string */
87 char *bufp, *charp = NULL /* GCC paranoia */;
88 int len, m = 0 /* GCC paranoia */, i, n;
98 /* ensure that msg is never overwritten */
123 charp = va_arg(ap, char *);
129 i = va_arg(ap, uint);
130 sprintf(digits, "%u", i);
136 i = va_arg(ap, uint);
137 sprintf(digits, "%x", i);
144 sprintf(digits, "%d", i);
149 syslog(LOG_ERR, "Unknown format character '%c', ignoring it", *fp);
153 if ((len + m + 1) >= n) {
157 memcpy(bufp, charp, m);
165 /* check we never overwrote the end of the buffer */
177 ostream = fopen("/dev/console", "w");
180 if (priority == LOG_ERR)
181 fprintf(ostream, "Error ");
182 fprintf(ostream, "%s\n", msg);
185 syslog(LOG_ERR, "Cannot open /dev/console errno=%d", errno);
191 logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0640);
194 time_t t = time(NULL);
195 char *ct = ctime(&t);
198 tac_lockfd(logfile, logfd);
199 sprintf(buf, "%s [%d]: ", ct, (int) getpid());
200 write(logfd, buf, strlen(buf));
201 if (priority == LOG_ERR)
202 write(logfd, "Error ", 6);
203 write(logfd, msg, strlen(msg));
204 write(logfd, "\n", 1);
210 fprintf(stderr, "%s\n", msg);
213 if (priority == LOG_DEBUG)
216 if (priority == LOG_ERR)
217 syslog(priority, "Error %s", msg);
219 syslog(priority, "%s", msg);
222 void report_hex TAC_ARGS((int priority, u_char *p, int len));
224 /* format a hex dump for syslog */
226 report_hex(priority, p, len)
240 for (i = 0; i < len && i < 255; i++, p++) {
242 sprintf(digit, "0x%x ", *p);
244 buflen += strlen(digit);
247 report(priority, "%s", buf);
254 report(priority, "%s", buf);
259 void report_string TAC_ARGS((int priority, u_char *p, int len));
261 /* format a non-null terminated string for syslog */
263 report_string(priority, p, len)
274 for (i = 0; i < len && i < 255; i++) {
275 if (32 <= *p && *p <= 126) {
278 sprintf(bufp, " 0x%x ", *p);
279 bufp += strlen(bufp);
284 report(priority, "%s", buf);
287 void tac_regerror TAC_ARGS((const char *s));
293 report(LOG_ERR, "in regular expression %s", s);