More changes to make a common naming scheme. This breaks
[freeradius.git] / src / main / log.c
1 /*
2  * log.c        Logging module.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
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>
24  */
25
26 #include <freeradius-devel/ident.h>
27 RCSID("$Id$")
28
29 #include <freeradius-devel/radiusd.h>
30
31 #ifdef HAVE_SYSLOG_H
32 #       include <syslog.h>
33 /* keep track of whether we've run openlog() */
34 static int openlog_run = 0;
35 #endif
36
37  /*
38  * Logging facility names
39  */
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   },
47         { NULL, 0 }
48 };
49
50 /*
51  *      Log the message to the logfile. Include the severity and
52  *      a time stamp.
53  */
54 int vradlog(int lvl, const char *fmt, va_list ap)
55 {
56         struct main_config_t *myconfig = &mainconfig;
57         int fd = myconfig->radlog_fd;
58         FILE *fp = NULL;
59         unsigned char *p;
60         char buffer[8192];
61         int len, print_timestamp = 0;
62
63         /*
64          *      NOT debugging, and trying to log debug messages.
65          *
66          *      Throw the message away.
67          */
68         if (!debug_flag && (lvl == L_DBG)) {
69                 return 0;
70         }
71
72         /*
73          *      If we don't want any messages, then
74          *      throw them away.
75          */
76         if (myconfig->radlog_dest == RADLOG_NULL) {
77                 return 0;
78         }
79
80         /*
81          *      Don't print timestamps to syslog, it does that for us.
82          *      Don't print timestamps for low levels of debugging.
83          *
84          *      Print timestamps for non-debugging, and for high levels
85          *      of debugging.
86          */
87         if ((myconfig->radlog_dest != RADLOG_SYSLOG) &&
88             (debug_flag != 1) && (debug_flag != 2)) {
89                 print_timestamp = 1;
90         }
91
92         *buffer = '\0';
93         len = 0;
94         if (fd != -1) {
95                 /*
96                  *      Use it, rather than anything else.
97                  */
98
99 #ifdef HAVE_SYSLOG_H
100         } else if (myconfig->radlog_dest == RADLOG_SYSLOG) {
101                 /*
102                  *      Open run openlog() on the first log message
103                  */
104                 if(!openlog_run) {
105                         openlog(progname, LOG_PID, myconfig->syslog_facility);
106                         openlog_run = 1;
107                 }
108 #endif
109
110         } else if (myconfig->radlog_dest == RADLOG_FILES) {
111                 if (!myconfig->log_file) {
112                         /*
113                          *      Errors go to stderr, in the hope that
114                          *      they will be printed somewhere.
115                          */
116                         if (lvl & L_ERR) {
117                                 fd = STDERR_FILENO;
118                                 print_timestamp = 0;
119                                 snprintf(buffer, sizeof(buffer), "%s: ", progname);
120                                 len = strlen(buffer);
121                         } else {
122                                 /*
123                                  *      No log file set.  Discard it.
124                                  */
125                                 return 0;
126                         }
127                         
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));
131                         
132                         fprintf(stderr, "  (");
133                         vfprintf(stderr, fmt, ap);  /* the message that caused the log */
134                         fprintf(stderr, ")\n");
135                         return -1;
136                 }
137         }
138
139         if (print_timestamp) {
140                 const char *s;
141                 time_t timeval;
142
143                 timeval = time(NULL);
144                 CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1);
145
146                 s = fr_int2str(levels, (lvl & ~L_CONS), ": ");
147
148                 strcat(buffer, s);
149                 len = strlen(buffer);
150         }
151
152         vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap);
153
154         /*
155          *      Filter out characters not in Latin-1.
156          */
157         for (p = (unsigned char *)buffer; *p != '\0'; p++) {
158                 if (*p == '\r' || *p == '\n')
159                         *p = ' ';
160                 else if (*p == '\t') continue;
161                 else if (*p < 32 || (*p >= 128 && *p <= 160))
162                         *p = '?';
163         }
164         strcat(buffer, "\n");
165
166 #ifdef HAVE_SYSLOG_H
167         if (myconfig->radlog_dest == RADLOG_SYSLOG) {
168                 switch(lvl & ~L_CONS) {
169                         case L_DBG:
170                                 lvl = LOG_DEBUG;
171                                 break;
172                         case L_AUTH:
173                                 lvl = LOG_NOTICE;
174                                 break;
175                         case L_PROXY:
176                                 lvl = LOG_NOTICE;
177                                 break;
178                         case L_ACCT:
179                                 lvl = LOG_NOTICE;
180                                 break;
181                         case L_INFO:
182                                 lvl = LOG_INFO;
183                                 break;
184                         case L_ERR:
185                                 lvl = LOG_ERR;
186                                 break;
187                 }
188                 syslog(lvl, "%s", buffer);
189         } else
190 #endif
191         if (fp != NULL) {
192                 fputs(buffer, fp);
193                 fflush(fp);
194                 fclose(fp);
195         } else if (fd >= 0) {
196                 write(fd, buffer, strlen(buffer));
197         }
198
199         return 0;
200 }
201
202 int log_debug(const char *msg, ...)
203 {
204         va_list ap;
205         int r;
206
207         va_start(ap, msg);
208         r = vradlog(L_DBG, msg, ap);
209         va_end(ap);
210
211         return r;
212 }
213
214 int radlog(int lvl, const char *msg, ...)
215 {
216         va_list ap;
217         int r;
218
219         va_start(ap, msg);
220         r = vradlog(lvl, msg, ap);
221         va_end(ap);
222
223         return r;
224 }
225
226
227 /*
228  *      Dump a whole list of attributes to DEBUG2
229  */
230 void vp_listdebug(VALUE_PAIR *vp)
231 {
232         char tmpPair[70];
233         for (; vp; vp = vp->next) {
234                 vp_prints(tmpPair, sizeof(tmpPair), vp);
235                 DEBUG2("     %s", tmpPair);
236         }
237 }
238
239
240
241