Main Page | Alphabetical List | Data Structures | Directories | 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
#define GETTID()   getpid()

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 GETTID  )     getpid()
 

Definition at line 55 of file logger.c.

Referenced by ast_log().

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

References __LOG_DEBUG, __LOG_EVENT, __LOG_VERBOSE, ast_console_puts(), ast_mutex_lock, ast_mutex_unlock, ast_verbose(), COLOR_BRWHITE, logchannel::console, logchannel::fileptr, GETTID, LOG_EVENT, logchannel::logmask, logchannel::next, option_debug, option_verbose, reload_logger(), logchannel::syslog, and term_color().

Referenced by __ast_context_destroy(), __ast_request_and_dial(), __ast_smoother_feed(), ast_add_extension2(), ast_app_getvoice(), ast_append_ha(), ast_apply_ha(), ast_async_goto(), ast_autoservice_start(), ast_best_codec(), ast_cdr_answer(), ast_cdr_busy(), ast_cdr_disposition(), ast_cdr_end(), ast_cdr_failed(), ast_cdr_free(), ast_cdr_init(), ast_cdr_post(), ast_cdr_register(), ast_cdr_setapp(), ast_cdr_setdestchan(), ast_cdr_start(), ast_channel_alloc(), ast_channel_bridge(), ast_channel_free(), ast_channel_make_compatible(), ast_channel_masquerade(), ast_channel_register_ex(), ast_channel_setoption(), ast_channel_unregister(), ast_channel_walk_locked(), ast_cli(), ast_cli_command(), ast_cli_register(), ast_cli_unregister(), ast_config_register(), ast_context_add_ignorepat2(), ast_context_add_include2(), ast_context_add_switch2(), ast_context_create(), ast_context_verify_includes(), ast_db_del(), ast_db_get(), ast_db_gettree(), ast_db_put(), ast_device_state(), ast_do_masquerade(), ast_dsp_call_progress(), ast_dsp_digitdetect(), ast_dsp_process(), ast_dsp_silence(), ast_dtmf_stream(), ast_find_ourip(), ast_format_register(), ast_format_unregister(), ast_fr_fdread(), ast_fr_fdwrite(), ast_frisolate(), ast_get_enum(), ast_get_group(), ast_get_indication_tone(), ast_get_indication_zone(), ast_get_ip(), ast_get_txt(), ast_hangup(), ast_indicate(), ast_io_add(), ast_io_dump(), ast_io_remove(), ast_io_wait(), ast_linear_stream(), ast_load_resource(), ast_loader_register(), ast_loader_unregister(), ast_lock_path(), ast_lookup_iface(), ast_manager_register2(), ast_merge_contexts_and_delete(), ast_openstream(), ast_openvstream(), ast_ouraddrfor(), ast_parse_allow_disallow(), ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_pbx_run(), ast_pbx_start(), ast_play_and_prepend(), ast_play_and_record(), ast_playtones_start(), ast_prod(), ast_pthread_create(), ast_queue_frame(), ast_read(), ast_read_image(), ast_readfile(), ast_register_application(), ast_register_indication(), ast_register_indication_country(), ast_register_switch(), ast_register_translator(), ast_request(), ast_rtcp_read(), ast_rtp_bridge(), ast_rtp_new_with_bindaddr(), ast_rtp_offered_from_local(), ast_rtp_proto_register(), ast_rtp_read(), ast_rtp_reload(), ast_rtp_senddigit(), ast_rtp_settos(), ast_rtp_write(), ast_safe_system(), ast_sched_add(), ast_sched_del(), ast_sched_dump(), ast_sched_runq(), ast_sched_wait(), ast_sched_when(), ast_search_dns(), ast_set_read_format(), ast_set_write_format(), ast_settimeout(), ast_smoother_read(), ast_softhangup_nolock(), ast_stopstream(), ast_strcasestr(), ast_streamfile(), ast_translate(), ast_translator_build_path(), ast_unload_resource(), ast_unlock_path(), ast_unregister_application(), ast_unregister_indication(), ast_unregister_indication_country(), ast_unregister_switch(), ast_update_use_count(), ast_var_assign(), ast_verbose(), ast_waitfor_n_fd(), ast_waitfor_nandfds(), ast_waitfordigit_full(), ast_waitstream(), ast_waitstream_fr(), ast_waitstream_full(), ast_write(), ast_writefile(), ast_writestream(), callerid_feed(), callerid_get_dtmf(), callerid_new(), init_logger(), init_manager(), load_modules(), load_pbx(), main(), pbx_builtin_setvar(), pbx_exec(), pbx_findapp(), pbx_substitute_variables_helper(), read_ast_cust_config(), reload_logger(), tdd_feed(), and tdd_new().

