2 * log.c Logging module.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2001 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
23 * Copyright 2001 Chad Miller <cmiller@surfsouth.com>
26 static const char rcsid[] = "$Id$";
28 #include <freeradius-devel/autoconf.h>
34 #include <sys/types.h>
37 #include <freeradius-devel/radiusd.h>
41 /* keep track of whether we've run openlog() */
42 static int openlog_run = 0;
46 * Logging facility names
48 static const LRAD_NAME_NUMBER levels[] = {
49 { ": Debug: ", L_DBG },
50 { ": Auth: ", L_AUTH },
51 { ": Proxy: ", L_PROXY },
52 { ": Info: ", L_INFO },
53 { ": Error: ", L_ERR },
58 * Log the message to the logfile. Include the severity and
61 int vradlog(int lvl, const char *fmt, va_list ap)
69 * NOT debugging, and trying to log debug messages.
71 * Throw the message away.
73 if (!debug_flag && (lvl == L_DBG)) {
78 * If we don't want any messages, then
81 if (mainconfig.radlog_dest == RADLOG_NULL) {
87 if (mainconfig.radlog_fd != -1) {
89 * Do nothing, but allow it.
93 } else if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
95 * Open run openlog() on the first log message
98 openlog(progname, LOG_PID, mainconfig.syslog_facility);
103 } else if (!mainconfig.log_file) {
105 * No log file set. Discard it.
109 } else if ((fp = fopen(mainconfig.log_file, "a")) == NULL) {
110 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
111 progname, mainconfig.log_file, strerror(errno));
113 fprintf(stderr, " (");
114 vfprintf(stderr, fmt, ap); /* the message that caused the log */
115 fprintf(stderr, ")\n");
120 * Don't print timestamps to syslog, it does that for us.
121 * Don't print timestamps for low levels of debugging.
123 * Print timestamps for non-debugging, and for high levels
126 if ((mainconfig.radlog_dest != RADLOG_SYSLOG) &&
127 (debug_flag != 1) && (debug_flag != 2)) {
131 timeval = time(NULL);
132 CTIME_R(&timeval, buffer, 8192);
134 s = lrad_int2str(levels, (lvl & ~L_CONS), ": ");
137 len = strlen(buffer);
140 vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
143 * Filter out characters not in Latin-1.
145 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
146 if (*p == '\r' || *p == '\n')
148 else if (*p == '\t') continue;
149 else if (*p < 32 || (*p >= 128 && *p <= 160))
152 strcat(buffer, "\n");
155 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
156 switch(lvl & ~L_CONS) {
173 syslog(lvl, "%s", buffer);
181 write(mainconfig.radlog_fd, buffer, strlen(buffer));
187 int log_debug(const char *msg, ...)
193 r = vradlog(L_DBG, msg, ap);
199 int radlog(int lvl, const char *msg, ...)
205 r = vradlog(lvl, msg, ap);
213 * Dump a whole list of attributes to DEBUG2
215 void vp_listdebug(VALUE_PAIR *vp)
218 for (; vp; vp = vp->next) {
219 vp_prints(tmpPair, sizeof(tmpPair), vp);
220 DEBUG2(" %s", tmpPair);