3 /* log - a generic logging facility in the spirit of log4j */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "xbt_modinter.h"
16 #include "xbt/sysdep.h"
18 #include "xbt/error.h"
19 #include "xbt/dynar.h"
23 e_xbt_log_priority_t thresh;
24 } s_xbt_log_setting_t,*xbt_log_setting_t;
26 static xbt_dynar_t xbt_log_settings=NULL;
27 static void _free_setting(void *s) {
28 xbt_log_setting_t set=(xbt_log_setting_t)s;
30 xbt_free(set->catname);
31 /* xbt_free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
35 const char *xbt_log_priority_names[8] = {
46 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
48 "root", xbt_log_priority_uninitialized, 0,
52 XBT_LOG_NEW_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories");
53 XBT_LOG_NEW_SUBCATEGORY(xbt,XBT_LOG_ROOT_CAT,"All XBT categories (gras toolbox)");
54 XBT_LOG_NEW_SUBCATEGORY(surf,XBT_LOG_ROOT_CAT,"All SURF categories");
55 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
58 static void _apply_control(xbt_log_category_t cat) {
60 xbt_log_setting_t setting=NULL;
63 if (!xbt_log_settings)
66 xbt_assert0(cat,"NULL category");
67 xbt_assert(cat->name);
69 xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
70 xbt_assert0(setting,"Damnit, NULL cat in the list");
71 xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
73 if (!strcmp(setting->catname,cat->name)) {
76 xbt_log_threshold_set(cat, setting->thresh);
77 xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
79 if (cat->threshold <= xbt_log_priority_verbose) {
80 s_xbt_log_event_t _log_ev =
81 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
82 _xbt_log_event_log(&_log_ev,
83 "Apply settings for category '%s': set threshold to %s (=%d)",
85 xbt_log_priority_names[cat->threshold], cat->threshold);
89 if (!found && cat->threshold <= xbt_log_priority_verbose) {
90 s_xbt_log_event_t _log_ev =
91 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
92 _xbt_log_event_log(&_log_ev,
93 "Category '%s': inherited threshold = %s (=%d)",
95 xbt_log_priority_names[cat->threshold], cat->threshold);
100 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
101 xbt_log_category_t cat = ev->cat;
102 va_start(ev->ap, fmt);
104 xbt_log_appender_t appender = cat->appender;
105 if (appender != NULL) {
106 appender->do_append(appender, ev, fmt);
108 if (!cat->willLogToParent)
116 static void _cat_init(xbt_log_category_t category) {
117 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
118 category->threshold = xbt_log_priority_info;
119 category->appender = xbt_log_default_appender;
121 xbt_log_parent_set(category, category->parent);
123 _apply_control(category);
127 * This gets called the first time a category is referenced and performs the
129 * Also resets threshold to inherited!
131 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
132 xbt_log_category_t category) {
136 return priority >= category->threshold;
139 void xbt_log_parent_set(xbt_log_category_t cat,
140 xbt_log_category_t parent) {
142 xbt_assert0(cat,"NULL category to be given a parent");
143 xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
145 /* unlink from current parent */
146 if (cat->threshold != xbt_log_priority_uninitialized) {
147 xbt_log_category_t* cpp = &parent->firstChild;
148 while(*cpp != cat && *cpp != NULL) {
149 cpp = &(*cpp)->nextSibling;
151 xbt_assert(*cpp == cat);
152 *cpp = cat->nextSibling;
156 cat->parent = parent;
157 cat->nextSibling = parent->firstChild;
158 parent->firstChild = cat;
160 /* Make sure parent is initialized */
161 if (parent->threshold == xbt_log_priority_uninitialized) {
166 cat->threshold = parent->threshold;
167 cat->isThreshInherited = 1;
168 } /* log_setParent */
170 static void _set_inherited_thresholds(xbt_log_category_t cat) {
171 xbt_log_category_t child = cat->firstChild;
172 for( ; child != NULL; child = child->nextSibling) {
173 if (child->isThreshInherited) {
174 if (cat != &_XBT_LOGV(log))
175 VERB3("Set category threshold of %s to %s (=%d)",
176 child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
177 child->threshold = cat->threshold;
178 _set_inherited_thresholds(child);
183 void xbt_log_threshold_set(xbt_log_category_t cat,
184 e_xbt_log_priority_t threshold) {
185 cat->threshold = threshold;
186 cat->isThreshInherited = 0;
187 _set_inherited_thresholds(cat);
190 static void _xbt_log_parse_setting(const char* control_string,
191 xbt_log_setting_t set) {
192 const char *name, *dot, *eq;
195 if (!*control_string)
197 DEBUG1("Parse log setting '%s'",control_string);
199 control_string += strspn(control_string, " ");
200 name = control_string;
201 control_string += strcspn(control_string, ".= ");
202 dot = control_string;
203 control_string += strcspn(control_string, "= ");
205 control_string += strcspn(control_string, " ");
207 xbt_assert1(*dot == '.' && *eq == '=',
208 "Invalid control string '%s'",control_string);
210 if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
212 char *neweq=xbt_strdup(eq+1);
215 while (*(++p) != '\0') {
216 if (*p >= 'a' && *p <= 'z') {
221 DEBUG1("New priority name = %s",neweq);
222 for (i=0; i<xbt_log_priority_infinite-1; i++) {
223 if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
224 DEBUG1("This is priority %d",i);
228 if (i<xbt_log_priority_infinite-1) {
231 xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
236 snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
237 xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
239 set->catname=(char*)xbt_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);
246 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
247 /*OUT*/xbt_log_category_t*whereto) {
249 xbt_log_category_t child;
251 if (!strcmp(cat->name,name)) {
255 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
256 errcode=_xbt_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 * xbt_log_control_set:
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 void xbt_log_control_set(const char* control_string) {
315 xbt_log_setting_t set;
320 DEBUG1("Parse log settings '%s'",control_string);
321 if (control_string == NULL)
323 if (xbt_log_settings == NULL)
324 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
327 set = xbt_new(s_xbt_log_setting_t,1);
328 cs=xbt_strdup(control_string);
330 _cleanup_double_spaces(cs);
333 xbt_log_category_t cat;
343 _xbt_log_parse_setting(p,set);
345 errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
346 if (errcode == mismatch_error) {
347 DEBUG0("Store for further application");
348 DEBUG1("push %p to the settings",(void*)set);
349 xbt_dynar_push(xbt_log_settings,&set);
350 /* malloc in advance the next slot */
351 set = xbt_new(s_xbt_log_setting_t,1);
353 DEBUG0("Apply directly");
354 xbt_free(set->catname);
355 xbt_log_threshold_set(cat,set->thresh);
362 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
366 void xbt_log_exit(void) {
367 VERB0("Exiting log");
368 xbt_dynar_free(&xbt_log_settings);