4 * This module handles the logging of requests.
6 * TODO: Merge the two "XXX_log() calls.
8 * Authors: Donald J. Becker, <becker@super.org>
9 * Rick Sladkey, <jrs@world.std.com>
10 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Olaf Kirch, <okir@monad.swb.de>
13 * This software maybe be used for any purpose provided
14 * the above copyright notice is retained. It is supplied
15 * as is, with no warranty expressed or implied.
32 static int log_stderr = 1;
33 static int log_syslog = 1;
34 static int logging = 0; /* enable/disable DEBUG logs */
35 static int logmask = 0; /* What will be logged */
36 static char log_name[256]; /* name of this program */
37 static int log_pid = -1; /* PID of this program */
39 static void xlog_toggle(int sig);
40 static struct xlog_debugfac debugnames[] = {
41 { "general", D_GENERAL, },
44 { "parse", D_PARSE, },
50 xlog_open(char *progname)
52 openlog(progname, LOG_PID, LOG_DAEMON);
54 strncpy(log_name, progname, sizeof (log_name) - 1);
55 log_name [sizeof (log_name) - 1] = '\0';
58 signal(SIGUSR1, xlog_toggle);
59 signal(SIGUSR2, xlog_toggle);
80 if ((logmask & D_ALL) && !logging) {
81 xlog(D_GENERAL, "turned on logging");
86 logmask |= ((logmask & D_ALL) << 1) | D_GENERAL;
87 for (i = -1, tmp &= logmask; tmp; tmp >>= 1, i++)
90 "turned on logging level %d", i);
92 xlog(D_GENERAL, "turned off logging");
95 signal(sig, xlog_toggle);
99 xlog_config(int fac, int on)
110 xlog_sconfig(char *kind, int on)
112 struct xlog_debugfac *tbl = debugnames;
114 while (tbl->df_name != NULL && strcasecmp(tbl->df_name, kind))
117 xlog (L_WARNING, "Invalid debug facility: %s\n", kind);
120 xlog_config(tbl->df_fac, on);
124 xlog_enabled(int fac)
126 return (logging && (fac & logmask));
130 /* Write something to the system logfile and/or stderr */
132 xlog(int kind, const char *fmt, ...)
138 if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
142 vsnprintf(buff, sizeof (buff), fmt, args);
145 if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
151 syslog(LOG_ERR, "%s", buff);
154 syslog(LOG_ERR, "%s", buff);
157 syslog(LOG_WARNING, "%s", buff);
160 syslog(LOG_NOTICE, "%s", buff);
164 syslog(LOG_DEBUG, "%s", buff);
170 #ifdef VERBOSE_PRINTF
175 tm = localtime(&now);
176 fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
178 tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
179 tm->tm_hour, tm->tm_min, tm->tm_sec,
182 fprintf(stderr, "%s: %s\n", log_name, buff);