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 2000 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
23 * Copyright 2000 Chad Miller <cmiller@surfsouth.com>
26 static const char rcsid[] = "$Id$";
29 #include "libradius.h"
42 * Log the message to the logfile. Include the severity and
45 static int do_log(int lvl, const char *fmt, va_list ap)
54 int use_syslog = FALSE;
56 if ((lvl & L_CONS) || radlog_dir == NULL || debug_flag) {
59 fprintf(stdout, "%s: ", progname);
60 vfprintf(stdout, fmt, ap);
61 fprintf(stdout, "\n");
63 if (radlog_dir == NULL || debug_flag)
66 if (strcmp(radlog_dir, "stdout") == 0) {
70 } else if (strcmp(radlog_dir, "syslog") == 0) {
74 sprintf(buffer, "%.1000s/%.1000s", radlog_dir, RADIUS_LOG);
75 if ((msgfd = fopen(buffer, "a")) == NULL) {
76 fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
77 progname, buffer, strerror(errno));
87 strcpy(buffer, ctime(&timeval));
109 len = strlen(buffer);
111 #ifdef HAVE_VSNPRINTF
112 vsnprintf(buffer + len, sizeof(buffer) - len -1, fmt, ap);
114 vsprintf(buffer + len, fmt, ap);
115 if (strlen(buffer) >= sizeof(buffer) - 1)
116 /* What can we do? */
121 * Filter out characters not in Latin-1.
123 for (p = (unsigned char *)buffer; *p != '\0'; p++) {
124 if (*p == '\r' || *p == '\n')
126 else if (*p < 32 || (*p >= 128 && *p <= 160))
129 strcat(buffer, "\n");
133 fputs(buffer, msgfd);
158 syslog(lvl, "%s", buffer);
165 int log_debug(const char *msg, ...)
171 r = do_log(L_DBG, msg, ap);
177 int radlog(int lvl, const char *msg, ...)
183 r = do_log(lvl, msg, ap);