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/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)
63 int fd = mainconfig.radlog_fd;
67 int len, print_timestamp = 0;
70 * NOT debugging, and trying to log debug messages.
72 * Throw the message away.
74 if (!debug_flag && (lvl == L_DBG)) {
79 * If we don't want any messages, then
82 if (mainconfig.radlog_dest == RADLOG_NULL) {
87 * Don't print timestamps to syslog, it does that for us.
88 * Don't print timestamps for low levels of debugging.
90 * Print timestamps for non-debugging, and for high levels
93 if ((mainconfig.radlog_dest != RADLOG_SYSLOG) &&
94 (debug_flag != 1) && (debug_flag != 2)) {
102 * Use it, rather than anything else.
106 } else if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
108 * Open run openlog() on the first log message
111 openlog(progname, LOG_PID, mainconfig.syslog_facility);
116 } else if (!mainconfig.log_file) {
118 * Errors go to stderr, in the hope that they will
119 * be printed somewhere.
124 snprintf(buffer, sizeof(buffer), "%s: ", progname);
125 len = strlen(buffer);
128 * No log file set. Discard it.
133 } else if ((fp = fopen(mainconfig.log_file, "a")) == NULL) {
134 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
135 progname, mainconfig.log_file, strerror(errno));
137 fprintf(stderr, " (");
138 vfprintf(stderr, fmt, ap); /* the message that caused the log */
139 fprintf(stderr, ")\n");
143 if (print_timestamp) {
147 timeval = time(NULL);
148 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
150 s = lrad_int2str(levels, (lvl & ~L_CONS), ": ");
153 len = strlen(buffer);
156 vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
159 * Filter out characters not in Latin-1.
161 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
162 if (*p == '\r' || *p == '\n')
164 else if (*p == '\t') continue;
165 else if (*p < 32 || (*p >= 128 && *p <= 160))
168 strcat(buffer, "\n");
171 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
172 switch(lvl & ~L_CONS) {
189 syslog(lvl, "%s", buffer);
196 } else if (fd >= 0) {
197 write(fd, buffer, strlen(buffer));
203 int log_debug(const char *msg, ...)
209 r = vradlog(L_DBG, msg, ap);
215 int radlog(int lvl, const char *msg, ...)
221 r = vradlog(lvl, msg, ap);
229 * Dump a whole list of attributes to DEBUG2
231 void vp_listdebug(VALUE_PAIR *vp)
234 for (; vp; vp = vp->next) {
235 vp_prints(tmpPair, sizeof(tmpPair), vp);
236 DEBUG2(" %s", tmpPair);