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 "gros_interface.h"
13 #include "gras_private.h"
20 gras_log_priority_t thresh;
23 static gras_dynar_t *gras_log_settings=NULL;
24 static void _free_setting(void *s) {
25 gras_log_setting_t *set=(gras_log_setting_t*)s;
27 gras_free(set->catname);
28 /* free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
32 const char *gras_log_priority_names[8] = {
43 gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
45 "root", gras_log_priority_uninitialized, 0,
49 GRAS_LOG_NEW_SUBCATEGORY(gras,GRAS_LOG_ROOT_CAT,"All GRAS categories");
50 GRAS_LOG_NEW_SUBCATEGORY(gros,GRAS_LOG_ROOT_CAT,"All GROS categories (gras toolbox)");
51 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,gros,"Loggings from the logging mecanism itself");
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",(void*)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 _gras_log_event_log(&_log_ev,
79 "Apply settings for category '%s': set threshold to %s (=%d)",
81 gras_log_priority_names[cat->threshold], cat->threshold);
85 if (!found && cat->threshold <= gras_log_priority_verbose) {
86 gras_log_event_t _log_ev =
87 {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
88 _gras_log_event_log(&_log_ev,
89 "Category '%s': inherited threshold = %s (=%d)",
91 gras_log_priority_names[cat->threshold], cat->threshold);
96 void _gras_log_event_log( gras_log_event_t* ev, const char *fmt, ...) {
97 gras_log_category_t* cat = ev->cat;
98 va_start(ev->ap, fmt);
100 gras_log_appender_t* appender = cat->appender;
101 if (appender != NULL) {
102 appender->do_append(appender, ev, fmt);
104 if (!cat->willLogToParent)
112 static void _cat_init(gras_log_category_t* category) {
113 if (category == &_GRAS_LOGV(GRAS_LOG_ROOT_CAT)) {
114 category->threshold = gras_log_priority_info;
115 category->appender = gras_log_default_appender;
117 gras_log_parent_set(category, category->parent);
119 _apply_control(category);
123 * This gets called the first time a category is referenced and performs the
125 * Also resets threshold to inherited!
127 int _gras_log_cat_init(gras_log_priority_t priority,
128 gras_log_category_t* category) {
132 return priority >= category->threshold;
135 void gras_log_parent_set(gras_log_category_t* cat,
136 gras_log_category_t* parent) {
138 gras_assert0(cat,"NULL category to be given a parent");
139 gras_assert1(parent,"The parent category of %s is NULL",cat->name);
141 /* unlink from current parent */
142 if (cat->threshold != gras_log_priority_uninitialized) {
143 gras_log_category_t** cpp = &parent->firstChild;
144 while(*cpp != cat && *cpp != NULL) {
145 cpp = &(*cpp)->nextSibling;
148 *cpp = cat->nextSibling;
152 cat->parent = parent;
153 cat->nextSibling = parent->firstChild;
154 parent->firstChild = cat;
156 /* Make sure parent is initialized */
157 if (parent->threshold == gras_log_priority_uninitialized) {
162 cat->threshold = parent->threshold;
163 cat->isThreshInherited = 1;
164 } /* log_setParent */
166 static void _set_inherited_thresholds(gras_log_category_t* cat) {
167 gras_log_category_t* child = cat->firstChild;
168 for( ; child != NULL; child = child->nextSibling) {
169 if (child->isThreshInherited) {
170 if (cat != &_GRAS_LOGV(log))
171 VERB3("Set category threshold of %s to %s (=%d)",
172 child->name,gras_log_priority_names[cat->threshold],cat->threshold);
173 child->threshold = cat->threshold;
174 _set_inherited_thresholds(child);
179 void gras_log_threshold_set(gras_log_category_t* cat,
180 gras_log_priority_t threshold) {
181 cat->threshold = threshold;
182 cat->isThreshInherited = 0;
183 _set_inherited_thresholds(cat);
186 static gras_error_t _gras_log_parse_setting(const char* control_string,
187 gras_log_setting_t *set) {
188 const char *name, *dot, *eq;
191 if (!*control_string)
193 DEBUG1("Parse log setting '%s'",control_string);
195 control_string += strspn(control_string, " ");
196 name = control_string;
197 control_string += strcspn(control_string, ".= ");
198 dot = control_string;
199 control_string += strcspn(control_string, "= ");
201 control_string += strcspn(control_string, " ");
203 gras_assert1(*dot == '.' && *eq == '=',
204 "Invalid control string '%s'",control_string);
206 if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
208 char *neweq=(char*)strdup(eq+1);
211 while (*(++p) != '\0') {
212 if (*p >= 'a' && *p <= 'z') {
217 DEBUG1("New priority name = %s",neweq);
218 for (i=0; i<gras_log_priority_infinite-1; i++) {
219 if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
220 DEBUG1("This is priority %d",i);
224 if (i<gras_log_priority_infinite-1) {
227 gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
232 snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
233 gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
235 if (!(set->catname=(char*)gras_malloc(dot - name+1)))
238 strncpy(set->catname,name,dot-name);
239 set->catname[dot-name]='\0'; /* Just in case */
240 DEBUG1("This is for cat '%s'", set->catname);
244 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
245 gras_error_t errcode;
246 gras_log_category_t *child;
248 if (!strcmp(cat->name,name)) {
252 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
253 errcode=_gras_log_cat_searchsub(child,name,whereto);
254 if (errcode==no_error)
257 return mismatch_error;
260 static void _cleanup_double_spaces(char *s) {
283 if (e ^ !isspace(*p))
293 * gras_log_control_set:
295 * @Returns: malloc_error or no_error
297 * Typically passed a command-line argument. The string has the syntax:
299 * ( [category] "." [keyword] "=" value (" ")... )...
301 * where [category] is one the category names and keyword is one of the
304 * thresh value is an integer priority level. Sets the category's
305 * threshold priority.
308 * This routine may only be called once and that must be before any other
309 * logging command! Typically, this is done from main().
311 gras_error_t gras_log_control_set(const char* control_string) {
312 gras_error_t errcode;
313 gras_log_setting_t *set;
318 DEBUG1("Parse log settings '%s'",control_string);
319 if (control_string == NULL)
321 if (gras_log_settings == NULL)
322 TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
325 if (! (set = gras_new(gras_log_setting_t,1)) )
328 if (!(cs=(char*)strdup(control_string)))
330 _cleanup_double_spaces(cs);
333 gras_log_category_t *cat;
343 errcode = _gras_log_parse_setting(p,set);
344 if (errcode != no_error) {
349 TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),
351 if (errcode == mismatch_error) {
352 DEBUG0("Store for further application");
353 DEBUG1("push %p to the settings",(void*)set);
354 TRY(gras_dynar_push(gras_log_settings,&set));
355 /* malloc in advance the next slot */
356 if (!(set = gras_new(gras_log_setting_t,1))) {
361 DEBUG0("Apply directly");
362 gras_free(set->catname);
363 gras_log_threshold_set(cat,set->thresh);
371 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
375 void gras_log_exit(void) {
376 VERB0("Exiting log");
377 gras_dynar_free(gras_log_settings);