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

logger.c File Reference

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <asterisk/lock.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>
#include <asterisk/term.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/stat.h>
#include "asterisk.h"
#include "astconf.h"

Go to the source code of this file.

Data Structures

struct  logfile
struct  msglist
struct  verb

Defines

#define MAX_MSG_QUEUE   200

Functions

int init_logger (void)
int reload_logger (void)
void ast_log (int level, const char *file, int line, const char *function, const char *fmt,...)
 Used for sending a log message.

void ast_verbose (const char *fmt,...)
 Send a verbose message (based on verbose level).

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

Referenced by ast_verbose().


Function Documentation

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

Used for sending a log message.

Cannot use ast_log() from locked section of ast_log()! ast_log(LOG_WARNING, "Unable to retrieve local time?\n"); *

Definition at line 222 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, COLOR_BRWHITE, option_debug, option_verbose, and term_color().

00223 {
00224    char date[256];
00225    char tmp[80];
00226    char tmp2[80];
00227    char tmp3[80];
00228    char tmp4[80];
00229    char linestr[80];
00230    time_t t;
00231    struct tm tm;
00232    struct logfile *f;
00233 
00234    va_list ap;
00235    if (!option_verbose && !option_debug && (!level)) {
00236       return;
00237    }
00238    ast_mutex_lock(&loglock);
00239    if (level == 1 /* Event */) {
00240       time(&t);
00241       localtime_r(&t,&tm);
00242       if (&tm) {
00243          /* Log events into the event log file, with a different format */
00244          strftime(date, sizeof(date), "%b %e %T", &tm);
00245          fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
00246          va_start(ap, fmt);
00247          vfprintf(eventlog, fmt, ap);
00248          va_end(ap);
00249          fflush(eventlog);
00250       } else
00251          /** Cannot use ast_log() from locked section of ast_log()!
00252              ast_log(LOG_WARNING, "Unable to retrieve local time?\n"); **/
00253          fprintf(stderr, "ast_log: Unable to retrieve local time for %ld?\n", (long)t);
00254    } else {
00255       if (logfiles) {
00256          f = logfiles;
00257          while(f) {
00258             if (f->logflags & (1 << level) && f->f) {
00259                if ((f->f != stdout) && (f->f != stderr)) {
00260                   time(&t);
00261                   localtime_r(&t,&tm);
00262                   strftime(date, sizeof(date), "%b %e %T", &tm);
00263                   fprintf(f->f, "%s %s[%ld]: File %s, Line %d (%s): ", date, levels[level], (long)pthread_self(), file, line, function);
00264                } else {
00265                   sprintf(linestr, "%d", line);
00266                   fprintf(f->f, "%s[%ld]: File %s, Line %s (%s): ",
00267                                                 term_color(tmp, levels[level], colors[level], 0, sizeof(tmp)),
00268                                                 (long)pthread_self(),
00269                                                 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
00270                                                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
00271                                                 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
00272                }
00273                va_start(ap, fmt);
00274                vfprintf(f->f, fmt, ap);
00275                va_end(ap);
00276                fflush(f->f);
00277             }
00278             f = f->next;
00279          }
00280       } else {
00281          fprintf(stdout, "%s[%ld]: File %s, Line %d (%s): ", levels[level], (long)pthread_self(), file, line, function);
00282          va_start(ap, fmt);
00283          vfprintf(stdout, fmt, ap);
00284          va_end(ap);
00285          fflush(stdout);
00286       }
00287    }
00288    ast_mutex_unlock(&loglock);
00289 }

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

Definition at line 366 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and malloc.

Referenced by main().

00367 {
00368    struct msglist *m;
00369    struct verb *tmp;
00370    /* XXX Should be more flexible here, taking > 1 verboser XXX */
00371    if ((tmp = malloc(sizeof (struct verb)))) {
00372       tmp->verboser = v;
00373       ast_mutex_lock(&msglist_lock);
00374       tmp->next = verboser;
00375       verboser = tmp;
00376       m = list;
00377       while(m) {
00378          /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00379          v(m->msg, 0, 0, 1);
00380          m = m->next;
00381       }
00382       ast_mutex_unlock(&msglist_lock);
00383       return 0;
00384    }
00385    return -1;
00386 }

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

Definition at line 388 of file logger.c.

References ast_mutex_lock, ast_mutex_unlock, and free.

00389 {
00390    int res = -1;
00391    struct verb *tmp, *tmpl=NULL;
00392    ast_mutex_lock(&msglist_lock);
00393    tmp = verboser;
00394    while(tmp) {
00395       if (tmp->verboser == v) {
00396          if (tmpl)
00397             tmpl->next = tmp->next;
00398          else
00399             verboser = tmp->next;
00400          free(tmp);
00401          break;
00402       }
00403       tmpl = tmp;
00404       tmp = tmp->next;
00405    }
00406    if (tmp)
00407       res = 0;
00408    ast_mutex_unlock(&msglist_lock);
00409    return res;
00410 }

void ast_verbose const char *  fmt,
... 
 

Send a verbose message (based on verbose level).

This works like ast_log, but prints verbose messages to the console depending on verbosity level set. ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing"); This will print the message to the console if the verbose level is set to a level >= 3 Note the abscence of a comma after the VERBOSE_PREFIX_3. This is important. VERBOSE_PREFIX_1 through VERBOSE_PREFIX_3 are defined.

Definition at line 291 of file logger.c.

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

00292 {
00293    static char stuff[4096];
00294    static int pos = 0, opos;
00295    static int replacelast = 0, complete;
00296    struct msglist *m;
00297    struct verb *v;
00298    va_list ap;
00299    va_start(ap, fmt);
00300    ast_mutex_lock(&msglist_lock);
00301    vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
00302    opos = pos;
00303    pos = strlen(stuff);
00304    if (fmt[strlen(fmt)-1] == '\n') 
00305       complete = 1;
00306    else
00307       complete=0;
00308    if (complete) {
00309       if (msgcnt < MAX_MSG_QUEUE) {
00310          /* Allocate new structure */
00311          m = malloc(sizeof(struct msglist));
00312          msgcnt++;
00313       } else {
00314          /* Recycle the oldest entry */
00315          m = list;
00316          list = list->next;
00317          free(m->msg);
00318       }
00319       if (m) {
00320          m->msg = strdup(stuff);
00321          if (m->msg) {
00322             if (last)
00323                last->next = m;
00324             else
00325                list = m;
00326             m->next = NULL;
00327             last = m;
00328          } else {
00329             msgcnt--;
00330             ast_log(LOG_DEBUG, "Out of memory\n");
00331             free(m);
00332          }
00333       }
00334    }
00335    if (verboser) {
00336       v = verboser;
00337       while(v) {
00338          v->verboser(stuff, opos, replacelast, complete);
00339          v = v->next;
00340       }
00341    } /* else
00342       fprintf(stdout, stuff + opos); */
00343 
00344    if (fmt[strlen(fmt)-1] != '\n') 
00345       replacelast = 1;
00346    else 
00347       replacelast = pos = 0;
00348    va_end(ap);
00349    ast_mutex_unlock(&msglist_lock);
00350 }

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

Definition at line 352 of file logger.c.

References ast_mutex_lock, and ast_mutex_unlock.

00353 {
00354    struct msglist *m;
00355    m = list;
00356    ast_mutex_lock(&msglist_lock);
00357    while(m) {
00358       /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
00359       v(m->msg, 0, 0, 1);
00360       m = m->next;
00361    }
00362    ast_mutex_unlock(&msglist_lock);
00363    return 0;
00364 }

int init_logger void   ) 
 

Definition at line 181 of file logger.c.

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

Referenced by main().

00182 {
00183    char tmp[AST_CONFIG_MAX_PATH];
00184    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00185    snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00186    eventlog = fopen((char *)tmp, "a");
00187    if (eventlog) {
00188       init_logger_chain();
00189       ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
00190       if (option_verbose)
00191          ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
00192       return 0;
00193    } else 
00194       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00195    init_logger_chain();
00196    return -1;
00197 }

int reload_logger void   ) 
 

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

00200 {
00201    char tmp[AST_CONFIG_MAX_PATH];
00202    ast_mutex_lock(&loglock);
00203    if (eventlog)
00204       fclose(eventlog);
00205    mkdir((char *)ast_config_AST_LOG_DIR, 0755);
00206    snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
00207    eventlog = fopen((char *)tmp, "a");
00208    ast_mutex_unlock(&loglock);
00209 
00210    if (eventlog) {
00211       init_logger_chain();
00212       ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
00213       if (option_verbose)
00214          ast_verbose("Asterisk Event Logger restarted\n");
00215       return 0;
00216    } else 
00217       ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
00218    init_logger_chain();
00219    return -1;
00220 }


Generated on Fri Oct 31 07:05:14 2003 for Asterisk by doxygen 1.3.4