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.
34 static int log_stderr = 1;
35 static int log_syslog = 1;
36 static int logging = 0; /* enable/disable DEBUG logs */
37 static int logmask = 0; /* What will be logged */
38 static char log_name[256]; /* name of this program */
39 static int log_pid = -1; /* PID of this program */
41 static void xlog_toggle(int sig);
42 static struct xlog_debugfac debugnames[] = {
43 { "general", D_GENERAL, },
46 { "parse", D_PARSE, },
52 xlog_open(char *progname)
54 openlog(progname, LOG_PID, LOG_DAEMON);
56 strncpy(log_name, progname, sizeof (log_name) - 1);
57 log_name [sizeof (log_name) - 1] = '\0';
60 signal(SIGUSR1, xlog_toggle);
61 signal(SIGUSR2, xlog_toggle);
82 if ((logmask & D_ALL) && !logging) {
83 xlog(D_GENERAL, "turned on logging");
88 logmask |= ((logmask & D_ALL) << 1) | D_GENERAL;
89 for (i = -1, tmp &= logmask; tmp; tmp >>= 1, i++)
92 "turned on logging level %d", i);
94 xlog(D_GENERAL, "turned off logging");
97 signal(sig, xlog_toggle);
101 xlog_config(int fac, int on)
112 xlog_sconfig(char *kind, int on)
114 struct xlog_debugfac *tbl = debugnames;
116 while (tbl->df_name != NULL && strcasecmp(tbl->df_name, kind))
119 xlog (L_WARNING, "Invalid debug facility: %s\n", kind);
122 xlog_config(tbl->df_fac, on);
126 xlog_enabled(int fac)
128 return (logging && (fac & logmask));
132 /* Write something to the system logfile and/or stderr */
134 xlog(int kind, const char *fmt, ...)
140 if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
144 vsnprintf(buff, sizeof (buff), fmt, args);
147 if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
153 syslog(LOG_ERR, "%s", buff);
156 syslog(LOG_ERR, "%s", buff);
159 syslog(LOG_WARNING, "%s", buff);
162 syslog(LOG_NOTICE, "%s", buff);
166 syslog(LOG_INFO, "%s", buff);
172 #ifdef VERBOSE_PRINTF
177 tm = localtime(&now);
178 fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
180 tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
181 tm->tm_hour, tm->tm_min, tm->tm_sec,
184 fprintf(stderr, "%s: %s\n", log_name, buff);