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 "gras_private.h"
17 #define MIN(a,b) ((a) < (b) ? (a) : (b))
22 gras_log_priority_t thresh;
25 static gras_dynar_t *gras_log_settings=NULL;
26 static void _free_setting(void *s) {
27 gras_log_setting_t *set=(gras_log_setting_t*)s;
34 const char *gras_log_priority_names[8] = {
44 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
46 "root", gras_log_priority_uninitialized, 0,
49 GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
50 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,GRAS);
53 static void _apply_control(gras_log_category_t* cat) {
55 gras_log_setting_t *setting=NULL;
58 if (!gras_log_settings)
61 gras_assert0(cat,"NULL category");
62 gras_assert(cat->name);
64 gras_dynar_foreach(gras_log_settings,cursor,setting) {
65 gras_assert0(setting,"Damnit, NULL cat in the list");
66 gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
68 if (!strcmp(setting->catname,cat->name)) {
71 gras_log_threshold_set(cat, setting->thresh);
72 gras_dynar_cursor_rm(gras_log_settings,&cursor);
74 if (cat->threshold <= gras_log_priority_verbose) {
75 gras_log_event_t _log_ev =
76 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
77 "Apply settings for category '%s': set threshold to %s (=%d)",};
78 _gras_log_event_log(&_log_ev, cat->name,
79 gras_log_priority_names[cat->threshold], cat->threshold);
83 if (!found && cat->threshold <= gras_log_priority_verbose) {
84 gras_log_event_t _log_ev =
85 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
86 "Category '%s': inherited threshold = %s (=%d)",};
87 _gras_log_event_log(&_log_ev, cat->name,
88 gras_log_priority_names[cat->threshold], cat->threshold);
93 void _gras_log_event_log( gras_log_event_t* ev, ...) {
94 gras_log_category_t* cat = ev->cat;
97 gras_log_appender_t* appender = cat->appender;
98 if (appender != NULL) {
99 appender->do_append(appender, ev);
101 if (!cat->willLogToParent)
109 static void _cat_init(gras_log_category_t* category) {
110 if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
111 category->threshold = gras_log_priority_info;
112 category->appender = gras_log_default_appender;
114 gras_log_parent_set(category, category->parent);
116 _apply_control(category);
120 * This gets called the first time a category is referenced and performs the
122 * Also resets threshold to inherited!
124 int _gras_log_cat_init(gras_log_priority_t priority,
125 gras_log_category_t* category) {
129 return priority >= category->threshold;
132 void gras_log_parent_set(gras_log_category_t* cat,
133 gras_log_category_t* parent) {
135 gras_assert0(cat,"NULL category to be given a parent");
136 gras_assert1(parent,"The parent category of %s is NULL",cat->name);
138 // unlink from current parent
139 if (cat->threshold != gras_log_priority_uninitialized) {
140 gras_log_category_t** cpp = &parent->firstChild;
141 while(*cpp != cat && *cpp != NULL) {
142 cpp = &(*cpp)->nextSibling;
145 *cpp = cat->nextSibling;
149 cat->parent = parent;
150 cat->nextSibling = parent->firstChild;
151 parent->firstChild = cat;
153 // Make sure parent is initialized
154 if (parent->threshold == gras_log_priority_uninitialized) {
159 cat->threshold = parent->threshold;
160 cat->isThreshInherited = 1;
163 static void _set_inherited_thresholds(gras_log_category_t* cat) {
164 gras_log_category_t* child = cat->firstChild;
165 for( ; child != NULL; child = child->nextSibling) {
166 if (child->isThreshInherited) {
167 if (cat != &_GRAS_LOGV(log))
168 VERB3("Set category threshold of %s to %s (=%d)",
169 child->name,gras_log_priority_names[cat->threshold],cat->threshold);
170 child->threshold = cat->threshold;
171 _set_inherited_thresholds(child);
176 void gras_log_threshold_set(gras_log_category_t* cat,
177 gras_log_priority_t threshold) {
178 cat->threshold = threshold;
179 cat->isThreshInherited = 0;
180 _set_inherited_thresholds(cat);
183 static gras_error_t _gras_log_parse_setting(const char* control_string,
184 gras_log_setting_t *set) {
185 const char *name, *dot, *eq;
188 if (!*control_string)
190 DEBUG1("Parse log setting '%s'",control_string);
192 control_string += strspn(control_string, " ");
193 name = control_string;
194 control_string += strcspn(control_string, ".= ");
195 dot = control_string;
196 control_string += strcspn(control_string, "= ");
198 control_string += strcspn(control_string, " ");
200 gras_assert1(*dot == '.' && *eq == '=',
201 "Invalid control string '%s'",control_string);
203 if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
205 char *neweq=strdup(eq+1);
208 while (*(++p) != '\0') {
209 if (*p >= 'a' && *p <= 'z') {
214 DEBUG1("New priority name = %s",neweq);
215 for (i=0; i<6; i++) {
216 if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
217 DEBUG1("This is priority %d",i);
224 gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
229 snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
230 gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
232 if (!(set->catname=malloc(dot - name+1)))
235 strncat(set->catname,name,dot-name);
236 DEBUG1("This is for cat '%s'", set->catname);
240 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
241 gras_error_t errcode;
242 gras_log_category_t *child;
244 if (!strcmp(cat->name,name)) {
248 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
249 errcode=_gras_log_cat_searchsub(child,name,whereto);
250 if (errcode==no_error)
253 return mismatch_error;
256 static void _cleanup_double_spaces(char *s) {
279 if (e ^ !isspace(*p))
289 * gras_log_control_set:
291 * @Returns: malloc_error or no_error
293 * Typically passed a command-line argument. The string has the syntax:
295 * ( [category] "." [keyword] "=" value (" ")... )...
297 * where [category] is one the category names and keyword is one of the
300 * thresh value is an integer priority level. Sets the category's
301 * threshold priority.
304 * This routine may only be called once and that must be before any other
305 * logging command! Typically, this is done from main().
307 gras_error_t gras_log_control_set(const char* control_string) {
308 gras_error_t errcode;
309 gras_log_setting_t *set;
314 DEBUG1("Parse log settings '%s'",control_string);
315 if (control_string == NULL)
317 if (gras_log_settings == NULL)
318 TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),_free_setting));
320 if (!(set = malloc(sizeof(gras_log_setting_t))))
323 if (!(cs=strdup(control_string)))
325 _cleanup_double_spaces(cs);
328 gras_log_category_t *cat;
338 errcode = _gras_log_parse_setting(p,set);
339 if (errcode != no_error) {
344 TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),mismatch_error);
345 if (errcode == mismatch_error) {
346 DEBUG0("Store for further application");
347 DEBUG1("push %p to the settings",set);
348 TRY(gras_dynar_push(gras_log_settings,&set));
349 /* malloc in advance the next slot */
350 if (!(set = malloc(sizeof(gras_log_setting_t)))) {
355 DEBUG0("Apply directly");
357 gras_log_threshold_set(cat,set->thresh);
365 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
369 void gras_log_finalize(void);
370 void gras_log_finalize(void) {
371 gras_dynar_free(gras_log_settings);