00537 {
00538    struct logchannel *chan;
00539    char buf[BUFSIZ];
00540    time_t t;
00541    struct tm tm;
00542    char date[256];
00543 
00544    va_list ap;
00545    
00546    if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
00547       return;
00548    }
00549 
00550    /* begin critical section */
00551    ast_mutex_lock(&loglock);
00552 
00553    time(&t);
00554    localtime_r(&t, &tm);
00555    strftime(date, sizeof(date), dateformat, &tm);
00556 
00557    if (level == __LOG_EVENT) {
00558       va_start(ap, fmt);
00559 
00560       fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
00561       vfprintf(eventlog, fmt, ap);
00562       fflush(eventlog);
00563 
00564       va_end(ap);
00565       ast_mutex_unlock(&loglock);
00566       return;
00567    }
00568 
00569    if (logchannels) {
00570       chan = logchannels;
00571       while(chan) {
00572          if (chan->syslog && (chan->logmask & (1 << level))) {
00573             va_start(ap, fmt);
00574             ast_log_vsyslog(level, file, line, function, fmt, ap);
00575             va_end(ap);
00576          } else if ((chan->logmask & (1 << level)) && (chan->console)) {
00577             char linestr[128];
00578             char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00579 
00580             if (level != __LOG_VERBOSE) {
00581                sprintf(linestr, "%d", line);
00582                snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
00583                   date,
00584                   term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
00585                   (long)GETTID(),
00586                   term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
00587                   term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
00588                   term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
00589           
00590                ast_console_puts(buf);
00591                va_start(ap, fmt);
00592                vsnprintf(buf, sizeof(buf), fmt, ap);
00593                va_end(ap);
00594                ast_console_puts(buf);
00595             }
00596          } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
00597             snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
00598                levels[level], (long)GETTID());
00599             fprintf(chan->fileptr, buf);
00600             va_start(ap, fmt);
00601             vsnprintf(buf, sizeof(buf), fmt, ap);
00602             strip_coloring(buf); 
00603             va_end(ap);
00604             fputs(buf, chan->fileptr);
00605             fflush(chan->fileptr);
00606          }
00607          chan = chan->next;
00608       }
00609    } else {
00610       /* 
00611        * we don't have the logger chain configured yet,
00612        * so just log to stdout 
00613       */
00614       if (level != __LOG_VERBOSE) {
00615          va_start(ap, fmt);
00616          vsnprintf(buf, sizeof(buf), fmt, ap);
00617          va_end(ap);
00618          fputs(buf, stdout);
00619       }
00620    }
00621 
00622    ast_mutex_unlock(&loglock);
00623    /* end critical section */
00624    if (pending_logger_reload) {
00625       reload_logger(1);
00626       ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
00627       if (option_verbose)
00628          ast_verbose("Rotated Logs Per SIGXFSZ\n");
00629    }
00630 }

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

References ast_mutex_lock, and ast_mutex_unlock.

00263 {
00264    va_list ap;
00265    ast_mutex_lock(&qloglock);
00266    if (qlog) {
00267       va_start(ap, fmt);
00268       fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
00269       vfprintf(qlog, fmt, ap);
00270       fprintf(qlog, "\n");
00271       va_end(ap);
00272       fflush(qlog);
00273    }
00274    ast_mutex_unlock(&qloglock);
00275 }

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

