2 * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 *---------------------------------------------------------------------------
27 * i4b daemon - logging routines
28 * -----------------------------
30 * $Id: log.c,v 1.25 2000/10/09 12:53:29 hm Exp $
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.6 2004/12/18 23:48:03 swildner Exp $
35 * last edit-date: [Mon Dec 13 21:47:28 1999]
37 *---------------------------------------------------------------------------*/
43 extern int do_monitor
;
47 static void check_reg(char *logstring
);
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 */
67 /*---------------------------------------------------------------------------*
69 *---------------------------------------------------------------------------*/
77 if((logfp
= fopen(logfile
, "a")) == NULL
)
79 fprintf(stderr
, "ERROR, cannot open logfile %s: %s\n",
80 logfile
, strerror(errno
));
84 /* set unbuffered operation */
86 setvbuf(logfp
, (char *)NULL
, _IONBF
, 0);
91 if(do_debug
&& do_fork
== 0 && do_fullscreen
== 0)
93 LOG_PID
|LOG_CONS
|LOG_NDELAY
|LOG_PERROR
,
97 openlog("isdnd", LOG_PID
|LOG_CONS
|LOG_NDELAY
, logfacility
);
100 /* initialize the regexp array */
102 for(i
= 0; i
< MAX_RE
; i
++)
107 snprintf(buf
, sizeof(buf
), "%s%d", REGPROG_DEF
, i
);
111 if((p
= malloc(strlen(buf
) + 1)) == NULL
)
113 log(LL_DBG
, "init_log: malloc failed: %s", strerror(errno
));
123 /*---------------------------------------------------------------------------*
125 *---------------------------------------------------------------------------*/
140 /*---------------------------------------------------------------------------*
141 * place entry into logfile
142 *---------------------------------------------------------------------------*/
144 log(int what
, const char *fmt
, ...)
146 char buffer
[LOGBUFLEN
];
151 vsnprintf(buffer
, LOGBUFLEN
-1, fmt
, ap
);
154 dp
= getlogdatetime(); /* get time string ptr */
158 /* put log on screen ? */
160 if((do_fullscreen
&& curses_ready
) &&
161 ((!debug_noscreen
) || (debug_noscreen
&& (what
!= LL_DBG
))))
163 wprintw(lower_w
, "%s %s %-.*s\n", dp
, logtab
[what
].text
,
166 * FreeBSD-current integrated ncurses. Since then it is no longer possible
167 * to write to the last column in the logfilewindow without causing an
168 * automatic newline to occur resulting in a blank line in that window.
171 #include <osreldate.h>
173 #if defined(__DragonFly__)
174 #warning "FreeBSD ncurses is buggy: write to last column = auto newline!"
175 COLS
-((strlen(dp
))+(strlen(logtab
[what
].text
))+3), buffer
);
177 (int)(COLS
-((strlen(dp
))+(strlen(logtab
[what
].text
))+2)), buffer
);
183 #ifdef I4B_EXTERNAL_MONITOR
184 if(what
!= LL_MER
) /* don't send monitor errs, endless loop !!! */
185 monitor_evnt_log(logtab
[what
].pri
, logtab
[what
].text
, buffer
);
190 fprintf(logfp
, "%s %s %s\n", dp
, logtab
[what
].text
, buffer
);
196 /* strip leading spaces from syslog output */
198 while(*s
&& (*s
== ' '))
201 syslog(logtab
[what
].pri
, "%s %s", logtab
[what
].text
, s
);
206 if(what
!= LL_DBG
) /* don't check debug logs, endless loop !!! */
211 /*---------------------------------------------------------------------------*
212 * return ptr to static area containing date/time
213 *---------------------------------------------------------------------------*/
217 static char logdatetime
[41];
222 tp
= localtime(&tim
);
223 strftime(logdatetime
,40,I4B_TIME_FORMAT
,tp
);
227 /*---------------------------------------------------------------------------*
228 * check for a match in the regexp array
229 *---------------------------------------------------------------------------*/
231 check_reg(char *logstring
)
235 for(i
= 0; i
< MAX_RE
; i
++)
237 if(rarr
[i
].re_flg
&& (!regexec(&(rarr
[i
].re
), logstring
, (size_t) 0, NULL
, 0)))
240 argv
[0] = rarr
[i
].re_prog
;
244 exec_prog(rarr
[i
].re_prog
, argv
);