2 * Cheops Next Generation
4 * Mark Spencer <markster@marko.net>
6 * Copyright(C)1999, Mark Spencer
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;
32 static pthread_mutex_t loglock = PTHREAD_MUTEX_INITIALIZER;
34 static struct msglist {
37 } *list = NULL, *last = NULL;
39 static int msgcnt = 0;
41 static FILE *eventlog = NULL;
43 static char *levels[] = {
52 void (*verboser)(char *string, int opos, int replacelast, int complete);
59 mkdir(AST_LOG_DIR, 0755);
60 eventlog = fopen(AST_EVENT_LOG, "a");
62 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
64 ast_verbose("Asterisk Event Logger Started\n");
67 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
71 extern void ast_log(int level, char *file, int line, char *function, char *fmt, ...)
79 pthread_mutex_lock(&loglock);
80 if (level == 1 /* Event */) {
84 /* Log events into the event log file, with a different format */
85 strftime(date, sizeof(date), "%b %e %T", tm);
86 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
87 vfprintf(eventlog, fmt, ap);
90 ast_log(LOG_WARNING, "Unable to retrieve local time?\n");
92 fprintf(stdout, "%s[%ld]: File %s, Line %d (%s): ", levels[level], pthread_self(), file, line, function);
93 vfprintf(stdout, fmt, ap);
96 pthread_mutex_unlock(&loglock);
100 extern void ast_verbose(char *fmt, ...)
102 static char stuff[4096];
103 static int pos = 0, opos;
104 static int replacelast = 0, complete;
109 pthread_mutex_lock(&msglist_lock);
110 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
113 if (fmt[strlen(fmt)-1] == '\n')
118 if (msgcnt < MAX_MSG_QUEUE) {
119 /* Allocate new structure */
120 m = malloc(sizeof(struct msglist));
123 /* Recycle the oldest entry */
129 m->msg = strdup(stuff);
139 ast_log(LOG_DEBUG, "Out of memory\n");
144 pthread_mutex_lock(&loglock);
148 v->verboser(stuff, opos, replacelast, complete);
152 fprintf(stdout, stuff + opos);
154 if (fmt[strlen(fmt)-1] != '\n')
157 replacelast = pos = 0;
158 pthread_mutex_unlock(&loglock);
160 pthread_mutex_unlock(&msglist_lock);
164 int ast_register_verbose(void (*v)(char *string, int opos, int replacelast, int complete))
168 /* XXX Should be more flexible here, taking > 1 verboser XXX */
169 if ((tmp = malloc(sizeof (struct verb)))) {
171 pthread_mutex_lock(&msglist_lock);
172 tmp->next = verboser;
176 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
180 pthread_mutex_unlock(&msglist_lock);
186 int ast_unregister_verbose(void (*v)(char *string, int opos, int replacelast, int complete))
189 struct verb *tmp, *tmpl=NULL;
190 pthread_mutex_lock(&msglist_lock);
193 if (tmp->verboser == v) {
195 tmpl->next = tmp->next;
197 verboser = tmp->next;
205 pthread_mutex_unlock(&msglist_lock);