Definition at line 710 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00711 {
00712    struct msglist *m;
00713    struct verb *tmp;
00714    /* XXX Should be more flexible here, taking > 1 verboser XXX */
00715    if ((tmp = malloc(sizeof (struct verb)))) {
00716       tmp->verboser = v;
00717       ast_mutex_lock(&msglist_lock);
00718       tmp->next = verboser;
00719       verboser = tmp;
00720       m = list;
00721       while(m) {
00722          /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00723          v(m->msg, 0, 0, 1);
00724          m = m->next;
00725       }
00726       ast_mutex_unlock(&msglist_lock);
00727       return 0;
00728    }
00729    return -1;
00730 }

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

Definition at line 732 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00733 {
00734    int res = -1;
00735    struct verb *tmp, *tmpl=NULL;
00736    ast_mutex_lock(&msglist_lock);
00737    tmp = verboser;
00738    while(tmp) {
00739       if (tmp->verboser == v) {
00740          if (tmpl)
00741             tmpl->next = tmp->next;
00742          else
00743             verboser = tmp->next;
00744          free(tmp);
00745          break;
00746       }
00747       tmpl = tmp;
00748       tmp = tmp->next;
00749    }
00750    if (tmp)
00751       res = 0;
00752    ast_mutex_unlock(&msglist_lock);
00753    return res;
00754 }

void ast_verbose const char *  fmt,
  ...
 

Definition at line 632 of file logger.c.

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

Referenced by ast_cdr_unregister(), ast_channel_bridge(), ast_channel_register_ex(), ast_channel_unregister(), ast_context_add_include2(), ast_context_add_switch2(), ast_context_create(), ast_format_register(), ast_format_unregister(), ast_frame_dump(), ast_image_register(), ast_image_unregister(), ast_load_resource(), ast_log(), ast_manager_unregister(), ast_module_reload(), ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_pbx_run(), ast_play_and_prepend(), ast_play_and_record(), ast_register_application(), ast_register_indication_country(), ast_register_translator(), ast_rtp_reload(), ast_save(), ast_set_indication_country(), ast_streamfile(), ast_unregister_application(), ast_unregister_indication_country(), ast_unregister_translator(), init_logger(), init_manager(), load_modules(), load_pbx(), main(), pbx_builtin_setvar_helper(), and reload_logger().

00633 {
00634    static char stuff[4096];
00635    static int pos = 0, opos;
00636    static int replacelast = 0, complete;
00637    struct msglist *m;
00638    struct verb *v;
00639    va_list ap;
00640    va_start(ap, fmt);
00641    ast_mutex_lock(&msglist_lock);
00642    vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
00643    opos = pos;
00644    pos = strlen(stuff);
00645    if (fmt[strlen(fmt)-1] == '\n') 
00646       complete = 1;
00647    else
00648       complete=0;
00649    if (complete) {
00650       if (msgcnt < MAX_MSG_QUEUE) {
00651          /* Allocate new structure */
00652          m = malloc(sizeof(struct msglist));
00653          msgcnt++;
00654       } else {
00655          /* Recycle the oldest entry */
00656          m = list;
00657          list = list->next;
00658          free(m->msg);
00659       }
00660       if (m) {
00661          m->msg = strdup(stuff);
00662          if (m->msg) {
00663             if (last)
00664                last->next = m;
00665             else
00666                list = m;
00667             m->next = NULL;
00668             last = m;
00669          } else {
00670             msgcnt--;
00671             ast_log(LOG_ERROR, "Out of memory\n");
00672             free(m);
00673          }
00674       }
00675    }
00676    if (verboser) {
00677       v = verboser;
00678       while(v) {
00679          v->verboser(stuff, opos, replacelast, complete);
00680          v = v->next;
00681       }
00682    } /* else
00683       fprintf(stdout, stuff + opos); */
00684 
00685    ast_log(LOG_VERBOSE, "%s", stuff);
00686 
00687    if (fmt[strlen(fmt)-1] != '\n') 
00688       replacelast = 1;
00689    else 
00690       replacelast = pos = 0;
00691    va_end(ap);
00692 
00693    ast_mutex_unlock(&msglist_lock);
00694 }

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

