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 foreground = 1; /* not a daemon initially */
33 static int logging = 0; /* enable/disable DEBUG logs */
34 static int logmask = 0; /* What will be logged */
35 static char log_name[256]; /* name of this program */
36 static int log_pid = -1; /* PID of this program */
37 static FILE *log_fp = (FILE *)NULL; /* fp for the log file */
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);
59 strncpy(log_name, progname, sizeof (log_name) - 1);
60 log_name [sizeof (log_name) - 1] = '\0';
63 signal(SIGUSR1, xlog_toggle);
64 signal(SIGUSR2, xlog_toggle);
79 if ((logmask & D_ALL) && !logging) {
80 xlog(D_GENERAL, "turned on logging");
85 logmask |= ((logmask & D_ALL) << 1) | D_GENERAL;
86 for (i = -1, tmp &= logmask; tmp; tmp >>= 1, i++)
89 "turned on logging level %d", i);
91 xlog(D_GENERAL, "turned off logging");
94 signal(sig, xlog_toggle);
98 xlog_config(int fac, int on)
109 xlog_sconfig(char *kind, int on)
111 struct xlog_debugfac *tbl = debugnames;
113 while (tbl->df_name != NULL && strcasecmp(tbl->df_name, kind))
116 xlog (L_WARNING, "Invalid debug facility: %s\n", kind);
119 xlog_config(tbl->df_fac, on);
123 xlog_enabled(int fac)
125 return (logging && (fac & logmask));
129 /* Write something to the system logfile. */
131 xlog(int kind, const char *fmt, ...)
136 #ifdef VERBOSE_PRINTF
141 if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
145 vsnprintf(buff, sizeof (buff) - 2, fmt, args);
147 buff[sizeof (buff) - 2] = 0;
149 if ((n = strlen(buff)) > 0 && buff[n-1] != '\n') {
150 buff[n++] = '\n'; buff[n++] = '\0';
155 syslog(LOG_ERR, "%s", buff);
158 syslog(LOG_ERR, "%s", buff);
161 syslog(LOG_WARNING, "%s", buff);
164 syslog(LOG_NOTICE, "%s", buff);
170 if (!logged || foreground) {
171 if (!logged && log_fp == NULL) {
172 syslog(LOG_DEBUG, "%s", buff);
173 } else if (log_fp != NULL) {
174 #ifdef VERBOSE_PRINTF
176 tm = localtime(&now);
177 fprintf(log_fp, "%s[%d] %02d/%02d/%02d %02d:%02d %s\n",
179 tm->tm_mon + 1, tm->tm_mday,
180 tm->tm_year, tm->tm_hour, tm->tm_min,
183 fprintf(log_fp, "%s: %s", log_name, buff);