Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

logger.c File Reference

#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 <asterisk/utils.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/stat.h>
#include "asterisk.h"
#include "astconf.h"
#include <syslog.h>
#include <asterisk/logger.h>

Include dependency graph for logger.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  msglist
struct  logchannel
struct  verb

Defines

#define SYSLOG_NAMES
#define SYSLOG_NLEVELS   6
#define MAX_MSG_QUEUE   200

Functions

 AST_MUTEX_DEFINE_STATIC (msglist_lock)
 AST_MUTEX_DEFINE_STATIC (loglock)
 AST_MUTEX_DEFINE_STATIC (qloglock)
void ast_queue_log (const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt,...)
int reload_logger (int rotate)
int init_logger (void)
void ast_log (int level, const char *file, int line, const char *function, const char *fmt,...)
void ast_verbose (const char *fmt,...)
int ast_verbose_dmesg (void(*v)(const char *string, int opos, int replacelast, int complete))
int ast_register_verbose (void(*v)(const char *string, int opos, int replacelast, int complete))
int ast_unregister_verbose (void(*v)(const char *string, int opos, int replacelast, int complete))


Define Documentation

#define MAX_MSG_QUEUE   200
 

Definition at line 49 of file logger.c.

Referenced by ast_verbose().

#define SYSLOG_NAMES
 

Value:

/* so we can map syslog facilities names to their numeric values, from <syslog.h> which is included by logger.h */

Definition at line 34 of file logger.c.

#define SYSLOG_NLEVELS   6
 

Definition at line 45 of file logger.c.


Function Documentation

void ast_log int  level,
const char *  file,
int  line,
const char *  function,
const char *  fmt,
  ...
 

Definition at line 455 of file logger.c.

References __LOG_DEBUG, __LOG_EVENT, __LOG_VERBOSE, ast_console_puts(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_verbose(), COLOR_BRWHITE, LOG_EVENT, option_debug, option_verbose, reload_logger(), and term_color().

00456 { 00457 struct logchannel *chan; 00458 char buf[BUFSIZ]; 00459 time_t t; 00460 struct tm tm; 00461 char date[256]; 00462 00463 va_list ap; 00464 00465 if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) { 00466 return; 00467 } 00468 00469 /* begin critical section */ 00470 ast_mutex_lock(&loglock); 00471 00472 time(&t); 00473 localtime_r(&t, &tm); 00474 strftime(date, sizeof(date), dateformat, &tm); 00475 00476 00477 if (level == __LOG_EVENT) { 00478 va_start(ap, fmt); 00479 00480 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid()); 00481 vfprintf(eventlog, fmt, ap); 00482 fflush(eventlog); 00483 00484 va_end(ap); 00485 ast_mutex_unlock(&loglock); 00486 return; 00487 } 00488 00489 if (logchannels) { 00490 chan = logchannels; 00491 while(chan) { 00492 if (chan->syslog && (chan->logmask & (1 << level))) { 00493 va_start(ap, fmt); 00494 ast_log_vsyslog(level, file, line, function, fmt, ap); 00495 va_end(ap); 00496 } else if ((chan->logmask & (1 << level)) && (chan->console)) { 00497 char linestr[128]; 00498 char tmp1[80], tmp2[80], tmp3[80], tmp4[80]; 00499 00500 if(level != __LOG_VERBOSE) { 00501 sprintf(linestr, "%d", line); 00502 snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ", 00503 date, 00504 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)), 00505 (long)pthread_self(), 00506 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)), 00507 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)), 00508 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4))); 00509 00510 ast_console_puts(buf); 00511 va_start(ap, fmt); 00512 vsnprintf(buf, sizeof(buf), fmt, ap); 00513 va_end(ap); 00514 ast_console_puts(buf); 00515 } 00516 } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) { 00517 snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date, 00518 levels[level], (long)pthread_self()); 00519 fprintf(chan->fileptr, buf); 00520 va_start(ap, fmt); 00521 vsnprintf(buf, sizeof(buf), fmt, ap); 00522 va_end(ap); 00523 fputs(buf, chan->fileptr); 00524 fflush(chan->fileptr); 00525 } 00526 chan = chan->next; 00527 } 00528 } else { 00529 /* 00530 * we don't have the logger chain configured yet, 00531 * so just log to stdout 00532 */ 00533 if (level != __LOG_VERBOSE) { 00534 va_start(ap, fmt); 00535 vsnprintf(buf, sizeof(buf), fmt, ap); 00536 va_end(ap); 00537 fputs(buf, stdout); 00538 } 00539 } 00540 00541 ast_mutex_unlock(&loglock); 00542 /* end critical section */ 00543 if (pending_logger_reload) { 00544 reload_logger(1); 00545 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n"); 00546 if (option_verbose) 00547 ast_verbose("Rotated Logs Per SIGXFSZ\n"); 00548 } 00549 }

AST_MUTEX_DEFINE_STATIC qloglock   ) 
 