Definition at line 696 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00697 {
00698    struct msglist *m;
00699    ast_mutex_lock(&msglist_lock);
00700    m = list;
00701    while(m) {
00702       /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00703       v(m->msg, 0, 0, 1);
00704       m = m->next;
00705    }
00706    ast_mutex_unlock(&msglist_lock);
00707    return 0;
00708 }

void close_logger void   ) 
 

Definition at line 461 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00462 {
00463    struct msglist *m, *tmp;
00464 
00465    ast_mutex_lock(&msglist_lock);
00466    m = list;
00467    while(m) {
00468       if (m->msg) {
00469          free(m->msg);
00470       }
00471       tmp = m->next;
00472       free(m);
00473       m = tmp;
00474    }
00475    list = last = NULL;
00476    msgcnt = 0;
00477    ast_mutex_unlock(&msglist_lock);
00478    return;
00479 }

int init_logger void   ) 
 

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

00430 {
00431    char tmp[256];
00432 
00433    /* auto rotate if sig SIGXFSZ comes a-knockin */
00434    (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
00435 
00436    /* register the relaod logger cli command */
00437    ast_cli_register(&reload_logger_cli);
00438    ast_cli_register(&rotate_logger_cli);
00439 
00440    /* initialize queue logger */
00441    queue_log_init();
00442 
00443    /* create the eventlog */
00444    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00445    snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00446    eventlog = fopen((char *)tmp, "a");
00447    if (eventlog) {
00448       init_logger_chain();
00449       ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
00450       if (option_verbose)
00451          ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
00452       return 0;
00453    } else 
00454       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00455 
00456    /* create log channels */
00457    init_logger_chain();
00458    return -1;
00459 }

int reload_logger int  rotate  ) 
 

Definition at line 296 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, logchannel::filename, logchannel::fileptr, LOG_ERROR, LOG_EVENT, logchannel::next, and option_verbose.

Referenced by ast_log(), and main().

00297 {
00298    char old[AST_CONFIG_MAX_PATH] = "";
00299    char new[AST_CONFIG_MAX_PATH];
00300    struct logchannel *f;
00301    FILE *myf;
00302 
00303    int x;
00304    ast_mutex_lock(&loglock);
00305    if (eventlog) 
00306       fclose(eventlog);
00307    else 
00308       rotate = 0;
00309    eventlog = NULL;
00310 
00311 
00312 
00313    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00314    snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00315 
00316    if(rotate) {
00317       for(x=0;;x++) {
00318          snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
00319          myf = fopen((char *)new, "r");
00320          if(myf) 
00321             fclose(myf);
00322          else
00323             break;
00324       }
00325    
00326       /* do it */
00327       if (rename(old,new))
00328          fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00329    }
00330 
00331    eventlog = fopen(old, "a");
00332 
00333    f = logchannels;
00334    while(f) {
00335       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
00336          fclose(f->fileptr);
00337          f->fileptr = NULL;
00338          if(rotate) {
00339             strncpy(old, f->filename, sizeof(old) - 1);
00340    
00341             for(x=0;;x++) {
00342                snprintf(new, sizeof(new), "%s.%d", f->filename, x);
00343                myf = fopen((char *)new, "r");
00344                if (myf) {
00345                   fclose(myf);
00346                } else {
00347                   break;
00348                }
00349             }
00350        
00351             /* do it */
00352             if (rename(old,new))
00353                fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00354          }
00355       }
00356       f = f->next;
00357    }
00358 
00359    ast_mutex_unlock(&loglock);
00360 
00361    queue_log_init();
00362 
00363    if (eventlog) {
00364       init_logger_chain();
00365       ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
00366       if (option_verbose)
00367          ast_verbose("Asterisk Event Logger restarted\n");
00368       return 0;
00369    } else 
00370       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00371    init_logger_chain();
00372    pending_logger_reload = 0;
00373    return -1;
00374 }


Generated on Wed Aug 10 11:36:34 2005 for Asterisk by  doxygen 1.4.4