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

Go to the source code of this file.

Data Structures

struct  logchannel
struct  msglist
struct  verb

Defines

#define SYSLOG_NAMES
#define SYSLOG_NLEVELS   6
#define MAX_MSG_QUEUE   200

Functions

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 48 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 44 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 408 of file logger.c.

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

00409 {
00410     struct logchannel *chan;
00411     char buf[BUFSIZ];
00412     time_t t;
00413     struct tm tm;
00414     char date[256];
00415 
00416     va_list ap;
00417    
00418     if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
00419    return;
00420     }
00421 
00422     /* begin critical section */
00423     ast_mutex_lock(&loglock);
00424 
00425     time(&t);
00426     localtime_r(&t, &tm);
00427     strftime(date, sizeof(date), "%b %e %T", &tm);
00428 
00429 
00430     if (level == __LOG_EVENT) {
00431        va_start(ap, fmt);
00432 
00433        fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
00434        vfprintf(eventlog, fmt, ap);
00435        fflush(eventlog);
00436 
00437        va_end(ap);
00438        ast_mutex_unlock(&loglock);
00439        return;
00440     }
00441 
00442     if (logchannels) {
00443    chan = logchannels;
00444    while(chan) {
00445        if (chan->syslog && (chan->logmask & (1 << level))) {
00446       va_start(ap, fmt);
00447       ast_log_vsyslog(level, file, line, function, fmt, ap);
00448       va_end(ap);
00449        } else if ((chan->logmask & (1 << level)) && (chan->console)) {
00450       char linestr[128];
00451       char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00452 
00453       if(level != __LOG_VERBOSE) {
00454           sprintf(linestr, "%d", line);
00455           snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
00456               date,
00457               term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
00458               (long)pthread_self(),
00459               term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
00460               term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
00461               term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
00462           
00463           ast_console_puts(buf);
00464           va_start(ap, fmt);
00465           vsnprintf(buf, sizeof(buf), fmt, ap);
00466           va_end(ap);
00467           ast_console_puts(buf);
00468       }
00469        } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
00470           snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
00471               levels[level], (long)pthread_self());
00472           fprintf(chan->fileptr, buf);
00473           va_start(ap, fmt);
00474           vsnprintf(buf, sizeof(buf), fmt, ap);
00475           va_end(ap);
00476           fprintf(chan->fileptr, buf);
00477           fflush(chan->fileptr);
00478        }
00479        chan = chan->next;
00480    }
00481     } else {
00482        /* 
00483         * we don't have the logger chain configured yet,
00484         * so just log to stdout 
00485         */
00486       if (level != __LOG_VERBOSE) {
00487           va_start(ap, fmt);
00488           vsnprintf(buf, sizeof(buf), fmt, ap);
00489           va_end(ap);
00490           fprintf(stdout, buf);
00491       }
00492     }
00493 
00494     ast_mutex_unlock(&loglock);
00495     /* end critical section */
00496 }

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

Definition at line 576 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00577 {
00578    struct msglist *m;
00579    struct verb *tmp;
00580    /* XXX Should be more flexible here, taking > 1 verboser XXX */
00581    if ((tmp = malloc(sizeof (struct verb)))) {
00582       tmp->verboser = v;
00583       ast_mutex_lock(&msglist_lock);
00584       tmp->next = verboser;
00585       verboser = tmp;
00586       m = list;
00587       while(m) {
00588          /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00589          v(m->msg, 0, 0, 1);
00590          m = m->next;
00591       }
00592       ast_mutex_unlock(&msglist_lock);
00593       return 0;
00594    }
00595    return -1;
00596 }

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

Definition at line 598 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00599 {
00600    int res = -1;
00601    struct verb *tmp, *tmpl=NULL;
00602    ast_mutex_lock(&msglist_lock);
00603    tmp = verboser;
00604    while(tmp) {
00605       if (tmp->verboser == v) {
00606          if (tmpl)
00607             tmpl->next = tmp->next;
00608          else
00609             verboser = tmp->next;
00610          free(tmp);
00611          break;
00612       }
00613       tmpl = tmp;
00614       tmp = tmp->next;
00615    }
00616    if (tmp)
00617       res = 0;
00618    ast_mutex_unlock(&msglist_lock);
00619    return res;
00620 }

void ast_verbose const char *  fmt,
... 
 

Definition at line 498 of file logger.c.

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

