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");
57 void xbt_log_init(int *argc,char **argv, const char *defaultlog) {
62 /** Set logs and init log submodule */
63 for (i=1; i<*argc; i++) {
64 if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
66 opt=strchr(argv[i],'=');
68 xbt_log_control_set(opt);
69 DEBUG1("Did apply '%s' as log setting",opt);
70 /*remove this from argv*/
71 for (j=i+1; j<*argc; j++) {
76 i--; /* compensate effect of next loop incrementation */
79 if (!found && defaultlog) {
80 xbt_log_control_set(defaultlog);
84 void xbt_log_exit(void) {
86 xbt_dynar_free(&xbt_log_settings);
90 static void _apply_control(xbt_log_category_t cat) {
92 xbt_log_setting_t setting=NULL;
95 if (!xbt_log_settings)
98 xbt_assert0(cat,"NULL category");
99 xbt_assert(cat->name);
101 xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
102 xbt_assert0(setting,"Damnit, NULL cat in the list");
103 xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
105 if (!strcmp(setting->catname,cat->name)) {
108 xbt_log_threshold_set(cat, setting->thresh);
109 xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
111 if (cat->threshold <= xbt_log_priority_verbose) {
112 s_xbt_log_event_t _log_ev =
113 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
114 _xbt_log_event_log(&_log_ev,
115 "Apply settings for category '%s': set threshold to %s (=%d)",
117 xbt_log_priority_names[cat->threshold], cat->threshold);
121 if (!found && cat->threshold <= xbt_log_priority_verbose) {
122 s_xbt_log_event_t _log_ev =
123 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
124 _xbt_log_event_log(&_log_ev,
125 "Category '%s': inherited threshold = %s (=%d)",
127 xbt_log_priority_names[cat->threshold], cat->threshold);
132 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
133 xbt_log_category_t cat = ev->cat;
134 va_start(ev->ap, fmt);
136 xbt_log_appender_t appender = cat->appender;
137 if (appender != NULL) {
138 appender->do_append(appender, ev, fmt);
140 if (!cat->willLogToParent)
148 static void _cat_init(xbt_log_category_t category) {
149 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
150 category->threshold = xbt_log_priority_info;
151 category->appender = xbt_log_default_appender;
153 xbt_log_parent_set(category, category->parent);
155 _apply_control(category);
159 * This gets called the first time a category is referenced and performs the
161 * Also resets threshold to inherited!
163 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
164 xbt_log_category_t category) {
168 return priority >= category->threshold;
171 void xbt_log_parent_set(xbt_log_category_t cat,
172 xbt_log_category_t parent) {
174 xbt_assert0(cat,"NULL category to be given a parent");
175 xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
177 /* unlink from current parent */
178 if (cat->threshold != xbt_log_priority_uninitialized) {
179 xbt_log_category_t* cpp = &parent->firstChild;
180 while(*cpp != cat && *cpp != NULL) {
181 cpp = &(*cpp)->nextSibling;
183 xbt_assert(*cpp == cat);
184 *cpp = cat->nextSibling;
188 cat->parent = parent;
189 cat->nextSibling = parent->firstChild;
190 parent->firstChild = cat;
192 /* Make sure parent is initialized */
193 if (parent->threshold == xbt_log_priority_uninitialized) {
198 cat->threshold = parent->threshold;
199 cat->isThreshInherited = 1;
200 } /* log_setParent */
202 static void _set_inherited_thresholds(xbt_log_category_t cat) {
203 xbt_log_category_t child = cat->firstChild;
204 for( ; child != NULL; child = child->nextSibling) {
205 if (child->isThreshInherited) {
206 if (cat != &_XBT_LOGV(log))
207 VERB3("Set category threshold of %s to %s (=%d)",
208 child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
209 child->threshold = cat->threshold;
210 _set_inherited_thresholds(child);
215 void xbt_log_threshold_set(xbt_log_category_t cat,
216 e_xbt_log_priority_t threshold) {
217 cat->threshold = threshold;
218 cat->isThreshInherited = 0;
219 _set_inherited_thresholds(cat);
222 static void _xbt_log_parse_setting(const char* control_string,
223 xbt_log_setting_t set) {
224 const char *name, *dot, *eq;
227 if (!*control_string)
229 DEBUG1("Parse log setting '%s'",control_string);
231 control_string += strspn(control_string, " ");
232 name = control_string;
233 control_string += strcspn(control_string, ".= ");
234 dot = control_string;
235 control_string += strcspn(control_string, "= ");
237 control_string += strcspn(control_string, " ");
239 xbt_assert1(*dot == '.' && *eq == '=',
240 "Invalid control string '%s'",control_string);
242 if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
244 char *neweq=xbt_strdup(eq+1);
247 while (*(++p) != '\0') {
248 if (*p >= 'a' && *p <= 'z') {
253 DEBUG1("New priority name = %s",neweq);
254 for (i=0; i<xbt_log_priority_infinite-1; i++) {
255 if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
256 DEBUG1("This is priority %d",i);
260 if (i<xbt_log_priority_infinite-1) {
263 xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
268 snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
269 xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
271 set->catname=(char*)xbt_malloc(dot - name+1);
273 strncpy(set->catname,name,dot-name);
274 set->catname[dot-name]='\0'; /* Just in case */
275 DEBUG1("This is for cat '%s'", set->catname);
278 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
279 /*OUT*/xbt_log_category_t*whereto) {
281 xbt_log_category_t child;
283 if (!strcmp(cat->name,name)) {
287 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
288 errcode=_xbt_log_cat_searchsub(child,name,whereto);
289 if (errcode==no_error)
292 return mismatch_error;
295 static void _cleanup_double_spaces(char *s) {
318 if (e ^ !isspace(*p))
328 * xbt_log_control_set:
331 * Typically passed a command-line argument. The string has the syntax:
333 * ( [category] "." [keyword] "=" value (" ")... )...
335 * where [category] is one the category names and keyword is one of the
338 * thresh value is an integer priority level. Sets the category's
339 * threshold priority.
342 * This routine may only be called once and that must be before any other
343 * logging command! Typically, this is done from main().
345 void xbt_log_control_set(const char* control_string) {
347 xbt_log_setting_t set;
352 DEBUG1("Parse log settings '%s'",control_string);
353 if (control_string == NULL)
355 if (xbt_log_settings == NULL)
356 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
359 set = xbt_new(s_xbt_log_setting_t,1);
360 cs=xbt_strdup(control_string);
362 _cleanup_double_spaces(cs);
365 xbt_log_category_t cat;
375 _xbt_log_parse_setting(p,set);
377 errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
378 if (errcode == mismatch_error) {
379 DEBUG0("Store for further application");
380 DEBUG1("push %p to the settings",(void*)set);
381 xbt_dynar_push(xbt_log_settings,&set);
382 /* malloc in advance the next slot */
383 set = xbt_new(s_xbt_log_setting_t,1);
385 DEBUG0("Apply directly");
386 xbt_free(set->catname);
387 xbt_log_threshold_set(cat,set->thresh);
394 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {