3 /* log - a generic logging facility in the spirit of log4j */
5 /* Copyright (c) 2003, 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(xbt,XBT_LOG_ROOT_CAT,"All XBT categories (simgrid toolbox)");
53 XBT_LOG_NEW_SUBCATEGORY(surf,XBT_LOG_ROOT_CAT,"All SURF categories");
54 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log,xbt,"Loggings from the logging mecanism itself");
56 void xbt_log_init(int *argc,char **argv, const char *defaultlog) {
61 /** Set logs and init log submodule */
62 for (i=1; i<*argc; i++) {
63 if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
65 opt=strchr(argv[i],'=');
67 xbt_log_control_set(opt);
68 DEBUG1("Did apply '%s' as log setting",opt);
69 /*remove this from argv*/
70 for (j=i+1; j<*argc; j++) {
75 i--; /* compensate effect of next loop incrementation */
78 if (!found && defaultlog) {
79 xbt_log_control_set(defaultlog);
83 void xbt_log_exit(void) {
85 xbt_dynar_free(&xbt_log_settings);
89 static void _apply_control(xbt_log_category_t cat) {
91 xbt_log_setting_t setting=NULL;
94 if (!xbt_log_settings)
97 xbt_assert0(cat,"NULL category");
98 xbt_assert(cat->name);
100 xbt_dynar_foreach(xbt_log_settings,cursor,setting) {
101 xbt_assert0(setting,"Damnit, NULL cat in the list");
102 xbt_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
104 if (!strcmp(setting->catname,cat->name)) {
107 xbt_log_threshold_set(cat, setting->thresh);
108 xbt_dynar_cursor_rm(xbt_log_settings,&cursor);
110 if (cat->threshold <= xbt_log_priority_verbose) {
111 s_xbt_log_event_t _log_ev =
112 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
113 _xbt_log_event_log(&_log_ev,
114 "Apply settings for category '%s': set threshold to %s (=%d)",
116 xbt_log_priority_names[cat->threshold], cat->threshold);
120 if (!found && cat->threshold <= xbt_log_priority_verbose) {
121 s_xbt_log_event_t _log_ev =
122 {cat,xbt_log_priority_verbose,__FILE__,_XBT_GNUC_FUNCTION,__LINE__};
123 _xbt_log_event_log(&_log_ev,
124 "Category '%s': inherited threshold = %s (=%d)",
126 xbt_log_priority_names[cat->threshold], cat->threshold);
131 void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) {
132 xbt_log_category_t cat = ev->cat;
133 va_start(ev->ap, fmt);
135 xbt_log_appender_t appender = cat->appender;
136 if (appender != NULL) {
137 appender->do_append(appender, ev, fmt);
139 if (!cat->willLogToParent)
147 static void _cat_init(xbt_log_category_t category) {
148 if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
149 category->threshold = xbt_log_priority_info;
150 category->appender = xbt_log_default_appender;
152 xbt_log_parent_set(category, category->parent);
154 _apply_control(category);
158 * This gets called the first time a category is referenced and performs the
160 * Also resets threshold to inherited!
162 int _xbt_log_cat_init(e_xbt_log_priority_t priority,
163 xbt_log_category_t category) {
167 return priority >= category->threshold;
170 void xbt_log_parent_set(xbt_log_category_t cat,
171 xbt_log_category_t parent) {
173 xbt_assert0(cat,"NULL category to be given a parent");
174 xbt_assert1(parent,"The parent category of %s is NULL",cat->name);
176 /* unlink from current parent */
177 if (cat->threshold != xbt_log_priority_uninitialized) {
178 xbt_log_category_t* cpp = &parent->firstChild;
179 while(*cpp != cat && *cpp != NULL) {
180 cpp = &(*cpp)->nextSibling;
182 xbt_assert(*cpp == cat);
183 *cpp = cat->nextSibling;
187 cat->parent = parent;
188 cat->nextSibling = parent->firstChild;
189 parent->firstChild = cat;
191 /* Make sure parent is initialized */
192 if (parent->threshold == xbt_log_priority_uninitialized) {
197 cat->threshold = parent->threshold;
198 cat->isThreshInherited = 1;
199 } /* log_setParent */
201 static void _set_inherited_thresholds(xbt_log_category_t cat) {
202 xbt_log_category_t child = cat->firstChild;
203 for( ; child != NULL; child = child->nextSibling) {
204 if (child->isThreshInherited) {
205 if (cat != &_XBT_LOGV(log))
206 VERB3("Set category threshold of %s to %s (=%d)",
207 child->name,xbt_log_priority_names[cat->threshold],cat->threshold);
208 child->threshold = cat->threshold;
209 _set_inherited_thresholds(child);
214 void xbt_log_threshold_set(xbt_log_category_t cat,
215 e_xbt_log_priority_t threshold) {
216 cat->threshold = threshold;
217 cat->isThreshInherited = 0;
218 _set_inherited_thresholds(cat);
221 static void _xbt_log_parse_setting(const char* control_string,
222 xbt_log_setting_t set) {
223 const char *name, *dot, *eq;
226 if (!*control_string)
228 DEBUG1("Parse log setting '%s'",control_string);
230 control_string += strspn(control_string, " ");
231 name = control_string;
232 control_string += strcspn(control_string, ".= ");
233 dot = control_string;
234 control_string += strcspn(control_string, "= ");
236 control_string += strcspn(control_string, " ");
238 xbt_assert1(*dot == '.' && *eq == '=',
239 "Invalid control string '%s'",control_string);
241 if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
243 char *neweq=xbt_strdup(eq+1);
246 while (*(++p) != '\0') {
247 if (*p >= 'a' && *p <= 'z') {
252 DEBUG1("New priority name = %s",neweq);
253 for (i=0; i<xbt_log_priority_infinite-1; i++) {
254 if (!strncmp(xbt_log_priority_names[i],neweq,p-eq)) {
255 DEBUG1("This is priority %d",i);
259 if (i<xbt_log_priority_infinite-1) {
262 xbt_assert1(FALSE,"Unknown priority name: %s",eq+1);
267 snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
268 xbt_assert1(FALSE,"Unknown setting of the log category: %s",buff);
270 set->catname=(char*)xbt_malloc(dot - name+1);
272 strncpy(set->catname,name,dot-name);
273 set->catname[dot-name]='\0'; /* Just in case */
274 DEBUG1("This is for cat '%s'", set->catname);
277 static xbt_error_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *name,
278 /*OUT*/xbt_log_category_t*whereto) {
280 xbt_log_category_t child;
282 if (!strcmp(cat->name,name)) {
286 for(child=cat->firstChild ; child != NULL; child = child->nextSibling) {
287 errcode=_xbt_log_cat_searchsub(child,name,whereto);
288 if (errcode==no_error)
291 return mismatch_error;
294 static void _cleanup_double_spaces(char *s) {
317 if (e ^ !isspace(*p))
328 * \param control_string What to parse
330 * Typically passed a command-line argument. The string has the syntax:
332 * ( [category] "." [keyword] "=" value (" ")... )...
334 * where [category] is one the category names and keyword is one of the
337 * thresh value is an integer priority level. Sets the category's
338 * threshold priority.
341 * This routine may only be called once and that must be before any other
342 * logging command! Typically, this is done from main().
344 void xbt_log_control_set(const char* control_string) {
346 xbt_log_setting_t set;
351 DEBUG1("Parse log settings '%s'",control_string);
352 if (control_string == NULL)
354 if (xbt_log_settings == NULL)
355 xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t),
358 set = xbt_new(s_xbt_log_setting_t,1);
359 cs=xbt_strdup(control_string);
361 _cleanup_double_spaces(cs);
364 xbt_log_category_t cat;
374 _xbt_log_parse_setting(p,set);
376 errcode = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname,&cat);
377 if (errcode == mismatch_error) {
378 DEBUG0("Store for further application");
379 DEBUG1("push %p to the settings",(void*)set);
380 xbt_dynar_push(xbt_log_settings,&set);
381 /* malloc in advance the next slot */
382 set = xbt_new(s_xbt_log_setting_t,1);
384 DEBUG0("Apply directly");
385 xbt_free(set->catname);
386 xbt_log_threshold_set(cat,set->thresh);
393 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) {