Source

asterisk / logger.c

Full commit
  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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 * Asterisk Logger
 * 
 * Mark Spencer <markster@marko.net>
 *
 * Copyright(C)1999, Linux Support Services, Inc.
 * 
 * Distributed under the terms of the GNU General Public License (GPL) Version 2
 *
 * Logging routines
 *
 */

#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <asterisk/lock.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>
#include <asterisk/term.h>
#include <asterisk/cli.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/stat.h>
#include "asterisk.h"
#include "astconf.h"

#define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
		        from <syslog.h> which is included by logger.h */
#include <syslog.h>
static int syslog_level_map[] = {
	LOG_DEBUG,
	LOG_INFO,    /* arbitrary equivalent of LOG_EVENT */
	LOG_NOTICE,
	LOG_WARNING,
	LOG_ERR
};
#define SYSLOG_NLEVELS 5

#include <asterisk/logger.h>

#define MAX_MSG_QUEUE 200

static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
static ast_mutex_t loglock = AST_MUTEX_INITIALIZER;

static struct msglist {
	char *msg;
	struct msglist *next;
} *list = NULL, *last = NULL;

struct logchannel {
        int logmask;
        int facility; /* syslog */
	int syslog; /* syslog flag */
        int console;  /* console logging */
	FILE *fileptr; /* logfile logging */
	char filename[256];
        struct logchannel *next;
};

static struct logchannel *logchannels = NULL;

static int msgcnt = 0;

static FILE *eventlog = NULL;

static char *levels[] = {
       "DEBUG",
       "EVENT",
       "NOTICE",
       "WARNING",
       "ERROR"
};

static int colors[] = {
       COLOR_BRGREEN,
       COLOR_BRBLUE,
       COLOR_YELLOW,
       COLOR_BRRED,
       COLOR_RED
};

static int make_components(char *s, int lineno)
{
	char *w;
	int res = 0;
	char *stringp=NULL;
	stringp=s;
	w = strsep(&stringp, ",");
	while(w) {
	    while(*w && (*w < 33))
		w++;
	    if (!strcasecmp(w, "error")) 
		res |= (1 << __LOG_ERROR);
	    else if (!strcasecmp(w, "warning"))
		res |= (1 << __LOG_WARNING);
	    else if (!strcasecmp(w, "notice"))
		res |= (1 << __LOG_NOTICE);
	    else if (!strcasecmp(w, "event"))
		res |= (1 << __LOG_EVENT);
	    else if (!strcasecmp(w, "debug"))
		res |= (1 << __LOG_DEBUG);
	    else {
		fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
	    }
	    w = strsep(&stringp, ",");
	}
	return res;
}

static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
{
	struct logchannel *chan;
	char *facility;
	CODE *cptr;

	if (!strlen(channel))
		return NULL;
	chan = malloc(sizeof(struct logchannel));

	if (chan) {
		memset(chan, 0, sizeof(struct logchannel));
		if (!strcasecmp(channel, "console")) {
		    chan->console = 1;
		} else if (!strncasecmp(channel, "syslog", 6)) {
		    /*
		     * syntax is:
		     *  syslog.facility => level,level,level
		     */
		    facility = strchr(channel, '.');
		    if(!facility++ || !facility) {
			facility = "local0";
		    }
		    /*
		     * Walk through the list of facilitynames (defined in sys/syslog.h)
		     * to see if we can find the one we have been given
		     */
		    chan->facility = -1;
		    cptr = facilitynames;
		    while (cptr->c_name) {
			if (!strncasecmp(facility, cptr->c_name, sizeof(cptr->c_name))) {
			    chan->facility = cptr->c_val;
			    break;
			}
			cptr++;
		    }
		    if (0 > chan->facility) {
			fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
			free(chan);
			return NULL;
		    }

		    chan->syslog = 1;
		    openlog("asterisk", LOG_PID, chan->facility);
		} else {
			if (channel[0] == '/') 
				strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
			else
				snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
			chan->fileptr = fopen(chan->filename, "a");
			if (!chan->fileptr) {
				/* Can't log here, since we're called with a lock */
				fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
			}
		}
		chan->logmask = make_components(components, lineno);
	}
	return chan;
}

static void init_logger_chain(void)
{
	struct logchannel *chan, *cur;
	struct ast_config *cfg;
	struct ast_variable *var;

	/* delete our list of log channels */
	ast_mutex_lock(&loglock);
	chan = logchannels;
	while (chan) {
	    cur = chan->next;
	    free(chan);
	    chan = cur;
	}
	logchannels = NULL;
	ast_mutex_unlock(&loglock);

	/* close syslog */
	closelog();

	cfg = ast_load("logger.conf");
	
	/* If no config file, we're fine */
	if (!cfg)
	    return;

	ast_mutex_lock(&loglock);
	var = ast_variable_browse(cfg, "logfiles");
	while(var) {
		chan = make_logchannel(var->name, var->value, var->lineno);
		if (chan) {
			chan->next = logchannels;
			logchannels = chan;
		}
		var = var->next;
	}

	ast_destroy(cfg);
	ast_mutex_unlock(&loglock);
}