00499 {
00500    static char stuff[4096];
00501    static int pos = 0, opos;
00502    static int replacelast = 0, complete;
00503    struct msglist *m;
00504    struct verb *v;
00505    va_list ap;
00506    va_start(ap, fmt);
00507    ast_mutex_lock(&msglist_lock);
00508    vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
00509    opos = pos;
00510    pos = strlen(stuff);
00511    if (fmt[strlen(fmt)-1] == '\n') 
00512       complete = 1;
00513    else
00514       complete=0;
00515    if (complete) {
00516       if (msgcnt < MAX_MSG_QUEUE) {
00517          /* Allocate new structure */
00518          m = malloc(sizeof(struct msglist));
00519          msgcnt++;
00520       } else {
00521          /* Recycle the oldest entry */
00522          m = list;
00523          list = list->next;
00524          free(m->msg);
00525       }
00526       if (m) {
00527          m->msg = strdup(stuff);
00528          if (m->msg) {
00529             if (last)
00530                last->next = m;
00531             else
00532                list = m;
00533             m->next = NULL;
00534             last = m;
00535          } else {
00536             msgcnt--;
00537             ast_log(LOG_ERROR, "Out of memory\n");
00538             free(m);
00539          }
00540       }
00541    }
00542    if (verboser) {
00543       v = verboser;
00544       while(v) {
00545          v->verboser(stuff, opos, replacelast, complete);
00546          v = v->next;
00547       }
00548    } /* else
00549       fprintf(stdout, stuff + opos); */
00550 
00551    ast_log(LOG_VERBOSE, stuff);
00552 
00553    if (fmt[strlen(fmt)-1] != '\n') 
00554       replacelast = 1;
00555    else 
00556       replacelast = pos = 0;
00557    va_end(ap);
00558 
00559    ast_mutex_unlock(&msglist_lock);
00560 }

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

Definition at line 562 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00563 {
00564    struct msglist *m;
00565    m = list;
00566    ast_mutex_lock(&msglist_lock);
00567    while(m) {
00568       /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00569       v(m->msg, 0, 0, 1);
00570       m = m->next;
00571    }
00572    ast_mutex_unlock(&msglist_lock);
00573    return 0;
00574 }

int init_logger void   ) 
 

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

00356 {
00357    char tmp[256];
00358 
00359    /* auto rotate if sig SIGXFSZ comes a-knockin */
00360    (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
00361 
00362    /* register the relaod logger cli command */
00363    ast_cli_register(&reload_logger_cli);
00364    ast_cli_register(&rotate_logger_cli);
00365 
00366    /* create the eventlog */
00367    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00368    snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00369    eventlog = fopen((char *)tmp, "a");
00370    if (eventlog) {
00371       init_logger_chain();
00372       ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
00373       if (option_verbose)
00374          ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
00375       return 0;
00376    } else 
00377       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00378 
00379    init_logger_chain();
00380 
00381    /* create log channels */
00382    init_logger_chain();
00383    return -1;
00384 }

int reload_logger int  rotate  ) 
 

Definition at line 223 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.

00224 {
00225    char old[AST_CONFIG_MAX_PATH];
00226    char new[AST_CONFIG_MAX_PATH];
00227    struct logchannel *f;
00228    FILE *myf;
00229 
00230    int x;
00231 
00232    ast_mutex_lock(&loglock);
00233    if (eventlog) 
00234       fclose(eventlog);
00235    else 
00236       rotate = 0;
00237    eventlog = NULL;
00238 
00239 
00240 
00241    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00242    snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00243 
00244    if(rotate) {
00245       for(x=0;;x++) {
00246          snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
00247          myf = fopen((char *)new, "r");
00248          if(myf) 
00249             fclose(myf);
00250          else
00251             break;
00252       }
00253    
00254       /* do it */
00255       if (rename(old,new))
00256          fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00257    }
00258 
00259    eventlog = fopen(old, "a");
00260 
00261    f = logchannels;
00262    while(f) {
00263       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
00264          fclose(f->fileptr);
00265          f->fileptr = NULL;
00266          if(rotate) {
00267             strncpy(old, f->filename, sizeof(old));
00268    
00269             for(x=0;;x++) {
00270                snprintf(new, sizeof(new), "%s.%d", f->filename, x);
00271                myf = fopen((char *)new, "r");
00272                if (myf) {
00273                   fclose(myf);
00274                } else {
00275                   break;
00276                }
00277             }
00278        
00279             /* do it */
00280             if (rename(old,new))
00281                fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00282          }
00283       }
00284       f = f->next;
00285    }
00286 
00287    ast_mutex_unlock(&loglock);
00288 
00289    if (eventlog) {
00290       init_logger_chain();
00291       ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
00292       if (option_verbose)
00293          ast_verbose("Asterisk Event Logger restarted\n");
00294       return 0;
00295    } else 
00296       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00297    init_logger_chain();
00298    return -1;
00299 }


Generated on Fri Feb 27 12:19:49 2004 for Asterisk by doxygen 1.3.5