AST_MUTEX_DEFINE_STATIC loglock   ) 
 

AST_MUTEX_DEFINE_STATIC msglist_lock   ) 
 

void ast_queue_log const char *  queuename,
const char *  callid,
const char *  agent,
const char *  event,
const char *  fmt,
  ...
 

Definition at line 233 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00234 { 00235 va_list ap; 00236 ast_mutex_lock(&qloglock); 00237 if (qlog) { 00238 va_start(ap, fmt); 00239 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event); 00240 vfprintf(qlog, fmt, ap); 00241 fprintf(qlog, "\n"); 00242 va_end(ap); 00243 fflush(qlog); 00244 } 00245 ast_mutex_unlock(&qloglock); 00246 }

int ast_register_verbose void(*  v)(const char *string, int opos, int replacelast, int complete)  ) 
 

Definition at line 629 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00630 { 00631 struct msglist *m; 00632 struct verb *tmp; 00633 /* XXX Should be more flexible here, taking > 1 verboser XXX */ 00634 if ((tmp = malloc(sizeof (struct verb)))) { 00635 tmp->verboser = v; 00636 ast_mutex_lock(&msglist_lock); 00637 tmp->next = verboser; 00638 verboser = tmp; 00639 m = list; 00640 while(m) { 00641 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */ 00642 v(m->msg, 0, 0, 1); 00643 m = m->next; 00644 } 00645 ast_mutex_unlock(&msglist_lock); 00646 return 0; 00647 } 00648 return -1; 00649 }

int ast_unregister_verbose void(*  v)(const char *string, int opos, int replacelast, int complete)  ) 
 

Definition at line 651 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00652 { 00653 int res = -1; 00654 struct verb *tmp, *tmpl=NULL; 00655 ast_mutex_lock(&msglist_lock); 00656 tmp = verboser; 00657 while(tmp) { 00658 if (tmp->verboser == v) { 00659 if (tmpl) 00660 tmpl->next = tmp->next; 00661 else 00662 verboser = tmp->next; 00663 free(tmp); 00664 break; 00665 } 00666 tmpl = tmp; 00667 tmp = tmp->next; 00668 } 00669 if (tmp) 00670 res = 0; 00671 ast_mutex_unlock(&msglist_lock); 00672 return res; 00673 }

void ast_verbose const char *  fmt,
  ...
 

Definition at line 551 of file logger.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, free, LOG_ERROR, LOG_VERBOSE, malloc, MAX_MSG_QUEUE, and strdup.

00552 { 00553 static char stuff[4096]; 00554 static int pos = 0, opos; 00555 static int replacelast = 0, complete; 00556 struct msglist *m; 00557 struct verb *v; 00558 va_list ap; 00559 va_start(ap, fmt); 00560 ast_mutex_lock(&msglist_lock); 00561 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap); 00562 opos = pos; 00563 pos = strlen(stuff); 00564 if (fmt[strlen(fmt)-1] == '\n') 00565 complete = 1; 00566 else 00567 complete=0; 00568 if (complete) { 00569 if (msgcnt < MAX_MSG_QUEUE) { 00570 /* Allocate new structure */ 00571 m = malloc(sizeof(struct msglist)); 00572 msgcnt++; 00573 } else { 00574 /* Recycle the oldest entry */ 00575 m = list; 00576 list = list->next; 00577 free(m->msg); 00578 } 00579 if (m) { 00580 m->msg = strdup(stuff); 00581 if (m->msg) { 00582 if (last) 00583 last->next = m; 00584 else 00585 list = m; 00586 m->next = NULL; 00587 last = m; 00588 } else { 00589 msgcnt--; 00590 ast_log(LOG_ERROR, "Out of memory\n"); 00591 free(m); 00592 } 00593 } 00594 } 00595 if (verboser) { 00596 v = verboser; 00597 while(v) { 00598 v->verboser(stuff, opos, replacelast, complete); 00599 v = v->next; 00600 } 00601 } /* else 00602 fprintf(stdout, stuff + opos); */ 00603 00604 ast_log(LOG_VERBOSE, stuff); 00605 00606 if (fmt[strlen(fmt)-1] != '\n') 00607 replacelast = 1; 00608 else 00609 replacelast = pos = 0; 00610 va_end(ap); 00611 00612 ast_mutex_unlock(&msglist_lock); 00613 }

