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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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$";
34 #include <sys/types.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 #ifdef HAVE_VSNPRINTF
141 vsnprintf(buffer + len, sizeof(buffer) - len -1, fmt, ap);
143 vsprintf(buffer + len, fmt, ap);
144 if (strlen(buffer) >= sizeof(buffer) - 1)
145 /* What can we do? */
150 * Filter out characters not in Latin-1.
152 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
153 if (*p == '\r' || *p == '\n')
155 else if (*p < 32 || (*p >= 128 && *p <= 160))
158 strcat(buffer, "\n");
161 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
162 switch(lvl & ~L_CONS) {
179 syslog(lvl, "%s", buffer);
187 write(mainconfig.radlog_fd, buffer, strlen(buffer));
193 int log_debug(const char *msg, ...)
199 r = vradlog(L_DBG, msg, ap);
205 int radlog(int lvl, const char *msg, ...)
211 r = vradlog(lvl, msg, ap);
219 * Dump a whole list of attributes to DEBUG2
221 void vp_listdebug(VALUE_PAIR *vp)
224 for (; vp; vp = vp->next) {
225 vp_prints(tmpPair, sizeof(tmpPair), vp);
226 DEBUG2(" %s", tmpPair);