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,2006 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 #include <freeradius-devel/ident.h>
29 #include <freeradius-devel/radiusd.h>
33 /* keep track of whether we've run openlog() */
34 static int openlog_run = 0;
38 * Logging facility names
40 static const LRAD_NAME_NUMBER levels[] = {
41 { ": Debug: ", L_DBG },
42 { ": Auth: ", L_AUTH },
43 { ": Proxy: ", L_PROXY },
44 { ": Info: ", L_INFO },
45 { ": Acct: ", L_ACCT },
46 { ": Error: ", L_ERR },
51 * Log the message to the logfile. Include the severity and
54 int vradlog(int lvl, const char *fmt, va_list ap)
56 int fd = mainconfig.radlog_fd;
60 int len, print_timestamp = 0;
63 * NOT debugging, and trying to log debug messages.
65 * Throw the message away.
67 if (!debug_flag && (lvl == L_DBG)) {
72 * If we don't want any messages, then
75 if (mainconfig.radlog_dest == RADLOG_NULL) {
80 * Don't print timestamps to syslog, it does that for us.
81 * Don't print timestamps for low levels of debugging.
83 * Print timestamps for non-debugging, and for high levels
86 if ((mainconfig.radlog_dest != RADLOG_SYSLOG) &&
87 (debug_flag != 1) && (debug_flag != 2)) {
95 * Use it, rather than anything else.
99 } else if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
101 * Open run openlog() on the first log message
104 openlog(progname, LOG_PID, mainconfig.syslog_facility);
109 } else if (!mainconfig.log_file) {
111 * Errors go to stderr, in the hope that they will
112 * be printed somewhere.
117 snprintf(buffer, sizeof(buffer), "%s: ", progname);
118 len = strlen(buffer);
121 * No log file set. Discard it.
126 } else if ((fp = fopen(mainconfig.log_file, "a")) == NULL) {
127 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
128 progname, mainconfig.log_file, strerror(errno));
130 fprintf(stderr, " (");
131 vfprintf(stderr, fmt, ap); /* the message that caused the log */
132 fprintf(stderr, ")\n");
136 if (print_timestamp) {
140 timeval = time(NULL);
141 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
143 s = lrad_int2str(levels, (lvl & ~L_CONS), ": ");
146 len = strlen(buffer);
149 vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
152 * Filter out characters not in Latin-1.
154 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
155 if (*p == '\r' || *p == '\n')
157 else if (*p == '\t') continue;
158 else if (*p < 32 || (*p >= 128 && *p <= 160))
161 strcat(buffer, "\n");
164 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
165 switch(lvl & ~L_CONS) {
185 syslog(lvl, "%s", buffer);
192 } else if (fd >= 0) {
193 write(fd, buffer, strlen(buffer));
199 int log_debug(const char *msg, ...)
205 r = vradlog(L_DBG, msg, ap);
211 int radlog(int lvl, const char *msg, ...)
217 r = vradlog(lvl, msg, ap);
225 * Dump a whole list of attributes to DEBUG2
227 void vp_listdebug(VALUE_PAIR *vp)
230 for (; vp; vp = vp->next) {
231 vp_prints(tmpPair, sizeof(tmpPair), vp);
232 DEBUG2(" %s", tmpPair);