int reload_logger(int rotate)
{
	char old[AST_CONFIG_MAX_PATH];
	char new[AST_CONFIG_MAX_PATH];
	struct logchannel *f;
	FILE *myf;

	int x;

	ast_mutex_lock(&loglock);
	if (eventlog) 
		fclose(eventlog);
	else 
		rotate = 0;
	eventlog = NULL;



	mkdir((char *)ast_config_AST_LOG_DIR, 0755);
	snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);

	if(rotate) {
		for(x=0;;x++) {
			snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
			myf = fopen((char *)new, "r");
			if(myf) 
				fclose(myf);
			else
				break;
		}
	
		/* do it */
		if (rename(old,new))
			fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
	}

	eventlog = fopen(old, "a");

	f = logchannels;
	while(f) {
		if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
			fclose(f->fileptr);
			f->fileptr = NULL;
			if(rotate) {
				strncpy(old, f->filename, sizeof(old));
	
				for(x=0;;x++) {
					snprintf(new, sizeof(new), "%s.%d", f->filename, x);
					myf = fopen((char *)new, "r");
					if (myf) {
						fclose(myf);
					} else {
						break;
					}
				}
	    
				/* do it */
				if (rename(old,new))
					fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
			}
		}
		f = f->next;
	}

	ast_mutex_unlock(&loglock);

	if (eventlog) {
		init_logger_chain();
		ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
		if (option_verbose)
			ast_verbose("Asterisk Event Logger restarted\n");
		return 0;
	} else 
		ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
	init_logger_chain();
	return -1;
}

static int handle_logger_reload(int fd, int argc, char *argv[])
{
	if(reload_logger(0))
	{
		ast_cli(fd, "Failed to reloadthe logger\n");
		return RESULT_FAILURE;
	}
	else
		return RESULT_SUCCESS;
}

static int handle_logger_rotate(int fd, int argc, char *argv[])
{
	if(reload_logger(1))
	{
		ast_cli(fd, "Failed to reloadthe logger\n");
		return RESULT_FAILURE;
	}
	else
		return RESULT_SUCCESS;
}

static struct verb {
	void (*verboser)(const char *string, int opos, int replacelast, int complete);
	struct verb *next;
} *verboser = NULL;


static char logger_reload_help[] =
"Usage: logger reload\n"
"       Reloads the logger subsystem state.  Use after restarting syslogd(8)\n";

static char logger_rotate_help[] =
"Usage: logger reload\n"
"       Rotates and Reopens the log files.\n";

static struct ast_cli_entry reload_logger_cli = 
	{ { "logger", "reload", NULL }, 
	handle_logger_reload, "Reopens the log files",
	logger_reload_help };

static struct ast_cli_entry rotate_logger_cli = 
	{ { "logger", "rotate", NULL }, 
	handle_logger_rotate, "Reopens the log files",
	logger_rotate_help };

static int handle_SIGXFSZ(int sig) {
    reload_logger(1);
    ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
    if (option_verbose)
	    ast_verbose("Rotated Logs Per SIGXFSZ\n");
    return 0;
}

int init_logger(void)
{
	char tmp[256];

	/* auto rotate if sig SIGXFSZ comes a-knockin */
	(void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);

	/* register the relaod logger cli command */
	ast_cli_register(&reload_logger_cli);
	ast_cli_register(&rotate_logger_cli);

	/* create the eventlog */
	mkdir((char *)ast_config_AST_LOG_DIR, 0755);
	snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
	eventlog = fopen((char *)tmp, "a");
	if (eventlog) {
		init_logger_chain();
		ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
		if (option_verbose)
			ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
		return 0;
	} else 
		ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));

	init_logger_chain();

	/* create log channels */
	init_logger_chain();
	return -1;
}

static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) {
    char buf[BUFSIZ];

    if(level >= SYSLOG_NLEVELS) {
	    /* we are locked here, so cannot ast_log() */
	    fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
	    return;
    }
    if(file && line && function) {
	snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
		 levels[level], (long)pthread_self(), file, line, function);
    } else {
	snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)pthread_self());
	level = __LOG_DEBUG;
    }
    vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
    syslog(syslog_level_map[level], buf);
}

