00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdarg.h>
00015 #include <stdio.h>
00016 #include <unistd.h>
00017 #include <time.h>
00018 #include <asterisk/lock.h>
00019 #include <asterisk/logger.h>
00020 #include <asterisk/options.h>
00021 #include <asterisk/channel.h>
00022 #include <asterisk/config.h>
00023 #include <asterisk/term.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <errno.h>
00027 #include <pthread.h>
00028 #include <sys/stat.h>
00029 #include "asterisk.h"
00030 #include "astconf.h"
00031
00032 #define MAX_MSG_QUEUE 200
00033
00034 static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
00035 static ast_mutex_t loglock = AST_MUTEX_INITIALIZER;
00036
00037 static struct msglist {
00038 char *msg;
00039 struct msglist *next;
00040 } *list = NULL, *last = NULL;
00041
00042 struct logfile {
00043 char fn[256];
00044 int logflags;
00045 FILE *f;
00046 struct logfile *next;
00047 };
00048
00049 static struct logfile *logfiles = NULL;
00050
00051 static int msgcnt = 0;
00052
00053 static FILE *eventlog = NULL;
00054
00055 static char *levels[] = {
00056 "DEBUG",
00057 "EVENT",
00058 "NOTICE",
00059 "WARNING",
00060 "ERROR"
00061 };
00062
00063 static int colors[] = {
00064 COLOR_BRGREEN,
00065 COLOR_BRBLUE,
00066 COLOR_YELLOW,
00067 COLOR_BRRED,
00068 COLOR_RED
00069 };
00070
00071 static int make_components(char *s, int lineno)
00072 {
00073 char *w;
00074 int res = 0;
00075 char *stringp=NULL;
00076 stringp=s;
00077 w = strsep(&stringp, ",");
00078 while(w) {
00079 while(*w && (*w < 33))
00080 w++;
00081 if (!strcasecmp(w, "debug"))
00082 res |= (1 << 0);
00083 else if (!strcasecmp(w, "notice"))
00084 res |= (1 << 2);
00085 else if (!strcasecmp(w, "warning"))
00086 res |= (1 << 3);
00087 else if (!strcasecmp(w, "error"))
00088 res |= (1 << 4);
00089 else {
00090 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
00091 }
00092 w = strsep(&stringp, ",");
00093 }
00094 return res;
00095 }
00096
00097 static struct logfile *make_logfile(char *fn, char *components, int lineno)
00098 {
00099 struct logfile *f;
00100 char tmp[256];
00101 if (!strlen(fn))
00102 return NULL;
00103 f = malloc(sizeof(struct logfile));
00104 if (f) {
00105 memset(f, 0, sizeof(f));
00106 strncpy(f->fn, fn, sizeof(f->fn) - 1);
00107 if (!strcasecmp(fn, "ignore")) {
00108 f->f = NULL;
00109 } else if (!strcasecmp(fn, "console")) {
00110 f->f = stdout;
00111 } else {
00112 if (fn[0] == '/')
00113 strncpy(tmp, fn, sizeof(tmp) - 1);
00114 else
00115 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, fn);
00116 f->f = fopen(tmp, "a");
00117 if (!f->f) {
00118
00119 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", tmp, strerror(errno));
00120 }
00121 }
00122 f->logflags = make_components(components, lineno);
00123
00124 }
00125 return f;
00126 }
00127
00128 static void init_logger_chain(void)
00129 {
00130 struct logfile *f, *cur;
00131 struct ast_config *cfg;
00132 struct ast_variable *var;
00133
00134 ast_mutex_lock(&loglock);
00135
00136
00137 f = logfiles;
00138 while(f) {
00139 cur = f->next;
00140 if (f->f && (f->f != stdout) && (f->f != stderr))
00141 fclose(f->f);
00142 free(f);
00143 f = cur;
00144 }
00145
00146 logfiles = NULL;
00147
00148 ast_mutex_unlock(&loglock);
00149 cfg = ast_load("logger.conf");
00150 ast_mutex_lock(&loglock);
00151
00152
00153 if (!cfg) {
00154 ast_mutex_unlock(&loglock);
00155 return;
00156 }
00157 var = ast_variable_browse(cfg, "logfiles");
00158 while(var) {
00159 f = make_logfile(var->name, var->value, var->lineno);
00160 if (f) {
00161 f->next = logfiles;
00162 logfiles = f;
00163 }
00164 var = var->next;
00165 }
00166 if (!logfiles) {
00167
00168 logfiles = make_logfile("ignore", "", -1);
00169 }
00170 ast_destroy(cfg);
00171 ast_mutex_unlock(&loglock);
00172
00173
00174 }
00175
00176 static struct verb {
00177 void (*verboser)(const char *string, int opos, int replacelast, int complete);
00178 struct verb *next;
00179 } *verboser = NULL;
00180
00181 int init_logger(void)
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 }
00198
00199 int reload_logger(void)
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 }
00221
00222 extern void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
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 ) {
00240 time(&t);
00241 localtime_r(&t,&tm);
00242 if (&tm) {
00243
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
00252
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 }
00290
00291 extern void ast_verbose(const char *fmt, ...)
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
00311 m = malloc(sizeof(struct msglist));
00312 msgcnt++;
00313 } else {
00314
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 }
00342
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 }
00351
00352 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
00353 {
00354 struct msglist *m;
00355 m = list;
00356 ast_mutex_lock(&msglist_lock);
00357 while(m) {
00358
00359 v(m->msg, 0, 0, 1);
00360 m = m->next;
00361 }
00362 ast_mutex_unlock(&msglist_lock);
00363 return 0;
00364 }
00365
00366 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
00367 {
00368 struct msglist *m;
00369 struct verb *tmp;
00370
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
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 }
00387
00388 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
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 }