2 * Copyright (c) 1997, 2001 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 * -----------------------------
32 * last edit-date: [Sat May 13 13:07:18 2006]
34 *---------------------------------------------------------------------------*/
37 #include <osreldate.h>
44 extern int do_monitor;
48 static void check_reg(char *logstring);
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 */
68 /*---------------------------------------------------------------------------*
70 *---------------------------------------------------------------------------*/
78 if((logfp = fopen(logfile, "a")) == NULL)
80 fprintf(stderr, "ERROR, cannot open logfile %s: %s\n",
81 logfile, strerror(errno));
85 /* set unbuffered operation */
87 setvbuf(logfp, (char *)NULL, _IONBF, 0);
92 if(do_debug && do_fork == 0 && do_fullscreen == 0)
93 (void)openlog("isdnd",
94 LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR,
98 (void)openlog("isdnd", LOG_PID|LOG_CONS|LOG_NDELAY,
102 /* initialize the regexp array */
104 for(i = 0; i < MAX_RE; i++)
109 snprintf(buf, sizeof(buf), "%s%d", REGPROG_DEF, i);
113 if((p = malloc(strlen(buf) + 1)) == NULL)
115 llog(LL_DBG, "init_log: malloc failed: %s", strerror(errno));
125 /*---------------------------------------------------------------------------*
127 *---------------------------------------------------------------------------*/
131 if(uselogfile && logfp)
142 /*---------------------------------------------------------------------------*
143 * place entry into logfile
144 *---------------------------------------------------------------------------*/
146 llog(int what, const char *fmt, ...)
148 char buffer[LOGBUFLEN];
153 vsnprintf(buffer, LOGBUFLEN-1, fmt, ap);
156 dp = getlogdatetime(); /* get time string ptr */
160 /* put log on screen ? */
162 if((do_fullscreen && curses_ready) &&
163 ((!debug_noscreen) || (debug_noscreen && (what != LL_DBG))))
165 wprintw(lower_w, "%s %s %-.*s\n", dp, logtab[what].text,
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.
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);
176 (int)(COLS-((strlen(dp))+(strlen(logtab[what].text))+2)), buffer);
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);
189 fprintf(logfp, "%s %s %s\n", dp, logtab[what].text, buffer);
193 register char *s = buffer;
195 /* strip leading spaces from syslog output */
197 while(*s && (*s == ' '))
200 syslog(logtab[what].pri, "%s %s", logtab[what].text, s);
205 if(what != LL_DBG) /* don't check debug logs, endless loop !!! */
210 /*---------------------------------------------------------------------------*
211 * return ptr to static area containing date/time
212 *---------------------------------------------------------------------------*/
216 static char logdatetime[41];
218 register struct tm *tp;
221 tp = localtime(&tim);
222 strftime(logdatetime,40,I4B_TIME_FORMAT,tp);
226 /*---------------------------------------------------------------------------*
227 * check for a match in the regexp array
228 *---------------------------------------------------------------------------*/
230 check_reg(char *logstring)
234 for(i = 0; i < MAX_RE; i++)
236 if(rarr[i].re_flg && (!regexec(&(rarr[i].re), logstring, (size_t) 0, NULL, 0)))
239 argv[0] = rarr[i].re_prog;
243 exec_prog(rarr[i].re_prog, argv);