int ast_verbose_dmesg void(*  v)(const char *string, int opos, int replacelast, int complete)  ) 
 

Definition at line 615 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00616 { 00617 struct msglist *m; 00618 m = list; 00619 ast_mutex_lock(&msglist_lock); 00620 while(m) { 00621 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */ 00622 v(m->msg, 0, 0, 1); 00623 m = m->next; 00624 } 00625 ast_mutex_unlock(&msglist_lock); 00626 return 0; 00627 }

int init_logger void   ) 
 

Definition at line 399 of file logger.c.

References ast_cli_register(), ast_config_AST_LOG_DIR, ast_log(), ast_verbose(), EVENTLOG, LOG_ERROR, LOG_EVENT, and option_verbose.

Referenced by main().

00400 { 00401 char tmp[256]; 00402 00403 /* auto rotate if sig SIGXFSZ comes a-knockin */ 00404 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ); 00405 00406 /* register the relaod logger cli command */ 00407 ast_cli_register(&reload_logger_cli); 00408 ast_cli_register(&rotate_logger_cli); 00409 00410 /* initialize queue logger */ 00411 queue_log_init(); 00412 00413 /* create the eventlog */ 00414 mkdir((char *)ast_config_AST_LOG_DIR, 0755); 00415 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG); 00416 eventlog = fopen((char *)tmp, "a"); 00417 if (eventlog) { 00418 init_logger_chain(); 00419 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n"); 00420 if (option_verbose) 00421 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp); 00422 return 0; 00423 } else 00424 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno)); 00425 00426 init_logger_chain(); 00427 00428 /* create log channels */ 00429 init_logger_chain(); 00430 return -1; 00431 }

int reload_logger int  rotate  ) 
 

Definition at line 267 of file logger.c.

References ast_config_AST_LOG_DIR, AST_CONFIG_MAX_PATH, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_verbose(), EVENTLOG, LOG_ERROR, LOG_EVENT, and option_verbose.

Referenced by ast_log().

00268 { 00269 char old[AST_CONFIG_MAX_PATH]; 00270 char new[AST_CONFIG_MAX_PATH]; 00271 struct logchannel *f; 00272 FILE *myf; 00273 00274 int x; 00275 ast_mutex_lock(&loglock); 00276 if (eventlog) 00277 fclose(eventlog); 00278 else 00279 rotate = 0; 00280 eventlog = NULL; 00281 00282 00283 00284 mkdir((char *)ast_config_AST_LOG_DIR, 0755); 00285 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG); 00286 00287 if(rotate) { 00288 for(x=0;;x++) { 00289 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x); 00290 myf = fopen((char *)new, "r"); 00291 if(myf) 00292 fclose(myf); 00293 else 00294 break; 00295 } 00296 00297 /* do it */ 00298 if (rename(old,new)) 00299 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new); 00300 } 00301 00302 eventlog = fopen(old, "a"); 00303 00304 f = logchannels; 00305 while(f) { 00306 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) { 00307 fclose(f->fileptr); 00308 f->fileptr = NULL; 00309 if(rotate) { 00310 strncpy(old, f->filename, sizeof(old)); 00311 00312 for(x=0;;x++) { 00313 snprintf(new, sizeof(new), "%s.%d", f->filename, x); 00314 myf = fopen((char *)new, "r"); 00315 if (myf) { 00316 fclose(myf); 00317 } else { 00318 break; 00319 } 00320 } 00321 00322 /* do it */ 00323 if (rename(old,new)) 00324 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new); 00325 } 00326 } 00327 f = f->next; 00328 } 00329 00330 ast_mutex_unlock(&loglock); 00331 00332 queue_log_init(); 00333 00334 if (eventlog) { 00335 init_logger_chain(); 00336 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n"); 00337 if (option_verbose) 00338 ast_verbose("Asterisk Event Logger restarted\n"); 00339 return 0; 00340 } else 00341 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno)); 00342 init_logger_chain(); 00343 pending_logger_reload = 0; 00344 return -1; 00345 }


Generated on Sat Jun 12 16:41:39 2004 for Asterisk by doxygen 1.3.7