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