3 /* log - a generic logging facility in the spirit of log4j */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003, 2004 Martin Quinson. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "Core/core_interface.h"
13 #include "gras_private.h"
19 #define MIN(a,b) ((a) < (b) ? (a) : (b))
24 gras_log_priority_t thresh;
27 static gras_dynar_t *gras_log_settings=NULL;
28 static void _free_setting(void *s) {
29 gras_log_setting_t *set=(gras_log_setting_t*)s;
32 // free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log
36 const char *gras_log_priority_names[8] = {
47 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
49 "root", gras_log_priority_uninitialized, 0,
52 GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
53 GRAS_LOG_NEW_SUBCATEGORY(tbx,GRAS);
54 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,tbx);
57 static void _apply_control(gras_log_category_t* cat) {
59 gras_log_setting_t *setting=NULL;
62 if (!gras_log_settings)
65 gras_assert0(cat,"NULL category");
66 gras_assert(cat->name);
68 gras_dynar_foreach(gras_log_settings,cursor,setting) {
69 gras_assert0(setting,"Damnit, NULL cat in the list");
70 gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
72 if (!strcmp(setting->catname,cat->name)) {
75 gras_log_threshold_set(cat, setting->thresh);
76 gras_dynar_cursor_rm(gras_log_settings,&cursor);
78 if (cat->threshold <= gras_log_priority_verbose) {
79 gras_log_event_t _log_ev =
80 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
81 _gras_log_event_log(&_log_ev,
82 "Apply settings for category '%s': set threshold to %s (=%d)",
84 gras_log_priority_names[cat->threshold], cat->threshold);
88 if (!found && cat->threshold <= gras_log_priority_verbose) {
89 gras_log_event_t _log_ev =
90 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
91 _gras_log_event_log(&_log_ev,
92 "Category '%s': inherited threshold = %s (=%d)",
94 gras_log_priority_names[cat->threshold], cat->threshold);
99 void _gras_log_event_log( gras_log_event_t* ev, const char *fmt, ...) {
100 gras_log_category_t* cat = ev->cat;
101 va_start(ev->ap, fmt);
103 gras_log_appender_t* appender = cat->appender;
104 if (appender != NULL) {
105 appender->do_append(appender, ev, fmt);
107 if (!cat->willLogToParent)
115 static void _cat_init(gras_log_category_t* category) {
116 if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
117 category->threshold = gras_log_priority_info;
118 category->appender = gras_log_default_appender;
120 gras_log_parent_set(category, category->parent);
122 _apply_control(category);
126 * This gets called the first time a category is referenced and performs the
128 * Also resets threshold to inherited!
130 int _gras_log_cat_init(gras_log_priority_t priority,
131 gras_log_category_t* category) {
135 return priority >= category->threshold;
138 void gras_log_parent_set(gras_log_category_t* cat,
139 gras_log_category_t* parent) {
141 gras_assert0(cat,"NULL category to be given a parent");
142 gras_assert1(parent,"The parent category of %s is NULL",cat->name);
144 // unlink from current parent
145 if (cat->threshold != gras_log_priority_uninitialized) {
146 gras_log_category_t** cpp = &parent->firstChild;
147 while(*cpp != cat && *cpp != NULL) {
148 cpp = &(*cpp)->nextSibling;
151 *cpp = cat->nextSibling;
155 cat->parent = parent;
156 cat->nextSibling = parent->firstChild;
157 parent->firstChild = cat;
159 // Make sure parent is initialized
160 if (parent->threshold == gras_log_priority_uninitialized) {
165 cat->threshold = parent->threshold;
166 cat->isThreshInherited = 1;
169 static void _set_inherited_thresholds(gras_log_category_t* cat) {
170 gras_log_category_t* child = cat->firstChild;
171 for( ; child != NULL; child = child->nextSibling) {
172 if (child->isThreshInherited) {
173 if (cat != &_GRAS_LOGV(log))
174 VERB3("Set category threshold of %s to %s (=%d)",
175 child->name,gras_log_priority_names[cat->threshold],cat->threshold);
176 child->threshold = cat->threshold;
177 _set_inherited_thresholds(child);
182 void gras_log_threshold_set(gras_log_category_t* cat,
183 gras_log_priority_t threshold) {
184 cat->threshold = threshold;
185 cat->isThreshInherited = 0;
186 _set_inherited_thresholds(cat);
189 static gras_error_t _gras_log_parse_setting(const char* control_string,
190 gras_log_setting_t *set) {
191 const char *name, *dot, *eq;
194 if (!*control_string)
196 DEBUG1("Parse log setting '%s'",control_string);
198 control_string += strspn(control_string, " ");
199 name = control_string;
200 control_string += strcspn(control_string, ".= ");
201 dot = control_string;
202 control_string += strcspn(control_string, "= ");
204 control_string += strcspn(control_string, " ");
206 gras_assert1(*dot == '.' && *eq == '=',
207 "Invalid control string '%s'",control_string);
209 if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
211 char *neweq=strdup(eq+1);
214 while (*(++p) != '\0') {
215 if (*p >= 'a' && *p <= 'z') {
220 DEBUG1("New priority name = %s",neweq);
221 for (i=0; i<gras_log_priority_infinite-1; i++) {
222 if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
223 DEBUG1("This is priority %d",i);
227 if (i<gras_log_priority_infinite-1) {
230 gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
235 snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
236 gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
238 if (!(set->catname=malloc(dot - name+1)))
241 strncpy(set->catname,name,dot-name);
242 set->catname[dot-name]='\0'; /* Just in case */
243 DEBUG1("This is for cat '%s'", set->catname);
247 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
248 gras_error_t errcode;
249 gras_log_category_t *child;
251 if (!strcmp(cat->name,name)) {
255 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
256 errcode=_gras_log_cat_searchsub(child,name,whereto);
257 if (errcode==no_error)
260 return mismatch_error;
263 static void _cleanup_double_spaces(char *s) {
286 if (e ^ !isspace(*p))
296 * gras_log_control_set:
298 * @Returns: malloc_error or no_error
300 * Typically passed a command-line argument. The string has the syntax:
302 * ( [category] "." [keyword] "=" value (" ")... )...
304 * where [category] is one the category names and keyword is one of the
307 * thresh value is an integer priority level. Sets the category's
308 * threshold priority.
311 * This routine may only be called once and that must be before any other
312 * logging command! Typically, this is done from main().
314 gras_error_t gras_log_control_set(const char* control_string) {
315 gras_error_t errcode;
316 gras_log_setting_t *set;
321 DEBUG1("Parse log settings '%s'",control_string);
322 if (control_string == NULL)
324 if (gras_log_settings == NULL)
325 TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
328 if (!(set = malloc(sizeof(gras_log_setting_t))))
331 if (!(cs=strdup(control_string)))
333 _cleanup_double_spaces(cs);
336 gras_log_category_t *cat;
346 errcode = _gras_log_parse_setting(p,set);
347 if (errcode != no_error) {
352 TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),
354 if (errcode == mismatch_error) {
355 DEBUG0("Store for further application");
356 DEBUG1("push %p to the settings",set);
357 TRY(gras_dynar_push(gras_log_settings,&set));
358 /* malloc in advance the next slot */
359 if (!(set = malloc(sizeof(gras_log_setting_t)))) {
364 DEBUG0("Apply directly");
366 gras_log_threshold_set(cat,set->thresh);
374 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
378 void gras_log_exit(void) {
379 VERB0("Exiting log");
380 gras_dynar_free(gras_log_settings);