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. */
14 #include "xbt_modinter.h"
17 #include "xbt/sysdep.h"
19 #include "xbt/error.h"
20 #include "xbt/dynar.h"
24 e_xbt_log_priority_t thresh;
25 } s_xbt_log_setting_t,*xbt_log_setting_t;
27 static xbt_dynar_t xbt_log_settings=NULL;
28 static void _free_setting(void *s) {
29 xbt_log_setting_t set=(xbt_log_setting_t)s;
31 xbt_free(set->catname);
32 /* xbt_free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
36 const char *xbt_log_priority_names[8] = {
47 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
49 "root", xbt_log_priority_uninitialized, 0,
53 XBT_LOG_NEW_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories");
54 XBT_LOG_NEW_SUBCATEGORY(xbt,XBT_LOG_ROOT_CAT,"All XBT categories (gras toolbox)");
55 XBT_LOG_NEW_SUBCATEGORY(surf,XBT_LOG_ROOT_CAT,"All SURF categories");
56 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
59 static void _apply_control(xbt_log_category_t cat) {
61 xbt_log_setting_t setting=NULL;
64 if (!xbt_log_settings)
67 xbt_assert0(cat,"NULL category");
68 xbt_assert(cat->name);
70 xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
71 xbt_assert0(setting,"Damnit, NULL cat in the list");
72 xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
74 if (!strcmp(setting->catname,cat->name)) {
77 xbt_log_threshold_set(cat, setting->thresh);
78 xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
80 if (cat->threshold <= xbt_log_priority_verbose) {
81 s_xbt_log_event_t _log_ev =
82 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
83 _xbt_log_event_log(&_log_ev,
84 "Apply settings for category '%s': set threshold to %s (=%d)",
86 xbt_log_priority_names[cat->threshold], cat->threshold);
90 if (!found && cat->threshold <= xbt_log_priority_verbose) {
91 s_xbt_log_event_t _log_ev =
92 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
93 _xbt_log_event_log(&_log_ev,
94 "Category '%s': inherited threshold = %s (=%d)",
96 xbt_log_priority_names[cat->threshold], cat->threshold);
101 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
102 xbt_log_category_t cat = ev->cat;
103 va_start(ev->ap, fmt);
105 xbt_log_appender_t appender = cat->appender;
106 if (appender != NULL) {
107 appender->do_append(appender, ev, fmt);
109 if (!cat->willLogToParent)
117 static void _cat_init(xbt_log_category_t category) {
118 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
119 category->threshold = xbt_log_priority_info;
120 category->appender = xbt_log_default_appender;
122 xbt_log_parent_set(category, category->parent);
124 _apply_control(category);
128 * This gets called the first time a category is referenced and performs the
130 * Also resets threshold to inherited!
132 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
133 xbt_log_category_t category) {
137 return priority >= category->threshold;
140 void xbt_log_parent_set(xbt_log_category_t cat,
141 xbt_log_category_t parent) {
143 xbt_assert0(cat,"NULL category to be given a parent");
144 xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
146 /* unlink from current parent */
147 if (cat->threshold != xbt_log_priority_uninitialized) {
148 xbt_log_category_t* cpp = &parent->firstChild;
149 while(*cpp != cat && *cpp != NULL) {
150 cpp = &(*cpp)->nextSibling;
152 xbt_assert(*cpp == cat);
153 *cpp = cat->nextSibling;
157 cat->parent = parent;
158 cat->nextSibling = parent->firstChild;
159 parent->firstChild = cat;
161 /* Make sure parent is initialized */
162 if (parent->threshold == xbt_log_priority_uninitialized) {
167 cat->threshold = parent->threshold;
168 cat->isThreshInherited = 1;
169 } /* log_setParent */
171 static void _set_inherited_thresholds(xbt_log_category_t cat) {
172 xbt_log_category_t child = cat->firstChild;
173 for( ; child != NULL; child = child->nextSibling) {
174 if (child->isThreshInherited) {
175 if (cat != &_XBT_LOGV(log))
176 VERB3("Set category threshold of %s to %s (=%d)",
177 child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
178 child->threshold = cat->threshold;
179 _set_inherited_thresholds(child);
184 void xbt_log_threshold_set(xbt_log_category_t cat,
185 e_xbt_log_priority_t threshold) {
186 cat->threshold = threshold;
187 cat->isThreshInherited = 0;
188 _set_inherited_thresholds(cat);
191 static void _xbt_log_parse_setting(const char* control_string,
192 xbt_log_setting_t set) {
193 const char *name, *dot, *eq;
196 if (!*control_string)
198 DEBUG1("Parse log setting '%s'",control_string);
200 control_string += strspn(control_string, " ");
201 name = control_string;
202 control_string += strcspn(control_string, ".= ");
203 dot = control_string;
204 control_string += strcspn(control_string, "= ");
206 control_string += strcspn(control_string, " ");
208 xbt_assert1(*dot == '.' && *eq == '=',
209 "Invalid control string '%s'",control_string);
211 if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
213 char *neweq=xbt_strdup(eq+1);
216 while (*(++p) != '\0') {
217 if (*p >= 'a' && *p <= 'z') {
222 DEBUG1("New priority name = %s",neweq);
223 for (i=0; i<xbt_log_priority_infinite-1; i++) {
224 if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
225 DEBUG1("This is priority %d",i);
229 if (i<xbt_log_priority_infinite-1) {
232 xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
237 snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
238 xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
240 set->catname=(char*)xbt_malloc(dot - name+1);
242 strncpy(set->catname,name,dot-name);
243 set->catname[dot-name]='\0'; /* Just in case */
244 DEBUG1("This is for cat '%s'", set->catname);
247 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
248 /*OUT*/xbt_log_category_t*whereto) {
250 xbt_log_category_t child;
252 if (!strcmp(cat->name,name)) {
256 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
257 errcode=_xbt_log_cat_searchsub(child,name,whereto);
258 if (errcode==no_error)
261 return mismatch_error;
264 static void _cleanup_double_spaces(char *s) {
287 if (e ^ !isspace(*p))
297 * xbt_log_control_set:
300 * Typically passed a command-line argument. The string has the syntax:
302 * ( [category] "." [keyword] "=" value (" ")... )...
304 * where [category] is one the category names and keyword is one of the
307 * thresh value is an integer priority level. Sets the category's
308 * threshold priority.
311 * This routine may only be called once and that must be before any other
312 * logging command! Typically, this is done from main().
314 void xbt_log_control_set(const char* control_string) {
316 xbt_log_setting_t set;
321 DEBUG1("Parse log settings '%s'",control_string);
322 if (control_string == NULL)
324 if (xbt_log_settings == NULL)
325 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
328 set = xbt_new(s_xbt_log_setting_t,1);
329 cs=xbt_strdup(control_string);
331 _cleanup_double_spaces(cs);
334 xbt_log_category_t cat;
344 _xbt_log_parse_setting(p,set);
346 errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
347 if (errcode == mismatch_error) {
348 DEBUG0("Store for further application");
349 DEBUG1("push %p to the settings",(void*)set);
350 xbt_dynar_push(xbt_log_settings,&set);
351 /* malloc in advance the next slot */
352 set = xbt_new(s_xbt_log_setting_t,1);
354 DEBUG0("Apply directly");
355 xbt_free(set->catname);
356 xbt_log_threshold_set(cat,set->thresh);
363 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {
367 void xbt_log_exit(void) {
368 VERB0("Exiting log");
369 xbt_dynar_free(&xbt_log_settings);