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] = {
46 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
48 "root", gras_log_priority_uninitialized, 0,
51 GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
52 GRAS_LOG_NEW_SUBCATEGORY(tbx,GRAS);
53 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,tbx);
56 static void _apply_control(gras_log_category_t* cat) {
58 gras_log_setting_t *setting=NULL;
61 if (!gras_log_settings)
64 gras_assert0(cat,"NULL category");
65 gras_assert(cat->name);
67 gras_dynar_foreach(gras_log_settings,cursor,setting) {
68 gras_assert0(setting,"Damnit, NULL cat in the list");
69 gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
71 if (!strcmp(setting->catname,cat->name)) {
74 gras_log_threshold_set(cat, setting->thresh);
75 gras_dynar_cursor_rm(gras_log_settings,&cursor);
77 if (cat->threshold <= gras_log_priority_verbose) {
78 gras_log_event_t _log_ev =
79 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
80 _gras_log_event_log(&_log_ev,
81 "Apply settings for category '%s': set threshold to %s (=%d)",
83 gras_log_priority_names[cat->threshold], cat->threshold);
87 if (!found && cat->threshold <= gras_log_priority_verbose) {
88 gras_log_event_t _log_ev =
89 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
90 _gras_log_event_log(&_log_ev,
91 "Category '%s': inherited threshold = %s (=%d)",
93 gras_log_priority_names[cat->threshold], cat->threshold);
98 void _gras_log_event_log( gras_log_event_t* ev, const char *fmt, ...) {
99 gras_log_category_t* cat = ev->cat;
100 va_start(ev->ap, fmt);
102 gras_log_appender_t* appender = cat->appender;
103 if (appender != NULL) {
104 appender->do_append(appender, ev, fmt);
106 if (!cat->willLogToParent)
114 static void _cat_init(gras_log_category_t* category) {
115 if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
116 category->threshold = gras_log_priority_info;
117 category->appender = gras_log_default_appender;
119 gras_log_parent_set(category, category->parent);
121 _apply_control(category);
125 * This gets called the first time a category is referenced and performs the
127 * Also resets threshold to inherited!
129 int _gras_log_cat_init(gras_log_priority_t priority,
130 gras_log_category_t* category) {
134 return priority >= category->threshold;
137 void gras_log_parent_set(gras_log_category_t* cat,
138 gras_log_category_t* parent) {
140 gras_assert0(cat,"NULL category to be given a parent");
141 gras_assert1(parent,"The parent category of %s is NULL",cat->name);
143 // unlink from current parent
144 if (cat->threshold != gras_log_priority_uninitialized) {
145 gras_log_category_t** cpp = &parent->firstChild;
146 while(*cpp != cat && *cpp != NULL) {
147 cpp = &(*cpp)->nextSibling;
150 *cpp = cat->nextSibling;
154 cat->parent = parent;
155 cat->nextSibling = parent->firstChild;
156 parent->firstChild = cat;
158 // Make sure parent is initialized
159 if (parent->threshold == gras_log_priority_uninitialized) {
164 cat->threshold = parent->threshold;
165 cat->isThreshInherited = 1;
168 static void _set_inherited_thresholds(gras_log_category_t* cat) {
169 gras_log_category_t* child = cat->firstChild;
170 for( ; child != NULL; child = child->nextSibling) {
171 if (child->isThreshInherited) {
172 if (cat != &_GRAS_LOGV(log))
173 VERB3("Set category threshold of %s to %s (=%d)",
174 child->name,gras_log_priority_names[cat->threshold],cat->threshold);
175 child->threshold = cat->threshold;
176 _set_inherited_thresholds(child);
181 void gras_log_threshold_set(gras_log_category_t* cat,
182 gras_log_priority_t threshold) {
183 cat->threshold = threshold;
184 cat->isThreshInherited = 0;
185 _set_inherited_thresholds(cat);
188 static gras_error_t _gras_log_parse_setting(const char* control_string,
189 gras_log_setting_t *set) {
190 const char *name, *dot, *eq;
193 if (!*control_string)
195 DEBUG1("Parse log setting '%s'",control_string);
197 control_string += strspn(control_string, " ");
198 name = control_string;
199 control_string += strcspn(control_string, ".= ");
200 dot = control_string;
201 control_string += strcspn(control_string, "= ");
203 control_string += strcspn(control_string, " ");
205 gras_assert1(*dot == '.' && *eq == '=',
206 "Invalid control string '%s'",control_string);
208 if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
210 char *neweq=strdup(eq+1);
213 while (*(++p) != '\0') {
214 if (*p >= 'a' && *p <= 'z') {
219 DEBUG1("New priority name = %s",neweq);
220 for (i=0; i<6; i++) {
221 if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
222 DEBUG1("This is priority %d",i);
229 gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
234 snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
235 gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
237 if (!(set->catname=malloc(dot - name+1)))
240 strncpy(set->catname,name,dot-name);
241 set->catname[dot-name]='\0'; /* Just in case */
242 DEBUG1("This is for cat '%s'", set->catname);
246 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
247 gras_error_t errcode;
248 gras_log_category_t *child;
250 if (!strcmp(cat->name,name)) {
254 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
255 errcode=_gras_log_cat_searchsub(child,name,whereto);
256 if (errcode==no_error)
259 return mismatch_error;
262 static void _cleanup_double_spaces(char *s) {
285 if (e ^ !isspace(*p))
295 * gras_log_control_set:
297 * @Returns: malloc_error or no_error
299 * Typically passed a command-line argument. The string has the syntax:
301 * ( [category] "." [keyword] "=" value (" ")... )...
303 * where [category] is one the category names and keyword is one of the
306 * thresh value is an integer priority level. Sets the category's
307 * threshold priority.
310 * This routine may only be called once and that must be before any other
311 * logging command! Typically, this is done from main().
313 gras_error_t gras_log_control_set(const char* control_string) {
314 gras_error_t errcode;
315 gras_log_setting_t *set;
320 DEBUG1("Parse log settings '%s'",control_string);
321 if (control_string == NULL)
323 if (gras_log_settings == NULL)
324 TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
327 if (!(set = malloc(sizeof(gras_log_setting_t))))
330 if (!(cs=strdup(control_string)))
332 _cleanup_double_spaces(cs);
335 gras_log_category_t *cat;
345 errcode = _gras_log_parse_setting(p,set);
346 if (errcode != no_error) {
351 TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),
353 if (errcode == mismatch_error) {
354 DEBUG0("Store for further application");
355 DEBUG1("push %p to the settings",set);
356 TRY(gras_dynar_push(gras_log_settings,&set));
357 /* malloc in advance the next slot */
358 if (!(set = malloc(sizeof(gras_log_setting_t)))) {
363 DEBUG0("Apply directly");
365 gras_log_threshold_set(cat,set->thresh);
373 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
377 void gras_log_exit(void) {
378 VERB0("Exiting log");
379 gras_dynar_free(gras_log_settings);