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

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 close_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
 

Definition at line 33 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 474 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().

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

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 647 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00648 { 00649 struct msglist *m; 00650 struct verb *tmp; 00651 /* XXX Should be more flexible here, taking > 1 verboser XXX */ 00652 if ((tmp = malloc(sizeof (struct verb)))) { 00653 tmp->verboser = v; 00654 ast_mutex_lock(&msglist_lock); 00655 tmp->next = verboser; 00656 verboser = tmp; 00657 m = list; 00658 while(m) { 00659 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */ 00660 v(m->msg, 0, 0, 1); 00661 m = m->next; 00662 } 00663 ast_mutex_unlock(&msglist_lock); 00664 return 0; 00665 } 00666 return -1; 00667 }

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

Definition at line 669 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00670 { 00671 int res = -1; 00672 struct verb *tmp, *tmpl=NULL; 00673 ast_mutex_lock(&msglist_lock); 00674 tmp = verboser; 00675 while(tmp) { 00676 if (tmp->verboser == v) { 00677 if (tmpl) 00678 tmpl->next = tmp->next; 00679 else 00680 verboser = tmp->next; 00681 free(tmp); 00682 break; 00683 } 00684 tmpl = tmp; 00685 tmp = tmp->next; 00686 } 00687 if (tmp) 00688 res = 0; 00689 ast_mutex_unlock(&msglist_lock); 00690 return res; 00691 }

void ast_verbose const char *  fmt,
  ...
 

Definition at line 569 of file logger.c.

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

00570 { 00571 static char stuff[4096]; 00572 static int pos = 0, opos; 00573 static int replacelast = 0, complete; 00574 struct msglist *m; 00575 struct verb *v; 00576 va_list ap; 00577 va_start(ap, fmt); 00578 ast_mutex_lock(&msglist_lock); 00579 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap); 00580 opos = pos; 00581 pos = strlen(stuff); 00582 if (fmt[strlen(fmt)-1] == '\n') 00583 complete = 1; 00584 else 00585 complete=0; 00586 if (complete) { 00587 if (msgcnt < MAX_MSG_QUEUE) { 00588 /* Allocate new structure */ 00589 m = malloc(sizeof(struct msglist)); 00590 msgcnt++; 00591 } else { 00592 /* Recycle the oldest entry */ 00593 m = list; 00594 list = list->next; 00595 free(m->msg); 00596 } 00597 if (m) { 00598 m->msg = strdup(stuff); 00599 if (m->msg) { 00600 if (last) 00601 last->next = m; 00602 else 00603 list = m; 00604 m->next = NULL; 00605 last = m; 00606 } else { 00607 msgcnt--; 00608 ast_log(LOG_ERROR, "Out of memory\n"); 00609 free(m); 00610 } 00611 } 00612 } 00613 if (verboser) { 00614 v = verboser; 00615 while(v) { 00616 v->verboser(stuff, opos, replacelast, complete); 00617 v = v->next; 00618 } 00619 } /* else 00620 fprintf(stdout, stuff + opos); */ 00621 00622 ast_log(LOG_VERBOSE, stuff); 00623 00624 if (fmt[strlen(fmt)-1] != '\n') 00625 replacelast = 1; 00626 else 00627 replacelast = pos = 0; 00628 va_end(ap); 00629 00630 ast_mutex_unlock(&msglist_lock); 00631 }

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

Definition at line 633 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00634 { 00635 struct msglist *m; 00636 m = list; 00637 ast_mutex_lock(&msglist_lock); 00638 while(m) { 00639 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */ 00640 v(m->msg, 0, 0, 1); 00641 m = m->next; 00642 } 00643 ast_mutex_unlock(&msglist_lock); 00644 return 0; 00645 }

void close_logger void   ) 
 

Definition at line 432 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00433 { 00434 struct msglist *m, *tmp; 00435 00436 m = list; 00437 ast_mutex_lock(&msglist_lock); 00438 while(m) { 00439 if (m->msg) { 00440 free(m->msg); 00441 } 00442 tmp = m->next; 00443 free(m); 00444 m = tmp; 00445 } 00446 list = last = NULL; 00447 ast_mutex_unlock(&msglist_lock); 00448 return; 00449 }

int init_logger void   ) 
 

Definition at line 400 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().

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

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(), and main().

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) - 1); 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 Tue Aug 17 16:13:55 2004 for Asterisk by doxygen 1.3.8