]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.sbin/i4b/isdnd/log.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / usr.sbin / i4b / isdnd / log.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b daemon - logging routines
28  *      -----------------------------
29  *
30  * $FreeBSD$
31  *
32  *      last edit-date: [Wed Dec 26 12:49:45 2001]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include "isdnd.h"
37
38 #define LOGBUFLEN 256
39
40 extern int do_monitor;
41 extern int accepted;
42 extern FILE *logfp;
43
44 static void check_reg(char *logstring);
45
46 struct logtab {
47         char *text;
48         int pri;
49 };
50
51 /*---------------------------------------------------------------------------*
52  *      table for converting internal log levels into syslog levels
53  *---------------------------------------------------------------------------*/
54 static struct logtab logtab[] = {
55         {"ERR", LOG_ERR},       /* error conditions                     */
56         {"WRN", LOG_WARNING},   /* warning conditions, nonfatal         */
57         {"DMN", LOG_NOTICE},    /* significant conditions of the daemon */
58         {"CHD", LOG_INFO},      /* informational, call handling         */
59         {"DBG", LOG_DEBUG},     /* debug messages                       */
60         {"MER", LOG_ERR},       /* monitor error conditions             */      
61         {"PKT", LOG_INFO}       /* packet logging                       */
62 };
63
64 /*---------------------------------------------------------------------------*
65  *      initialize logging
66  *---------------------------------------------------------------------------*/
67 void
68 init_log(void)
69 {
70         int i;
71
72         if(uselogfile)
73         {
74                 if((logfp = fopen(logfile, "a")) == NULL)
75                 {
76                         fprintf(stderr, "ERROR, cannot open logfile %s: %s\n",
77                                 logfile, strerror(errno));
78                         exit(1);
79                 }
80         
81                 /* set unbuffered operation */
82         
83                 setvbuf(logfp, (char *)NULL, _IONBF, 0);
84         }
85         else
86         {
87 #if DEBUG
88                 if(do_debug && do_fork == 0 && do_fullscreen == 0)
89                         (void)openlog("isdnd",
90                                 LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR,
91                                 logfacility);
92                 else
93 #endif
94                 (void)openlog("isdnd", LOG_PID|LOG_CONS|LOG_NDELAY,
95                                 logfacility);
96         }
97
98         /* initialize the regexp array */
99
100         for(i = 0; i < MAX_RE; i++)
101         {
102                 char *p;
103                 char buf[64];
104
105                 snprintf(buf, sizeof(buf), "%s%d", REGPROG_DEF, i);
106
107                 rarr[i].re_flg = 0;
108
109                 if((p = malloc(strlen(buf) + 1)) == NULL)
110                 {
111                         log(LL_DBG, "init_log: malloc failed: %s", strerror(errno));
112                         do_exit(1);
113                 }
114
115                 strcpy(p, buf);
116
117                 rarr[i].re_prog = p;
118         }
119 }
120
121 /*---------------------------------------------------------------------------*
122  *      finish logging
123  *---------------------------------------------------------------------------*/
124 void
125 finish_log(void)
126 {
127         if(uselogfile && logfp)
128         {
129                 fflush(logfp);
130                 fclose(logfp);
131         }
132         else
133         {
134                 (void)closelog();
135         }
136 }
137
138 /*---------------------------------------------------------------------------*
139  *      place entry into logfile
140  *---------------------------------------------------------------------------*/
141 void
142 log(int what, const char *fmt, ...)
143 {
144         char buffer[LOGBUFLEN];
145         register char *dp;
146         va_list ap;
147
148         va_start(ap, fmt);
149         vsnprintf(buffer, LOGBUFLEN-1, fmt, ap);
150         va_end(ap);
151         
152         dp = getlogdatetime();  /* get time string ptr */
153         
154 #ifdef USE_CURSES
155
156         /* put log on screen ? */
157
158         if((do_fullscreen && curses_ready) &&
159            ((!debug_noscreen) || (debug_noscreen && (what != LL_DBG))))
160         {
161                 wprintw(lower_w, "%s %s %-.*s\n", dp, logtab[what].text,
162
163 /*
164  * FreeBSD-current integrated ncurses. Since then it is no longer possible
165  * to write to the last column in the logfilewindow without causing an
166  * automatic newline to occur resulting in a blank line in that window.
167  */
168 #ifdef __FreeBSD__
169 #include <osreldate.h>
170 #endif
171 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400009           
172 #warning "FreeBSD ncurses is buggy: write to last column = auto newline!"
173                      COLS-((strlen(dp))+(strlen(logtab[what].text))+3), buffer);
174 #else
175                      (int)(COLS-((strlen(dp))+(strlen(logtab[what].text))+2)), buffer);
176 #endif
177                 wrefresh(lower_w);
178         }
179 #endif
180
181 #ifdef I4B_EXTERNAL_MONITOR
182         if(what != LL_MER) /* don't send monitor errs, endless loop !!! */
183                 monitor_evnt_log(logtab[what].pri, logtab[what].text, buffer);
184 #endif
185
186         if(uselogfile)
187         {
188                 fprintf(logfp, "%s %s %s\n", dp, logtab[what].text, buffer);
189         }
190         else
191         {
192                 register char *s = buffer;
193                 
194                 /* strip leading spaces from syslog output */
195                 
196                 while(*s && (*s == ' '))
197                         s++;
198                         
199                 syslog(logtab[what].pri, "%s %s", logtab[what].text, s);
200         }
201
202
203 #if DEBUG
204         if(what != LL_DBG) /* don't check debug logs, endless loop !!! */
205 #endif
206                 check_reg(buffer);
207 }
208
209 /*---------------------------------------------------------------------------*
210  *      return ptr to static area containing date/time
211  *---------------------------------------------------------------------------*/
212 char *
213 getlogdatetime()
214 {
215         static char logdatetime[41];
216         time_t tim;
217         register struct tm *tp;
218         
219         tim = time(NULL);
220         tp = localtime(&tim);
221         strftime(logdatetime,40,I4B_TIME_FORMAT,tp);
222         return(logdatetime);
223 }
224
225 /*---------------------------------------------------------------------------*
226  *      check for a match in the regexp array
227  *---------------------------------------------------------------------------*/
228 static void
229 check_reg(char *logstring)
230 {
231         register int i;
232
233         for(i = 0; i < MAX_RE; i++)
234         {
235                 if(rarr[i].re_flg && (!regexec(&(rarr[i].re), logstring, (size_t) 0, NULL, 0)))
236                 {
237                         char* argv[3];
238                         argv[0] = rarr[i].re_prog;
239                         argv[1] = logstring;
240                         argv[2] = NULL;
241
242                         exec_prog(rarr[i].re_prog, argv);
243                         break;
244                 }
245         }
246 }
247
248 /* EOF */