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>
31 #ifdef HAVE_SYS_STAT_H
40 * Logging facility names
42 static const FR_NAME_NUMBER levels[] = {
43 { ": Debug: ", L_DBG },
44 { ": Auth: ", L_AUTH },
45 { ": Proxy: ", L_PROXY },
46 { ": Info: ", L_INFO },
47 { ": Acct: ", L_ACCT },
48 { ": Error: ", L_ERR },
52 int log_dates_utc = 0;
56 * Log the message to the logfile. Include the severity and
59 int vradlog(int lvl, const char *fmt, va_list ap)
61 struct main_config_t *myconfig = &mainconfig;
67 * NOT debugging, and trying to log debug messages.
69 * Throw the message away.
71 if (!debug_flag && (lvl == L_DBG)) {
76 * If we don't want any messages, then
79 if (myconfig->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 ((myconfig->radlog_dest != RADLOG_SYSLOG) &&
94 (debug_flag != 1) && (debug_flag != 2)) {
99 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
101 s = fr_int2str(levels, (lvl & ~L_CONS), ": ");
104 len = strlen(buffer);
107 vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
110 * Filter out characters not in Latin-1.
112 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
113 if (*p == '\r' || *p == '\n')
115 else if (*p == '\t') continue;
116 else if (*p < 32 || (*p >= 128 && *p <= 160))
119 strcat(buffer, "\n");
121 switch (myconfig->radlog_dest) {
125 switch(lvl & ~L_CONS) {
145 syslog(lvl, "%s", buffer);
152 write(myconfig->radlog_fd, buffer, strlen(buffer));
156 case RADLOG_NULL: /* should have been caught above */
163 int log_debug(const char *msg, ...)
169 r = vradlog(L_DBG, msg, ap);
175 int radlog(int lvl, const char *msg, ...)
181 r = vradlog(lvl, msg, ap);
189 * Dump a whole list of attributes to DEBUG2
191 void vp_listdebug(VALUE_PAIR *vp)
194 for (; vp; vp = vp->next) {
195 vp_prints(tmpPair, sizeof(tmpPair), vp);
196 DEBUG2(" %s", tmpPair);
200 extern char *request_log_file;
201 #ifdef WITH_COMMAND_SOCKET
202 extern char *debug_log_file;
205 void radlog_request(int lvl, int priority, REQUEST *request, const char *msg, ...)
208 const char *filename = request_log_file;
216 * Debug messages get treated specially.
220 * There is log function, but the debug level
221 * isn't high enough. OR, we're in debug mode,
222 * and the debug level isn't high enough. Return.
224 if ((request && request->radlog &&
225 (priority > request->options)) ||
226 ((debug_flag != 0) && (priority > debug_flag))) {
232 * Use the debug output file, if specified,
233 * otherwise leave it as "request_log_file".
235 #ifdef WITH_COMMAND_SOCKET
236 filename = debug_log_file;
239 filename = request_log_file;
242 * Debug messages get mashed to L_INFO for
245 if (!filename) lvl = L_INFO;
248 if (request && filename) {
250 radlog_func_t rl = request->radlog;
252 request->radlog = NULL;
255 * This is SLOW! Doing it for every log message
256 * in every request is NOT recommended!
259 radius_xlat(buffer, sizeof(buffer), filename,
260 request, NULL, NULL); /* FIXME: escape chars! */
261 request->radlog = rl;
263 p = strrchr(buffer, FR_DIR_SEP);
266 if (rad_mkdir(buffer, S_IRWXU) < 0) {
267 radlog(L_ERR, "Failed creating %s: %s",
268 buffer,strerror(errno));
275 fp = fopen(buffer, "a");
279 * Print timestamps to the file.
284 timeval = time(NULL);
289 gmtime_r(&timeval, &utc);
290 asctime_r(&utc, buffer + len);
293 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
295 s = strrchr(buffer, '\n');
301 strcat(buffer, fr_int2str(levels, (lvl & ~L_CONS), ": "));
302 len = strlen(buffer);
305 if (request && request->module[0]) {
306 snprintf(buffer + len, sizeof(buffer) + len, "%s : ", request->module);
307 len = strlen(buffer);
309 vsnprintf(buffer + len, sizeof(buffer) - len, msg, ap);
313 radlog(lvl, "(%u) %s", request->number, buffer);
315 radlog(lvl, "%s", buffer);
318 if (request) fprintf(fp, "(%u) ", request->number);