]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/i4b/isdnd/log.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / 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: [Sat May 13 13:07:18 2006]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #ifdef __FreeBSD__
37 #include <osreldate.h>
38 #endif
39
40 #include "isdnd.h"
41
42 #define LOGBUFLEN 256
43
44 extern int do_monitor;
45 extern int accepted;
46 extern FILE *logfp;
47
48 static void check_reg(char *logstring);
49
50 struct logtab {
51         char *text;
52         int pri;
53 };
54
55 /*---------------------------------------------------------------------------*
56  *      table for converting internal log levels into syslog levels
57  *---------------------------------------------------------------------------*/
58 static struct logtab logtab[] = {
59         {"ERR", LOG_ERR},       /* error conditions                     */
60         {"WRN", LOG_WARNING},   /* warning conditions, nonfatal         */
61         {"DMN", LOG_NOTICE},    /* significant conditions of the daemon */
62         {"CHD", LOG_INFO},      /* informational, call handling         */
63         {"DBG", LOG_DEBUG},     /* debug messages                       */
64         {"MER", LOG_ERR},       /* monitor error conditions             */      
65         {"PKT", LOG_INFO}       /* packet logging                       */
66 };
67
68 /*---------------------------------------------------------------------------*
69  *      initialize logging
70  *---------------------------------------------------------------------------*/
71 void
72 init_log(void)
73 {
74         int i;
75
76         if(uselogfile)
77         {
78                 if((logfp = fopen(logfile, "a")) == NULL)
79                 {
80                         fprintf(stderr, "ERROR, cannot open logfile %s: %s\n",
81                                 logfile, strerror(errno));
82                         exit(1);
83                 }
84         
85                 /* set unbuffered operation */
86         
87                 setvbuf(logfp, (char *)NULL, _IONBF, 0);
88         }
89         else
90         {
91 #if DEBUG
92                 if(do_debug && do_fork == 0 && do_fullscreen == 0)
93                         (void)openlog("isdnd",
94                                 LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR,
95                                 logfacility);
96                 else
97 #endif
98                 (void)openlog("isdnd", LOG_PID|LOG_CONS|LOG_NDELAY,
99                                 logfacility);
100         }
101
102         /* initialize the regexp array */
103
104         for(i = 0; i < MAX_RE; i++)
105         {
106                 char *p;
107                 char buf[64];
108
109                 snprintf(buf, sizeof(buf), "%s%d", REGPROG_DEF, i);
110
111                 rarr[i].re_flg = 0;
112
113                 if((p = malloc(strlen(buf) + 1)) == NULL)
114                 {
115                         llog(LL_DBG, "init_log: malloc failed: %s", strerror(errno));
116                         do_exit(1);
117                 }
118
119                 strcpy(p, buf);
120
121                 rarr[i].re_prog = p;
122         }
123 }
124
125 /*---------------------------------------------------------------------------*
126  *      finish logging
127  *---------------------------------------------------------------------------*/
128 void
129 finish_log(void)
130 {
131         if(uselogfile && logfp)
132         {
133                 fflush(logfp);
134                 fclose(logfp);
135         }
136         else
137         {
138                 (void)closelog();
139         }
140 }
141
142 /*---------------------------------------------------------------------------*
143  *      place entry into logfile
144  *---------------------------------------------------------------------------*/
145 void
146 llog(int what, const char *fmt, ...)
147 {
148         char buffer[LOGBUFLEN];
149         register char *dp;
150         va_list ap;
151
152         va_start(ap, fmt);
153         vsnprintf(buffer, LOGBUFLEN-1, fmt, ap);
154         va_end(ap);
155         
156         dp = getlogdatetime();  /* get time string ptr */
157         
158 #ifdef USE_CURSES
159
160         /* put log on screen ? */
161
162         if((do_fullscreen && curses_ready) &&
163            ((!debug_noscreen) || (debug_noscreen && (what != LL_DBG))))
164         {
165                 wprintw(lower_w, "%s %s %-.*s\n", dp, logtab[what].text,
166
167 /*
168  * FreeBSD-current integrated ncurses. Since then it is no longer possible
169  * to write to the last column in the logfilewindow without causing an
170  * automatic newline to occur resulting in a blank line in that window.
171  */
172 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400009           
173 #warning "FreeBSD ncurses is buggy: write to last column = auto newline!"
174                      COLS-((strlen(dp))+(strlen(logtab[what].text))+3), buffer);
175 #else
176                      (int)(COLS-((strlen(dp))+(strlen(logtab[what].text))+2)), buffer);
177 #endif
178                 wrefresh(lower_w);
179         }
180 #endif
181
182 #ifdef I4B_EXTERNAL_MONITOR
183         if(what != LL_MER) /* don't send monitor errs, endless loop !!! */
184                 monitor_evnt_log(logtab[what].pri, logtab[what].text, buffer);
185 #endif
186
187         if(uselogfile)
188         {
189                 fprintf(logfp, "%s %s %s\n", dp, logtab[what].text, buffer);
190         }
191         else
192         {
193                 register char *s = buffer;
194                 
195                 /* strip leading spaces from syslog output */
196                 
197                 while(*s && (*s == ' '))
198                         s++;
199                         
200                 syslog(logtab[what].pri, "%s %s", logtab[what].text, s);
201         }
202
203
204 #if DEBUG
205         if(what != LL_DBG) /* don't check debug logs, endless loop !!! */
206 #endif
207                 check_reg(buffer);
208 }
209
210 /*---------------------------------------------------------------------------*
211  *      return ptr to static area containing date/time
212  *---------------------------------------------------------------------------*/
213 char *
214 getlogdatetime()
215 {
216         static char logdatetime[41];
217         time_t tim;
218         register struct tm *tp;
219         
220         tim = time(NULL);
221         tp = localtime(&tim);
222         strftime(logdatetime,40,I4B_TIME_FORMAT,tp);
223         return(logdatetime);
224 }
225
226 /*---------------------------------------------------------------------------*
227  *      check for a match in the regexp array
228  *---------------------------------------------------------------------------*/
229 static void
230 check_reg(char *logstring)
231 {
232         register int i;
233
234         for(i = 0; i < MAX_RE; i++)
235         {
236                 if(rarr[i].re_flg && (!regexec(&(rarr[i].re), logstring, (size_t) 0, NULL, 0)))
237                 {
238                         char* argv[3];
239                         argv[0] = rarr[i].re_prog;
240                         argv[1] = logstring;
241                         argv[2] = NULL;
242
243                         exec_prog(rarr[i].re_prog, argv);
244                         break;
245                 }
246         }
247 }
248
249 /* EOF */