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 FR_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 struct main_config_t *myconfig = &mainconfig;
57 int fd = myconfig->radlog_fd;
61 int len, print_timestamp = 0;
64 * NOT debugging, and trying to log debug messages.
66 * Throw the message away.
68 if (!debug_flag && (lvl == L_DBG)) {
73 * If we don't want any messages, then
76 if (myconfig->radlog_dest == RADLOG_NULL) {
81 * Don't print timestamps to syslog, it does that for us.
82 * Don't print timestamps for low levels of debugging.
84 * Print timestamps for non-debugging, and for high levels
87 if ((myconfig->radlog_dest != RADLOG_SYSLOG) &&
88 (debug_flag != 1) && (debug_flag != 2)) {
96 * Use it, rather than anything else.
100 } else if (myconfig->radlog_dest == RADLOG_SYSLOG) {
102 * Open run openlog() on the first log message
105 openlog(progname, LOG_PID, myconfig->syslog_facility);
110 } else if (myconfig->radlog_dest == RADLOG_FILES) {
111 if (!myconfig->log_file) {
113 * Errors go to stderr, in the hope that
114 * they will be printed somewhere.
119 snprintf(buffer, sizeof(buffer), "%s: ", progname);
120 len = strlen(buffer);
123 * No log file set. Discard it.
128 } else if ((fp = fopen(myconfig->log_file, "a")) == NULL) {
129 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
130 progname, myconfig->log_file, strerror(errno));
132 fprintf(stderr, " (");
133 vfprintf(stderr, fmt, ap); /* the message that caused the log */
134 fprintf(stderr, ")\n");
139 if (print_timestamp) {
143 timeval = time(NULL);
144 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
146 s = fr_int2str(levels, (lvl & ~L_CONS), ": ");
149 len = strlen(buffer);
152 vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
155 * Filter out characters not in Latin-1.
157 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
158 if (*p == '\r' || *p == '\n')
160 else if (*p == '\t') continue;
161 else if (*p < 32 || (*p >= 128 && *p <= 160))
164 strcat(buffer, "\n");
167 if (myconfig->radlog_dest == RADLOG_SYSLOG) {
168 switch(lvl & ~L_CONS) {
188 syslog(lvl, "%s", buffer);
195 } else if (fd >= 0) {
196 write(fd, buffer, strlen(buffer));
202 int log_debug(const char *msg, ...)
208 r = vradlog(L_DBG, msg, ap);
214 int radlog(int lvl, const char *msg, ...)
220 r = vradlog(lvl, msg, ap);
228 * Dump a whole list of attributes to DEBUG2
230 void vp_listdebug(VALUE_PAIR *vp)
233 for (; vp; vp = vp->next) {
234 vp_prints(tmpPair, sizeof(tmpPair), vp);
235 DEBUG2(" %s", tmpPair);