summaryrefslogtreecommitdiffstats
path: root/usr.sbin/i4b/isdnd/log.c
blob: 25a0307a76205844302cd68cd62740f4b4b6da0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *---------------------------------------------------------------------------
 *
 *	i4b daemon - logging routines
 *	-----------------------------
 *
 * $FreeBSD$ 
 *
 *      last edit-date: [Sun Feb 14 10:11:18 1999]
 *
 *---------------------------------------------------------------------------*/

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <regex.h>

#include <machine/i4b_ioctl.h>

#include "isdnd.h"

#define LOGBUFLEN 256

extern int do_monitor;
extern int accepted;
extern FILE *logfp;

static void check_reg(char *logstring);

struct logtab {
	char *text;
	int pri;
};

/*---------------------------------------------------------------------------*
 * 	table for converting internal log levels into syslog levels
 *---------------------------------------------------------------------------*/
static struct logtab logtab[] = {
	{"ERR", LOG_ERR},	/* error conditions */
	{"WRN", LOG_WARNING},   /* warning conditions, nonfatal */
	{"DMN", LOG_NOTICE},	/* normal but significant condition, daemon*/
	{"CHD", LOG_INFO},	/* informational, call handling */
	{"DBG", LOG_DEBUG}	/* debug messages */
};

/*---------------------------------------------------------------------------*
 *	initialize logging
 *---------------------------------------------------------------------------*/
void
init_log(void)
{
	int i;

	if(uselogfile)
	{
		if((logfp = fopen(logfile, "a")) == NULL)
		{
			fprintf(stderr, "ERROR, cannot open logfile %s: %s\n",
				logfile, strerror(errno));
			exit(1);
		}
	
		/* set unbuffered operation */
	
		setvbuf(logfp, (char *)NULL, _IONBF, 0);
	}
	else
	{
#if DEBUG
		if(do_debug && do_fork == 0 && do_fullscreen == 0)
			(void)openlog("isdnd",
				LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR,
				logfacility);
		else
#endif
		(void)openlog("isdnd", LOG_PID|LOG_CONS|LOG_NDELAY,
				logfacility);
	}

	/* initialize the regexp array */

	for(i = 0; i < MAX_RE; i++)
	{
		char *p;
		char buf[64];

		sprintf(buf, "%s%d", REGPROG_DEF, i);

		rarr[i].re_flg = 0;

		if((p = malloc(strlen(buf) + 1)) == NULL)
		{
			log(LL_DBG, "init_log: malloc failed: %s", strerror(errno));
			do_exit(1);
		}

		strcpy(p, buf);

		rarr[i].re_prog = p;
	}
}

/*---------------------------------------------------------------------------*
 *	finish logging
 *---------------------------------------------------------------------------*/
void
finish_log(void)
{
	if(uselogfile)
	{
		fflush(logfp);
		fclose(logfp);
	}
	else
	{
		(void)closelog();
	}
}

/*---------------------------------------------------------------------------*
 *	place entry into logfile
 *---------------------------------------------------------------------------*/
void
log(int what, const char *fmt, ...)
{
	char buffer[LOGBUFLEN];
	register char *dp;
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buffer, LOGBUFLEN-1, fmt, ap);
	va_end(ap);
	
	dp = getlogdatetime();	/* get time string ptr */
	
#ifdef USE_CURSES

	/* put log on screen ? */

	if((do_fullscreen && curses_ready) &&
	   ((!debug_noscreen) || (debug_noscreen && (what != LL_DBG))))
	{
		wprintw(lower_w, "%s %s %-.*s\n", dp, logtab[what].text,
		     COLS-((strlen(dp))+(strlen(logtab[what].text))+2), buffer);
		wrefresh(lower_w);
	}
#endif

#ifdef I4B_EXTERNAL_MONITOR
	monitor_evnt_log(logtab[what].pri, logtab[what].text, buffer);
#endif

	if(uselogfile)
	{
		fprintf(logfp, "%s %s %s\n", dp, logtab[what].text, buffer);
	}
	else
	{
		register char *s = buffer;
		
		/* strip leading spaces from syslog output */
		
		while(*s && (*s == ' '))
			s++;
			
		syslog(logtab[what].pri, "%s %s", logtab[what].text, s);
	}


#if DEBUG
	if(what != LL_DBG) /* don't check debug logs, endless loop !!! */
#endif
		check_reg(buffer);
}

/*---------------------------------------------------------------------------*
 *	return ptr to static area containing date/time
 *---------------------------------------------------------------------------*/
char *
getlogdatetime()
{
	static char logdatetime[41];
	time_t tim;
	register struct tm *tp;
	
	tim = time(NULL);
	tp = localtime(&tim);
	strftime(logdatetime,40,I4B_TIME_FORMAT,tp);
	return(logdatetime);
}

/*---------------------------------------------------------------------------*
 *	check for a match in the regexp array
 *---------------------------------------------------------------------------*/
static void
check_reg(char *logstring)
{
	register int i;

	for(i = 0; i < MAX_RE; i++)
	{
		if(rarr[i].re_flg && (!regexec(&(rarr[i].re), logstring, (size_t) 0, NULL, 0)))
		{
			char* argv[3];
			argv[0] = rarr[i].re_prog;
			argv[1] = logstring;
			argv[2] = NULL;

			exec_prog(rarr[i].re_prog, argv);
			break;
		}
	}
}

/* EOF */
OpenPOWER on IntegriCloud