/*
 * send log messages to syslog and/or the console
 */
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
{
    struct logchannel *chan;
    char buf[BUFSIZ];
    time_t t;
    struct tm tm;
    char date[256];

    va_list ap;
	
    if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
	return;
    }

    /* begin critical section */
    ast_mutex_lock(&loglock);

    time(&t);
    localtime_r(&t, &tm);
    strftime(date, sizeof(date), "%b %e %T", &tm);


    if (level == __LOG_EVENT) {
	    va_start(ap, fmt);

	    fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
	    vfprintf(eventlog, fmt, ap);
	    fflush(eventlog);

	    va_end(ap);
	    ast_mutex_unlock(&loglock);
	    return;
    }

    if (logchannels) {
	chan = logchannels;
	while(chan) {
	    if (chan->syslog && (chan->logmask & (1 << level))) {
		va_start(ap, fmt);
		ast_log_vsyslog(level, file, line, function, fmt, ap);
		va_end(ap);
	    } else if ((chan->logmask & (1 << level)) && (chan->console)) {
		char linestr[128];
		char tmp1[80], tmp2[80], tmp3[80], tmp4[80];

		sprintf(linestr, "%d", line);
		snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
			 date,
			 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
			 (long)pthread_self(),
			 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
			 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
			 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));

		ast_console_puts(buf);
		va_start(ap, fmt);
		vsnprintf(buf, sizeof(buf), fmt, ap);
		va_end(ap);
		ast_console_puts(buf);
	    } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
		    snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
			     levels[level], (long)pthread_self());
		    fprintf(chan->fileptr, buf);
		    va_start(ap, fmt);
		    vsnprintf(buf, sizeof(buf), fmt, ap);
		    va_end(ap);
		    fprintf(chan->fileptr, buf);
	    }
	    chan = chan->next;
	}
    } else {
	    /* 
	     * we don't have the logger chain configured yet,
	     * so just log to stdout 
	     */
	    va_start(ap, fmt);
	    vsnprintf(buf, sizeof(buf), fmt, ap);
	    va_end(ap);
	    fprintf(stdout, buf);
    }

    ast_mutex_unlock(&loglock);
    /* end critical section */
}

extern void ast_verbose(const char *fmt, ...)
{
	static char stuff[4096];
	static int pos = 0, opos;
	static int replacelast = 0, complete;
	struct msglist *m;
	struct verb *v;
	va_list ap;
	va_start(ap, fmt);
	ast_mutex_lock(&msglist_lock);
	vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
	opos = pos;
	pos = strlen(stuff);
	if (fmt[strlen(fmt)-1] == '\n') 
		complete = 1;
	else
		complete=0;
	if (complete) {
		if (msgcnt < MAX_MSG_QUEUE) {
			/* Allocate new structure */
			m = malloc(sizeof(struct msglist));
			msgcnt++;
		} else {
			/* Recycle the oldest entry */
			m = list;
			list = list->next;
			free(m->msg);
		}
		if (m) {
			m->msg = strdup(stuff);
			if (m->msg) {
				if (last)
					last->next = m;
				else
					list = m;
				m->next = NULL;
				last = m;
			} else {
				msgcnt--;
				ast_log(LOG_ERROR, "Out of memory\n");
				free(m);
			}
		}
	}
	if (verboser) {
		v = verboser;
		while(v) {
			v->verboser(stuff, opos, replacelast, complete);
			v = v->next;
		}
	} /* else
		fprintf(stdout, stuff + opos); */

	if (fmt[strlen(fmt)-1] != '\n') 
		replacelast = 1;
	else 
		replacelast = pos = 0;
	va_end(ap);

	ast_mutex_unlock(&msglist_lock);
}

int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
{
	struct msglist *m;
	m = list;
	ast_mutex_lock(&msglist_lock);
	while(m) {
		/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
		v(m->msg, 0, 0, 1);
		m = m->next;
	}
	ast_mutex_unlock(&msglist_lock);
	return 0;
}

int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete)) 
{
	struct msglist *m;
	struct verb *tmp;
	/* XXX Should be more flexible here, taking > 1 verboser XXX */
	if ((tmp = malloc(sizeof (struct verb)))) {
		tmp->verboser = v;
		ast_mutex_lock(&msglist_lock);
		tmp->next = verboser;
		verboser = tmp;
		m = list;
		while(m) {
			/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
			v(m->msg, 0, 0, 1);
			m = m->next;
		}
		ast_mutex_unlock(&msglist_lock);
		return 0;
	}
	return -1;
}

int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
{
	int res = -1;
	struct verb *tmp, *tmpl=NULL;
	ast_mutex_lock(&msglist_lock);
	tmp = verboser;
	while(tmp) {
		if (tmp->verboser == v)	{
			if (tmpl)
				tmpl->next = tmp->next;
			else
				verboser = tmp->next;
			free(tmp);
			break;
		}
		tmpl = tmp;
		tmp = tmp->next;
	}
	if (tmp)
		res = 0;
	ast_mutex_unlock(&msglist_lock);
	return res;
}