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. */
11 #include "Core/core_interface.h"
12 #include "gras_private.h"
18 #define MIN(a,b) ((a) < (b) ? (a) : (b))
23 gras_log_priority_t thresh;
26 static gras_dynar_t *gras_log_settings=NULL;
27 static void _free_setting(void *s) {
28 gras_log_setting_t *set=(gras_log_setting_t*)s;
35 const char *gras_log_priority_names[8] = {
45 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
47 "root", gras_log_priority_uninitialized, 0,
50 GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
51 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,GRAS);
54 static void _apply_control(gras_log_category_t* cat) {
56 gras_log_setting_t *setting=NULL;
59 if (!gras_log_settings)
62 gras_assert0(cat,"NULL category");
63 gras_assert(cat->name);
65 gras_dynar_foreach(gras_log_settings,cursor,setting) {
66 gras_assert0(setting,"Damnit, NULL cat in the list");
67 gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
69 if (!strcmp(setting->catname,cat->name)) {
72 gras_log_threshold_set(cat, setting->thresh);
73 gras_dynar_cursor_rm(gras_log_settings,&cursor);
75 if (cat->threshold <= gras_log_priority_verbose) {
76 gras_log_event_t _log_ev =
77 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
78 "Apply settings for category '%s': set threshold to %s (=%d)",};
79 _gras_log_event_log(&_log_ev, cat->name,
80 gras_log_priority_names[cat->threshold], cat->threshold);
84 if (!found && cat->threshold <= gras_log_priority_verbose) {
85 gras_log_event_t _log_ev =
86 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
87 "Category '%s': inherited threshold = %s (=%d)",};
88 _gras_log_event_log(&_log_ev, cat->name,
89 gras_log_priority_names[cat->threshold], cat->threshold);
94 void _gras_log_event_log( gras_log_event_t* ev, ...) {
95 gras_log_category_t* cat = ev->cat;
98 gras_log_appender_t* appender = cat->appender;
99 if (appender != NULL) {
100 appender->do_append(appender, ev);
102 if (!cat->willLogToParent)
110 static void _cat_init(gras_log_category_t* category) {
111 if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
112 category->threshold = gras_log_priority_info;
113 category->appender = gras_log_default_appender;
115 gras_log_parent_set(category, category->parent);
117 _apply_control(category);
121 * This gets called the first time a category is referenced and performs the
123 * Also resets threshold to inherited!
125 int _gras_log_cat_init(gras_log_priority_t priority,
126 gras_log_category_t* category) {
130 return priority >= category->threshold;
133 void gras_log_parent_set(gras_log_category_t* cat,
134 gras_log_category_t* parent) {
136 gras_assert0(cat,"NULL category to be given a parent");
137 gras_assert1(parent,"The parent category of %s is NULL",cat->name);
139 // unlink from current parent
140 if (cat->threshold != gras_log_priority_uninitialized) {
141 gras_log_category_t** cpp = &parent->firstChild;
142 while(*cpp != cat && *cpp != NULL) {
143 cpp = &(*cpp)->nextSibling;
146 *cpp = cat->nextSibling;
150 cat->parent = parent;
151 cat->nextSibling = parent->firstChild;
152 parent->firstChild = cat;
154 // Make sure parent is initialized
155 if (parent->threshold == gras_log_priority_uninitialized) {
160 cat->threshold = parent->threshold;
161 cat->isThreshInherited = 1;
164 static void _set_inherited_thresholds(gras_log_category_t* cat) {
165 gras_log_category_t* child = cat->firstChild;
166 for( ; child != NULL; child = child->nextSibling) {
167 if (child->isThreshInherited) {
168 if (cat != &_GRAS_LOGV(log))
169 VERB3("Set category threshold of %s to %s (=%d)",
170 child->name,gras_log_priority_names[cat->threshold],cat->threshold);
171 child->threshold = cat->threshold;
172 _set_inherited_thresholds(child);
177 void gras_log_threshold_set(gras_log_category_t* cat,
178 gras_log_priority_t threshold) {
179 cat->threshold = threshold;
180 cat->isThreshInherited = 0;
181 _set_inherited_thresholds(cat);
184 static gras_error_t _gras_log_parse_setting(const char* control_string,
185 gras_log_setting_t *set) {
186 const char *name, *dot, *eq;
189 if (!*control_string)
191 DEBUG1("Parse log setting '%s'",control_string);
193 control_string += strspn(control_string, " ");
194 name = control_string;
195 control_string += strcspn(control_string, ".= ");
196 dot = control_string;
197 control_string += strcspn(control_string, "= ");
199 control_string += strcspn(control_string, " ");
201 gras_assert1(*dot == '.' && *eq == '=',
202 "Invalid control string '%s'",control_string);
204 if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
206 char *neweq=strdup(eq+1);
209 while (*(++p) != '\0') {
210 if (*p >= 'a' && *p <= 'z') {
215 DEBUG1("New priority name = %s",neweq);
216 for (i=0; i<6; i++) {
217 if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
218 DEBUG1("This is priority %d",i);
225 gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
230 snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
231 gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
233 if (!(set->catname=malloc(dot - name+1)))
236 strncat(set->catname,name,dot-name);
237 DEBUG1("This is for cat '%s'", set->catname);
241 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
242 gras_error_t errcode;
243 gras_log_category_t *child;
245 if (!strcmp(cat->name,name)) {
249 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
250 errcode=_gras_log_cat_searchsub(child,name,whereto);
251 if (errcode==no_error)
254 return mismatch_error;
257 static void _cleanup_double_spaces(char *s) {
280 if (e ^ !isspace(*p))
290 * gras_log_control_set:
292 * @Returns: malloc_error or no_error
294 * Typically passed a command-line argument. The string has the syntax:
296 * ( [category] "." [keyword] "=" value (" ")... )...
298 * where [category] is one the category names and keyword is one of the
301 * thresh value is an integer priority level. Sets the category's
302 * threshold priority.
305 * This routine may only be called once and that must be before any other
306 * logging command! Typically, this is done from main().
308 gras_error_t gras_log_control_set(const char* control_string) {
309 gras_error_t errcode;
310 gras_log_setting_t *set;
315 DEBUG1("Parse log settings '%s'",control_string);
316 if (control_string == NULL)
318 if (gras_log_settings == NULL)
319 TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),_free_setting));
321 if (!(set = malloc(sizeof(gras_log_setting_t))))
324 if (!(cs=strdup(control_string)))
326 _cleanup_double_spaces(cs);
329 gras_log_category_t *cat;
339 errcode = _gras_log_parse_setting(p,set);
340 if (errcode != no_error) {
345 TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),mismatch_error);
346 if (errcode == mismatch_error) {
347 DEBUG0("Store for further application");
348 DEBUG1("push %p to the settings",set);
349 TRY(gras_dynar_push(gras_log_settings,&set));
350 /* malloc in advance the next slot */
351 if (!(set = malloc(sizeof(gras_log_setting_t)))) {
356 DEBUG0("Apply directly");
358 gras_log_threshold_set(cat,set->thresh);
366 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
370 void gras_log_exit(void) {
371 gras_dynar_free(gras_log_settings);