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$";
29 #include "libradius.h"
35 #include <sys/types.h>
45 * Logging facility names
47 static LRAD_NAME_NUMBER levels[] = {
48 { ": Debug: ", L_DBG },
49 { ": Auth: ", L_AUTH },
50 { ": Proxy: ", L_PROXY },
51 { ": Info: ", L_INFO },
52 { ": Error: ", L_ERR },
57 * Log the message to the logfile. Include the severity and
60 int vradlog(int lvl, const char *fmt, va_list ap)
68 * NOT debugging, and trying to log debug messages.
70 * Throw the message away.
72 if (!debug_flag && (lvl == L_DBG)) {
77 * If we don't want any messages, then
80 if (radlog_dest == RADLOG_NULL) {
85 || (radlog_dest == RADLOG_STDOUT)
86 || (radlog_dir == NULL)) {
89 } else if (radlog_dest == RADLOG_STDERR) {
92 } else if (radlog_dest != RADLOG_SYSLOG) {
94 * No log file set. It must go to stdout.
96 if (!mainconfig.log_file) {
100 * Else try to open the file.
102 } else if ((msgfd = fopen(mainconfig.log_file, "a")) == NULL) {
103 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
104 progname, mainconfig.log_file, strerror(errno));
106 fprintf(stderr, " (");
107 vfprintf(stderr, fmt, ap); /* the message that caused the log */
108 fprintf(stderr, ")\n");
114 if (radlog_dest == RADLOG_SYSLOG) {
123 timeval = time(NULL);
124 CTIME_R(&timeval, buffer, 8192);
126 s = lrad_int2str(levels, (lvl & ~L_CONS), ": ");
129 len = strlen(buffer);
132 #ifdef HAVE_VSNPRINTF
133 vsnprintf(buffer + len, sizeof(buffer) - len -1, fmt, ap);
135 vsprintf(buffer + len, fmt, ap);
136 if (strlen(buffer) >= sizeof(buffer) - 1)
137 /* What can we do? */
142 * Filter out characters not in Latin-1.
144 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
145 if (*p == '\r' || *p == '\n')
147 else if (*p < 32 || (*p >= 128 && *p <= 160))
150 strcat(buffer, "\n");
153 * If we're debugging, for small values of debug, then
154 * we don't do timestamps.
156 if ((debug_flag == 1) || (debug_flag == 2)) {
161 * No debugging, or lots of debugging. Print
168 if (radlog_dest != RADLOG_SYSLOG)
172 if (msgfd == stdout) {
174 } else if (msgfd == stderr) {
181 else { /* it was syslog */
182 switch(lvl & ~L_CONS) {
199 syslog(lvl, "%s", buffer + len); /* don't print timestamp */
206 int log_debug(const char *msg, ...)
212 r = vradlog(L_DBG, msg, ap);
218 int radlog(int lvl, const char *msg, ...)
224 r = vradlog(lvl, msg, ap);
232 * Dump a whole list of attributes to DEBUG2
234 void vp_listdebug(VALUE_PAIR *vp)
237 for (; vp; vp = vp->next) {
238 vp_prints(tmpPair, sizeof(tmpPair), vp);
239 DEBUG2(" %s", tmpPair);