2 * Cheops Next Generation
4 * Mark Spencer <markster@marko.net>
6 * Copyright(C) 1999, Adtran, Inc.
8 * Distributed under the terms of the GNU General Public License (GPL) Version 2
18 #include <asterisk/logger.h>
19 #include <asterisk/options.h>
27 #define AST_EVENT_LOG AST_LOG_DIR "/" EVENTLOG
29 #define MAX_MSG_QUEUE 200
31 static pthread_mutex_t msglist_lock = PTHREAD_MUTEX_INITIALIZER;
33 static struct msglist {
36 } *list = NULL, *last = NULL;
38 static int msgcnt = 0;
40 static FILE *eventlog = NULL;
42 static char *levels[] = {
51 void (*verboser)(char *string, int opos, int replacelast, int complete);
58 mkdir(AST_LOG_DIR, 0755);
59 eventlog = fopen(AST_EVENT_LOG, "a");
61 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
63 ast_verbose("Asterisk Event Logger Started\n");
66 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
70 extern void ast_log(int level, char *file, int line, char *function, char *fmt, ...)
79 if (level == 1 /* Event */) {
83 /* Log events into the event log file, with a different format */
84 strftime(date, sizeof(date), "%b %e %T", tm);
85 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
86 vfprintf(eventlog, fmt, ap);
89 ast_log(LOG_WARNING, "Unable to retrieve local time?\n");
91 fprintf(stdout, "%s: File %s, Line %d (%s): ", levels[level], file, line, function);
92 vfprintf(stdout, fmt, ap);
98 extern void ast_verbose(char *fmt, ...)
100 static char stuff[256];
101 static int pos = 0, opos;
102 static int replacelast = 0, complete;
107 pthread_mutex_lock(&msglist_lock);
108 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
111 if (fmt[strlen(fmt)-1] == '\n')
116 if (msgcnt < MAX_MSG_QUEUE) {
117 /* Allocate new structure */
118 m = malloc(sizeof(struct msglist));
121 /* Recycle the oldest entry */
127 m->msg = strdup(stuff);
137 ast_log(LOG_DEBUG, "Out of memory\n");
145 v->verboser(stuff, opos, replacelast, complete);
149 fprintf(stdout, stuff + opos);
151 if (fmt[strlen(fmt)-1] != '\n')
154 replacelast = pos = 0;
157 pthread_mutex_unlock(&msglist_lock);
161 int ast_register_verbose(void (*v)(char *string, int opos, int replacelast, int complete))
165 /* XXX Should be more flexible here, taking > 1 verboser XXX */
166 if ((tmp = malloc(sizeof (struct verb)))) {
168 pthread_mutex_lock(&msglist_lock);
169 tmp->next = verboser;
173 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
177 pthread_mutex_unlock(&msglist_lock);
183 int ast_unregister_verbose(void (*v)(char *string, int opos, int replacelast, int complete))
186 struct verb *tmp, *tmpl=NULL;
187 pthread_mutex_lock(&msglist_lock);
190 if (tmp->verboser == v) {
192 tmpl->next = tmp->next;
194 verboser = tmp->next;
202 pthread_mutex_unlock(&msglist_lock);