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.

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 504 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_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_masquerade_locked(), 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_run_app(), 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_search_dns(), ast_set_read_format(), ast_set_write_format(), ast_settimeout(), ast_smoother_feed(), 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().

00505 {
00506    struct logchannel *chan;
00507    char buf[BUFSIZ];
00508    time_t t;
00509    struct tm tm;
00510    char date[256];
00511 
00512    va_list ap;
00513    
00514    if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
00515       return;
00516    }
00517 
00518    /* begin critical section */
00519    ast_mutex_lock(&loglock);
00520 
00521    time(&t);
00522    localtime_r(&t, &tm);
00523    strftime(date, sizeof(date), dateformat, &tm);
00524 
00525    if (level == __LOG_EVENT) {
00526       va_start(ap, fmt);
00527 
00528       fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
00529       vfprintf(eventlog, fmt, ap);
00530       fflush(eventlog);
00531 
00532       va_end(ap);
00533       ast_mutex_unlock(&loglock);
00534       return;
00535    }
00536 
00537    if (logchannels) {
00538       chan = logchannels;
00539       while(chan) {
00540          if (chan->syslog && (chan->logmask & (1 << level))) {
00541             va_start(ap, fmt);
00542             ast_log_vsyslog(level, file, line, function, fmt, ap);
00543             va_end(ap);
00544          } else if ((chan->logmask & (1 << level)) && (chan->console)) {
00545             char linestr[128];
00546             char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00547 
00548             if (level != __LOG_VERBOSE) {
00549                sprintf(linestr, "%d", line);
00550                snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
00551                   date,
00552                   term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
00553                   (long)GETTID(),
00554                   term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
00555                   term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
00556                   term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
00557           
00558                ast_console_puts(buf);
00559                va_start(ap, fmt);
00560                vsnprintf(buf, sizeof(buf), fmt, ap);
00561                va_end(ap);
00562                ast_console_puts(buf);
00563             }
00564          } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
00565             snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
00566                levels[level], (long)GETTID());
00567             fprintf(chan->fileptr, buf);
00568             va_start(ap, fmt);
00569             vsnprintf(buf, sizeof(buf), fmt, ap);
00570             va_end(ap);
00571             fputs(buf, chan->fileptr);
00572             fflush(chan->fileptr);
00573          }
00574          chan = chan->next;
00575       }
00576    } else {
00577       /* 
00578        * we don't have the logger chain configured yet,
00579        * so just log to stdout 
00580       */
00581       if (level != __LOG_VERBOSE) {
00582          va_start(ap, fmt);
00583          vsnprintf(buf, sizeof(buf), fmt, ap);
00584          va_end(ap);
00585          fputs(buf, stdout);
00586       }
00587    }
00588 
00589    ast_mutex_unlock(&loglock);
00590    /* end critical section */
00591    if (pending_logger_reload) {
00592       reload_logger(1);
00593       ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
00594       if (option_verbose)
00595          ast_verbose("Rotated Logs Per SIGXFSZ\n");
00596    }
00597 }

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

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00678 {
00679    struct msglist *m;
00680    struct verb *tmp;
00681    /* XXX Should be more flexible here, taking > 1 verboser XXX */
00682    if ((tmp = malloc(sizeof (struct verb)))) {
00683       tmp->verboser = v;
00684       ast_mutex_lock(&msglist_lock);
00685       tmp->next = verboser;
00686       verboser = tmp;
00687       m = list;
00688       while(m) {
00689          /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00690          v(m->msg, 0, 0, 1);
00691          m = m->next;
00692       }
00693       ast_mutex_unlock(&msglist_lock);
00694       return 0;
00695    }
00696    return -1;
00697 }

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

Definition at line 699 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00700 {
00701    int res = -1;
00702    struct verb *tmp, *tmpl=NULL;
00703    ast_mutex_lock(&msglist_lock);
00704    tmp = verboser;
00705    while(tmp) {
00706       if (tmp->verboser == v) {
00707          if (tmpl)
00708             tmpl->next = tmp->next;
00709          else
00710             verboser = tmp->next;
00711          free(tmp);
00712          break;
00713       }
00714       tmpl = tmp;
00715       tmp = tmp->next;
00716    }
00717    if (tmp)
00718       res = 0;
00719    ast_mutex_unlock(&msglist_lock);
00720    return res;
00721 }

void ast_verbose const char *  fmt,
  ...
 

Definition at line 599 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_pbx_run_app(), 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().

00600 {
00601    static char stuff[4096];
00602    static int pos = 0, opos;
00603    static int replacelast = 0, complete;
00604    struct msglist *m;
00605    struct verb *v;
00606    va_list ap;
00607    va_start(ap, fmt);
00608    ast_mutex_lock(&msglist_lock);
00609    vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
00610    opos = pos;
00611    pos = strlen(stuff);
00612    if (fmt[strlen(fmt)-1] == '\n') 
00613       complete = 1;
00614    else
00615       complete=0;
00616    if (complete) {
00617       if (msgcnt < MAX_MSG_QUEUE) {
00618          /* Allocate new structure */
00619          m = malloc(sizeof(struct msglist));
00620          msgcnt++;
00621       } else {
00622          /* Recycle the oldest entry */
00623          m = list;
00624          list = list->next;
00625          free(m->msg);
00626       }
00627       if (m) {
00628          m->msg = strdup(stuff);
00629          if (m->msg) {
00630             if (last)
00631                last->next = m;
00632             else
00633                list = m;
00634             m->next = NULL;
00635             last = m;
00636          } else {
00637             msgcnt--;
00638             ast_log(LOG_ERROR, "Out of memory\n");
00639             free(m);
00640          }
00641       }
00642    }
00643    if (verboser) {
00644       v = verboser;
00645       while(v) {
00646          v->verboser(stuff, opos, replacelast, complete);
00647          v = v->next;
00648       }
00649    } /* else
00650       fprintf(stdout, stuff + opos); */
00651 
00652    ast_log(LOG_VERBOSE, "%s", stuff);
00653 
00654    if (fmt[strlen(fmt)-1] != '\n') 
00655       replacelast = 1;
00656    else 
00657       replacelast = pos = 0;
00658    va_end(ap);
00659 
00660    ast_mutex_unlock(&msglist_lock);
00661 }

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

Definition at line 663 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00664 {
00665    struct msglist *m;
00666    ast_mutex_lock(&msglist_lock);
00667    m = list;
00668    while(m) {
00669       /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00670       v(m->msg, 0, 0, 1);
00671       m = m->next;
00672    }
00673    ast_mutex_unlock(&msglist_lock);
00674    return 0;
00675 }

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 Mar 16 20:08:37 2005 for Asterisk by  doxygen 1.4.0