Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / src / xbt / log.c
1 /* log - a generic logging facility in the spirit of log4j                  */
2
3 /* Copyright (c) 2004-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdarg.h>
10 #include <ctype.h>
11 #include <stdio.h>              /* snprintf */
12 #include <stdlib.h>             /* snprintf */
13
14 #include "src/internal_config.h"
15
16 #include "src/xbt_modinter.h"
17
18 #include "src/xbt/log_private.h"
19 #include "xbt/asserts.h"
20 #include "xbt/dynar.h"
21 #include "xbt/ex.h"
22 #include "xbt/misc.h"
23 #include "xbt/str.h"
24 #include "xbt/sysdep.h"
25 #include "xbt/xbt_os_thread.h"
26
27 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
28 static xbt_os_mutex_t log_cat_init_mutex = NULL;
29
30 /** \addtogroup XBT_log
31  *
32  *  For more information, please refer to @ref outcomes_logs Section.
33  */
34
35 xbt_log_appender_t xbt_log_default_appender = NULL;     /* set in log_init */
36 xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
37
38 typedef struct {
39   char *catname;
40   char *fmt;
41   e_xbt_log_priority_t thresh;
42   int additivity;
43   xbt_log_appender_t appender;
44 } s_xbt_log_setting_t;
45
46 typedef s_xbt_log_setting_t* xbt_log_setting_t;
47
48 static xbt_dynar_t xbt_log_settings = NULL;
49
50 static void _free_setting(void *s)
51 {
52   xbt_log_setting_t set = *(xbt_log_setting_t *) s;
53   if (set) {
54     free(set->catname);
55     free(set->fmt);
56     free(set);
57   }
58 }
59
60 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting);
61
62 const char *xbt_log_priority_names[8] = {
63   "NONE",
64   "TRACE",
65   "DEBUG",
66   "VERBOSE",
67   "INFO",
68   "WARNING",
69   "ERROR",
70   "CRITICAL"
71 };
72
73 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
74   NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
75       "root", "The common ancestor for all categories",
76       0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
77       0 /* isThreshInherited */ ,
78       NULL /* appender */ , NULL /* layout */ ,
79       0                         /* additivity */
80 };
81
82 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
83
84 /* create the default appender and install it in the root category,
85    which were already created (damnit. Too slow little beetle) */
86 void xbt_log_preinit(void)
87 {
88   xbt_log_default_appender = xbt_log_appender_file_new(NULL);
89   xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
90   _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
91   _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
92   log_cat_init_mutex = xbt_os_mutex_init();
93 }
94
95 static void xbt_log_connect_categories(void)
96 {
97   /* Connect our log channels: that must be done manually under windows */
98   /* Also permit that they are correctly listed by xbt_log_help_categories() */
99
100   /* xbt */
101   XBT_LOG_CONNECT(xbt);
102   XBT_LOG_CONNECT(log);
103   XBT_LOG_CONNECT(module);
104   XBT_LOG_CONNECT(replay);
105   XBT_LOG_CONNECT(xbt_cfg);
106   XBT_LOG_CONNECT(xbt_dict);
107   XBT_LOG_CONNECT(xbt_dict_cursor);
108   XBT_LOG_CONNECT(xbt_dict_elm);
109   XBT_LOG_CONNECT(xbt_dyn);
110   XBT_LOG_CONNECT(xbt_ex);
111   XBT_LOG_CONNECT(xbt_backtrace);
112   XBT_LOG_CONNECT(xbt_exception);
113   XBT_LOG_CONNECT(xbt_graph);
114   XBT_LOG_CONNECT(xbt_heap);
115   XBT_LOG_CONNECT(xbt_lib);
116   XBT_LOG_CONNECT(xbt_mallocator);
117   XBT_LOG_CONNECT(xbt_matrix);
118   XBT_LOG_CONNECT(xbt_memory_map);
119   XBT_LOG_CONNECT(xbt_parmap);
120   XBT_LOG_CONNECT(xbt_sync);
121   XBT_LOG_CONNECT(xbt_sync_os);
122
123 #ifdef simgrid_EXPORTS
124   /* The following categories are only defined in libsimgrid */
125
126   /* bindings */
127 #if SIMGRID_HAVE_LUA
128   XBT_LOG_CONNECT(lua);
129   XBT_LOG_CONNECT(lua_host);
130   XBT_LOG_CONNECT(lua_platf);
131   XBT_LOG_CONNECT(lua_debug);
132 #endif
133
134   /* instr */
135   XBT_LOG_CONNECT(instr);
136   XBT_LOG_CONNECT(instr_api);
137   XBT_LOG_CONNECT(instr_config);
138   XBT_LOG_CONNECT(instr_msg);
139   XBT_LOG_CONNECT(instr_msg_process);
140   XBT_LOG_CONNECT(instr_paje_containers);
141   XBT_LOG_CONNECT(instr_paje_header);
142   XBT_LOG_CONNECT(instr_paje_trace);
143   XBT_LOG_CONNECT(instr_paje_types);
144   XBT_LOG_CONNECT(instr_paje_values);
145   XBT_LOG_CONNECT(instr_resource);
146   XBT_LOG_CONNECT(instr_routing);
147   XBT_LOG_CONNECT(instr_surf);
148
149   /* jedule */
150 #if SIMGRID_HAVE_JEDULE
151   XBT_LOG_CONNECT(jedule);
152   XBT_LOG_CONNECT(jed_sd);
153 #endif
154
155   /* mc */
156 #if SIMGRID_HAVE_MC
157   XBT_LOG_CONNECT(mc);
158   XBT_LOG_CONNECT(mc_checkpoint);
159   XBT_LOG_CONNECT(mc_comm_determinism);
160   XBT_LOG_CONNECT(mc_compare);
161   XBT_LOG_CONNECT(mc_dwarf);
162   XBT_LOG_CONNECT(mc_hash);
163   XBT_LOG_CONNECT(mc_liveness);
164   XBT_LOG_CONNECT(mc_memory);
165   XBT_LOG_CONNECT(mc_page_snapshot);
166   XBT_LOG_CONNECT(mc_request);
167   XBT_LOG_CONNECT(mc_safety);
168   XBT_LOG_CONNECT(mc_VisitedState);
169   XBT_LOG_CONNECT(mc_client);
170   XBT_LOG_CONNECT(mc_client_api);
171   XBT_LOG_CONNECT(mc_comm_pattern);
172   XBT_LOG_CONNECT(mc_process);
173   XBT_LOG_CONNECT(mc_protocol);
174   XBT_LOG_CONNECT(mc_Channel);
175   XBT_LOG_CONNECT(mc_ModelChecker);
176   XBT_LOG_CONNECT(mc_RegionSnaphot);
177   XBT_LOG_CONNECT(mc_Session);
178   XBT_LOG_CONNECT(mc_state);
179 #endif
180   XBT_LOG_CONNECT(mc_global);
181   XBT_LOG_CONNECT(mc_config);
182   XBT_LOG_CONNECT(mc_record);
183
184   /* msg */
185   XBT_LOG_CONNECT(msg);
186   XBT_LOG_CONNECT(msg_action);
187   XBT_LOG_CONNECT(msg_gos);
188   XBT_LOG_CONNECT(msg_io);
189   XBT_LOG_CONNECT(msg_kernel);
190   XBT_LOG_CONNECT(msg_mailbox);
191   XBT_LOG_CONNECT(msg_process);
192   XBT_LOG_CONNECT(msg_synchro);
193   XBT_LOG_CONNECT(msg_task);
194   XBT_LOG_CONNECT(msg_vm);
195
196   /* s4u */
197   XBT_LOG_CONNECT(s4u);
198   XBT_LOG_CONNECT(s4u_activity);
199   XBT_LOG_CONNECT(s4u_actor);
200   XBT_LOG_CONNECT(s4u_netzone);
201   XBT_LOG_CONNECT(s4u_channel);
202   XBT_LOG_CONNECT(s4u_comm);
203   XBT_LOG_CONNECT(s4u_file);
204    
205   /* sg */
206   XBT_LOG_CONNECT(sg_host);
207
208   /* simdag */
209   XBT_LOG_CONNECT(sd);
210   XBT_LOG_CONNECT(sd_daxparse);
211 #if HAVE_GRAPHVIZ
212   XBT_LOG_CONNECT(sd_dotparse);
213 #endif
214   XBT_LOG_CONNECT(sd_kernel);
215   XBT_LOG_CONNECT(sd_task);
216
217   /* simix */
218   XBT_LOG_CONNECT(simix);
219   XBT_LOG_CONNECT(simix_context);
220   XBT_LOG_CONNECT(simix_deployment);
221   XBT_LOG_CONNECT(simix_environment);
222   XBT_LOG_CONNECT(simix_host);
223   XBT_LOG_CONNECT(simix_io);
224   XBT_LOG_CONNECT(simix_kernel);
225   XBT_LOG_CONNECT(simix_network);
226   XBT_LOG_CONNECT(simix_process);
227   XBT_LOG_CONNECT(simix_popping);
228   XBT_LOG_CONNECT(simix_synchro);
229
230   /* smpi */
231   /* SMPI categories are connected in smpi_global.c */
232
233   /* surf */
234   XBT_LOG_CONNECT(surf);
235   XBT_LOG_CONNECT(surf_config);
236   XBT_LOG_CONNECT(surf_cpu);
237   XBT_LOG_CONNECT(surf_cpu_cas);
238   XBT_LOG_CONNECT(surf_cpu_ti);
239   XBT_LOG_CONNECT(surf_energy);
240   XBT_LOG_CONNECT(surf_kernel);
241   XBT_LOG_CONNECT(surf_lagrange);
242   XBT_LOG_CONNECT(surf_lagrange_dichotomy);
243   XBT_LOG_CONNECT(surf_maxmin);
244   XBT_LOG_CONNECT(surf_network);
245 #if SIMGRID_HAVE_NS3
246   XBT_LOG_CONNECT(ns3);
247 #endif
248   XBT_LOG_CONNECT(surf_parse);
249   XBT_LOG_CONNECT(surf_route);
250   XBT_LOG_CONNECT(surf_routing_generic);
251   XBT_LOG_CONNECT(surf_route_cluster);
252   XBT_LOG_CONNECT(surf_route_cluster_torus);
253   XBT_LOG_CONNECT(surf_route_cluster_dragonfly);
254   XBT_LOG_CONNECT(surf_route_dijkstra);
255   XBT_LOG_CONNECT(surf_route_fat_tree);
256   XBT_LOG_CONNECT(surf_route_floyd);
257   XBT_LOG_CONNECT(surf_route_full);
258   XBT_LOG_CONNECT(surf_route_none);
259   XBT_LOG_CONNECT(surf_route_vivaldi);
260   XBT_LOG_CONNECT(surf_storage);
261   XBT_LOG_CONNECT(surf_trace);
262   XBT_LOG_CONNECT(surf_vm);
263   XBT_LOG_CONNECT(surf_host);
264    
265 #endif /* simgrid_EXPORTS */
266 }
267
268 static void xbt_log_help(void);
269 static void xbt_log_help_categories(void);
270
271 /** @brief Get all logging settings from the command line
272  *
273  * xbt_log_control_set() is called on each string we got from cmd line
274  */
275 void xbt_log_init(int *argc, char **argv)
276 {
277   unsigned help_requested = 0;  /* 1: logs; 2: categories */
278   int i;
279   int j;
280   char *opt;
281
282   /* uncomment to set the LOG category to debug directly */
283   //    _XBT_LOGV(log).threshold = xbt_log_priority_debug;
284
285   xbt_log_connect_categories();
286
287   /* Set logs and init log submodule */
288   for (j = i = 1; i < *argc; i++) {
289     if (!strncmp(argv[i], "--log=", strlen("--log="))) {
290       opt = strchr(argv[i], '=');
291       opt++;
292       xbt_log_control_set(opt);
293       XBT_DEBUG("Did apply '%s' as log setting", opt);
294     } else if (!strcmp(argv[i], "--help-logs")) {
295       help_requested |= 1U;
296     } else if (!strcmp(argv[i], "--help-log-categories")) {
297       help_requested |= 2U;
298     } else {
299       argv[j++] = argv[i];
300     }
301   }
302   if (j < *argc) {
303     argv[j] = NULL;
304     *argc = j;
305   }
306
307   if (help_requested) {
308     if (help_requested & 1)
309       xbt_log_help();
310     if (help_requested & 2)
311       xbt_log_help_categories();
312     exit(0);
313   }
314 }
315
316 static void log_cat_exit(xbt_log_category_t cat)
317 {
318   xbt_log_category_t child;
319
320   if (cat->appender) {
321     if (cat->appender->free_)
322       cat->appender->free_(cat->appender);
323     free(cat->appender);
324   }
325   if (cat->layout) {
326     if (cat->layout->free_)
327       cat->layout->free_(cat->layout);
328     free(cat->layout);
329   }
330
331   for (child = cat->firstChild; child != NULL; child = child->nextSibling)
332     log_cat_exit(child);
333 }
334
335 void xbt_log_postexit(void)
336 {
337   XBT_VERB("Exiting log");
338   xbt_os_mutex_destroy(log_cat_init_mutex);
339   xbt_dynar_free(&xbt_log_settings);
340   log_cat_exit(&_XBT_LOGV(XBT_LOG_ROOT_CAT));
341 }
342
343  /* Size of the static string in which we  build the log string */
344 #define XBT_LOG_STATIC_BUFFER_SIZE 2048
345 /* Minimum size of the dynamic string in which we build the log string
346    (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
347 #define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
348
349 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
350 {
351   xbt_log_category_t cat = ev->cat;
352
353   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
354   xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value",
355              ev->priority);
356
357   do {
358     xbt_log_appender_t appender = cat->appender;
359
360     if (!appender)
361       continue;                 /* No appender, try next */
362
363     xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
364
365     /* First, try with a static buffer */
366     if (XBT_LOG_STATIC_BUFFER_SIZE) {
367       char buff[XBT_LOG_STATIC_BUFFER_SIZE];
368       ev->buffer = buff;
369       ev->buffer_size = sizeof buff;
370       va_start(ev->ap, fmt);
371       int done = cat->layout->do_layout(cat->layout, ev, fmt);
372       va_end(ev->ap);
373       if (done) {
374         appender->do_append(appender, buff);
375         continue;               /* Ok, that worked: go next */
376       }
377     }
378
379     /* The static buffer was too small, use a dynamically expanded one */
380     ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
381     ev->buffer = xbt_malloc(ev->buffer_size);
382     while (1) {
383       va_start(ev->ap, fmt);
384       int done = cat->layout->do_layout(cat->layout, ev, fmt);
385       va_end(ev->ap);
386       if (done)
387         break;                  /* Got it */
388       ev->buffer_size *= 2;
389       ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size);
390     }
391     appender->do_append(appender, ev->buffer);
392     xbt_free(ev->buffer);
393
394   } while (cat->additivity && (cat = cat->parent, 1));
395 }
396
397 #undef XBT_LOG_DYNAMIC_BUFFER_SIZE
398 #undef XBT_LOG_STATIC_BUFFER_SIZE
399
400 /* NOTE:
401  *
402  * The standard logging macros use _XBT_LOG_ISENABLED, which calls _xbt_log_cat_init().  Thus, if we want to avoid an
403  * infinite recursion, we can not use the standard logging macros in _xbt_log_cat_init(), and in all functions called
404  * from it.
405  *
406  * To circumvent the problem, we define the macro_xbt_log_init() as (0) for the length of the affected functions, and
407  * we do not forget to undefine it at the end!
408  */
409
410 static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_t setting)
411 {
412 #define _xbt_log_cat_init(a, b) (0)
413
414   if (setting->thresh != xbt_log_priority_uninitialized) {
415     xbt_log_threshold_set(category, setting->thresh);
416
417     XBT_DEBUG("Apply settings for category '%s': set threshold to %s (=%d)",
418            category->name, xbt_log_priority_names[category->threshold], category->threshold);
419   }
420
421   if (setting->fmt) {
422     xbt_log_layout_set(category, xbt_log_layout_format_new(setting->fmt));
423
424     XBT_DEBUG("Apply settings for category '%s': set format to %s", category->name, setting->fmt);
425   }
426
427   if (setting->additivity != -1) {
428     xbt_log_additivity_set(category, setting->additivity);
429
430     XBT_DEBUG("Apply settings for category '%s': set additivity to %s",
431            category->name, (setting->additivity ? "on" : "off"));
432   }
433   if (setting->appender) {
434     xbt_log_appender_set(category, setting->appender);
435     if (!category->layout)
436       xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
437     category->additivity = 0;
438     XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
439   }
440 #undef _xbt_log_cat_init
441 }
442
443 /*
444  * This gets called the first time a category is referenced and performs the initialization.
445  * Also resets threshold to inherited!
446  */
447 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
448 {
449 #define _xbt_log_cat_init(a, b) (0)
450
451   if (log_cat_init_mutex != NULL)
452     xbt_os_mutex_acquire(log_cat_init_mutex);
453
454   if (category->initialized) {
455     if (log_cat_init_mutex != NULL)
456       xbt_os_mutex_release(log_cat_init_mutex);
457     return priority >= category->threshold;
458   }
459
460   unsigned int cursor;
461   xbt_log_setting_t setting = NULL;
462   int found = 0;
463
464   XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
465          (category->firstChild ? category->firstChild->name : "none"),
466          (category->nextSibling ? category->nextSibling->name : "none"));
467
468   if (category == &_XBT_LOGV(XBT_LOG_ROOT_CAT)) {
469     category->threshold = xbt_log_priority_info;
470     category->appender = xbt_log_default_appender;
471     category->layout = xbt_log_default_layout;
472   } else {
473     if (!category->parent)
474       category->parent = &_XBT_LOGV(XBT_LOG_ROOT_CAT);
475
476     XBT_DEBUG("Set %s (%s) as father of %s ", category->parent->name,
477            (category->parent->initialized ? xbt_log_priority_names[category->parent->threshold] : "uninited"),
478            category->name);
479     xbt_log_parent_set(category, category->parent);
480
481     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
482       char *buf;
483       char *res = NULL;
484       xbt_log_category_t cpp = category->parent->firstChild;
485       while (cpp) {
486         if (res) {
487           buf = bprintf("%s %s", res, cpp->name);
488           free(res);
489           res = buf;
490         } else {
491           res = xbt_strdup(cpp->name);
492         }
493         cpp = cpp->nextSibling;
494       }
495
496       XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
497              (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
498
499       free(res);
500     }
501   }
502
503   /* Apply the control */
504   if (xbt_log_settings) {
505     xbt_assert(category, "NULL category");
506     xbt_assert(category->name);
507
508     xbt_dynar_foreach(xbt_log_settings, cursor, setting) {
509       xbt_assert(setting, "Damnit, NULL cat in the list");
510       xbt_assert(setting->catname, "NULL setting(=%p)->catname", (void *) setting);
511
512       if (!strcmp(setting->catname, category->name)) {
513         found = 1;
514         _xbt_log_cat_apply_set(category, setting);
515         xbt_dynar_cursor_rm(xbt_log_settings, &cursor);
516       }
517     }
518
519     if (!found)
520       XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)",
521                 category->name, xbt_log_priority_names[category->threshold], category->threshold);
522   }
523
524   category->initialized = 1;
525   if (log_cat_init_mutex != NULL)
526     xbt_os_mutex_release(log_cat_init_mutex);
527   return priority >= category->threshold;
528
529 #undef _xbt_log_cat_init
530 }
531
532 void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
533 {
534   xbt_assert(cat, "NULL category to be given a parent");
535   xbt_assert(parent, "The parent category of %s is NULL", cat->name);
536
537   /* if the category is initialized, unlink from current parent */
538   if (cat->initialized) {
539     xbt_log_category_t *cpp = &cat->parent->firstChild;
540
541     while (*cpp != cat && *cpp != NULL) {
542       cpp = &(*cpp)->nextSibling;
543     }
544
545     xbt_assert(*cpp == cat);
546     *cpp = cat->nextSibling;
547   }
548
549   cat->parent = parent;
550   cat->nextSibling = parent->firstChild;
551
552   parent->firstChild = cat;
553
554   if (!parent->initialized)
555     _xbt_log_cat_init(parent, xbt_log_priority_uninitialized /* ignored */ );
556
557   cat->threshold = parent->threshold;
558
559   cat->isThreshInherited = 1;
560 }
561
562 static void _set_inherited_thresholds(xbt_log_category_t cat)
563 {
564   xbt_log_category_t child = cat->firstChild;
565
566   for (; child != NULL; child = child->nextSibling) {
567     if (child->isThreshInherited) {
568       if (cat != &_XBT_LOGV(log))
569         XBT_VERB("Set category threshold of %s to %s (=%d)",
570               child->name, xbt_log_priority_names[cat->threshold], cat->threshold);
571       child->threshold = cat->threshold;
572       _set_inherited_thresholds(child);
573     }
574   }
575 }
576
577 void xbt_log_threshold_set(xbt_log_category_t cat, e_xbt_log_priority_t threshold)
578 {
579   cat->threshold = threshold;
580   cat->isThreshInherited = 0;
581
582   _set_inherited_thresholds(cat);
583 }
584
585 static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
586 {
587   const char *orig_control_string = control_string;
588   xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
589
590   set->catname = NULL;
591   set->thresh = xbt_log_priority_uninitialized;
592   set->fmt = NULL;
593   set->additivity = -1;
594   set->appender = NULL;
595
596   if (!*control_string)
597     return set;
598   XBT_DEBUG("Parse log setting '%s'", control_string);
599
600   control_string += strspn(control_string, " ");
601   const char *name = control_string;
602   control_string += strcspn(control_string, ".:= ");
603   const char *dot = control_string;
604   control_string += strcspn(control_string, ":= ");
605   const char *eq = control_string;
606
607   xbt_assert(*dot == '.' || (*eq != '=' && *eq != ':'), "Invalid control string '%s'", orig_control_string);
608
609   if (!strncmp(dot + 1, "threshold", (size_t) (eq - dot - 1))) {
610     int i;
611     char *neweq = xbt_strdup(eq + 1);
612     char *p = neweq - 1;
613
614     while (*(++p) != '\0') {
615       if (*p >= 'a' && *p <= 'z') {
616         *p -= 'a' - 'A';
617       }
618     }
619
620     XBT_DEBUG("New priority name = %s", neweq);
621     for (i = 0; i < xbt_log_priority_infinite; i++) {
622       if (!strncmp(xbt_log_priority_names[i], neweq, p - eq)) {
623         XBT_DEBUG("This is priority %d", i);
624         break;
625       }
626     }
627
628     if(i<XBT_LOG_STATIC_THRESHOLD){
629      fprintf(stderr,
630          "Priority '%s' (in setting '%s') is above allowed priority '%s'.\n\n"
631          "Compiling SimGrid with -DNDEBUG forbids the levels 'trace' and 'debug'\n"
632          "while -DNLOG forbids any logging, at any level.",
633              eq + 1, name, xbt_log_priority_names[XBT_LOG_STATIC_THRESHOLD]);
634      exit(1);
635     }else if (i < xbt_log_priority_infinite) {
636       set->thresh = (e_xbt_log_priority_t) i;
637     } else {
638       THROWF(arg_error, 0,
639              "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
640     }
641     free(neweq);
642   } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
643              !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
644     char *neweq = xbt_strdup(eq + 1);
645     char *p = neweq - 1;
646
647     while (*(++p) != '\0') {
648       if (*p >= 'a' && *p <= 'z') {
649         *p -= 'a' - 'A';
650       }
651     }
652     if (!strcmp(neweq, "ON") || !strcmp(neweq, "YES") || !strcmp(neweq, "1")) {
653       set->additivity = 1;
654     } else {
655       set->additivity = 0;
656     }
657     free(neweq);
658   } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
659              !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
660     char *neweq = xbt_strdup(eq + 1);
661
662     if (!strncmp(neweq, "file:", 5)) {
663       set->appender = xbt_log_appender_file_new(neweq + 5);
664     }else if (!strncmp(neweq, "rollfile:", 9)) {
665       set->appender = xbt_log_appender2_file_new(neweq + 9,1);
666     }else if (!strncmp(neweq, "splitfile:", 10)) {
667       set->appender = xbt_log_appender2_file_new(neweq + 10,0);
668     } else {
669       THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
670     }
671     free(neweq);
672   } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
673     set->fmt = xbt_strdup(eq + 1);
674   } else {
675     char buff[512];
676     snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
677     xbt_die("Unknown setting of the log category: '%s'", buff);
678   }
679   set->catname = (char *) xbt_malloc(dot - name + 1);
680
681   memcpy(set->catname, name, dot - name);
682   set->catname[dot - name] = '\0';      /* Just in case */
683   XBT_DEBUG("This is for cat '%s'", set->catname);
684
685   return set;
686 }
687
688 static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *name)
689 {
690   xbt_log_category_t child;
691   xbt_log_category_t res;
692
693   XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name,
694          cat->name, (cat->firstChild ? cat->firstChild->name : "none"),
695          (cat->nextSibling ? cat->nextSibling->name : "none"));
696   if (!strcmp(cat->name, name))
697     return cat;
698
699   for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
700     XBT_DEBUG("Dig into %s", child->name);
701     res = _xbt_log_cat_searchsub(child, name);
702     if (res)
703       return res;
704   }
705
706   return NULL;
707 }
708
709 /**
710  * \ingroup XBT_log
711  * \param control_string What to parse
712  *
713  * Typically passed a command-line argument. The string has the syntax:
714  *
715  *      ( [category] "." [keyword] ":" value (" ")... )...
716  *
717  * where [category] is one the category names (see \ref XBT_log_cats for a complete list of the ones defined in the
718  * SimGrid library) and keyword is one of the following:
719  *
720  *    - thres: category's threshold priority. Possible values:
721  *             TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL
722  *    - add or additivity: whether the logging actions must be passed to the parent category.
723  *      Possible values: 0, 1, no, yes, on, off.
724  *      Default value: yes.
725  *    - fmt: the format to use. See \ref log_use_conf_fmt for more information.
726  *    - app or appender: the appender to use. See \ref log_use_conf_app for more information.
727  */
728 void xbt_log_control_set(const char *control_string)
729 {
730   xbt_log_setting_t set;
731
732   /* To split the string in commands, and the cursors */
733   xbt_dynar_t set_strings;
734   char *str;
735   unsigned int cpt;
736
737   if (!control_string)
738     return;
739   XBT_DEBUG("Parse log settings '%s'", control_string);
740
741   /* Special handling of no_loc request, which asks for any file localization to be omitted (for tesh runs) */
742   if (!strcmp(control_string, "no_loc")) {
743     xbt_log_no_loc = 1;
744     return;
745   }
746   /* some initialization if this is the first time that this get called */
747   if (xbt_log_settings == NULL)
748     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
749
750   /* split the string, and remove empty entries */
751   set_strings = xbt_str_split_quoted(control_string);
752
753   if (xbt_dynar_is_empty(set_strings)) {     /* vicious user! */
754     xbt_dynar_free(&set_strings);
755     return;
756   }
757
758   /* Parse each entry and either use it right now (if the category was already created), or store it for further use */
759   xbt_dynar_foreach(set_strings, cpt, str) {
760     xbt_log_category_t cat = NULL;
761
762     set = _xbt_log_parse_setting(str);
763     cat = _xbt_log_cat_searchsub(&_XBT_LOGV(XBT_LOG_ROOT_CAT), set->catname);
764
765     if (cat) {
766       XBT_DEBUG("Apply directly");
767       _xbt_log_cat_apply_set(cat, set);
768       _free_setting((void *) &set);
769     } else {
770       XBT_DEBUG("Store for further application");
771       XBT_DEBUG("push %p to the settings", (void *) set);
772       xbt_dynar_push(xbt_log_settings, &set);
773     }
774   }
775   xbt_dynar_free(&set_strings);
776 }
777
778 void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
779 {
780   if (cat->appender) {
781     if (cat->appender->free_)
782       cat->appender->free_(cat->appender);
783     free(cat->appender);
784   }
785   cat->appender = app;
786 }
787
788 void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
789 {
790 #define _xbt_log_cat_init(a, b) (0)
791   if (!cat->appender) {
792     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
793     xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
794   }
795   if (cat->layout) {
796     if (cat->layout->free_) {
797       cat->layout->free_(cat->layout);
798     }
799     free(cat->layout);
800   }
801   cat->layout = lay;
802   xbt_log_additivity_set(cat, 0);
803 #undef _xbt_log_cat_init
804 }
805
806 void xbt_log_additivity_set(xbt_log_category_t cat, int additivity)
807 {
808   cat->additivity = additivity;
809 }
810
811 static void xbt_log_help(void)
812 {
813   printf(
814 "Description of the logging output:\n"
815 "\n"
816 "   Threshold configuration: --log=CATEGORY_NAME.thres:PRIORITY_LEVEL\n"
817 "      CATEGORY_NAME: defined in code with function 'XBT_LOG_NEW_CATEGORY'\n"
818 "      PRIORITY_LEVEL: the level to print (trace,debug,verbose,info,warning,error,critical)\n"
819 "         -> trace: enter and return of some functions\n"
820 "         -> debug: crufty output\n"
821 "         -> verbose: verbose output for the user wanting more\n"
822 "         -> info: output about the regular functionning\n"
823 "         -> warning: minor issue encountered\n"
824 "         -> error: issue encountered\n"
825 "         -> critical: major issue encountered\n"
826 "\n"
827 "   Format configuration: --log=CATEGORY_NAME.fmt:OPTIONS\n"
828 "      OPTIONS may be:\n"
829 "         -> %%%%: the %% char\n"
830 "         -> %%n: platform-dependent line separator (LOG4J compatible)\n"
831 "         -> %%e: plain old space (SimGrid extension)\n"
832 "\n"
833 "         -> %%m: user-provided message\n"
834 "\n"
835 "         -> %%c: Category name (LOG4J compatible)\n"
836 "         -> %%p: Priority name (LOG4J compatible)\n"
837 "\n"
838 "         -> %%h: Hostname (SimGrid extension)\n"
839 "         -> %%P: Process name (SimGrid extension)\n"
840 "         -> %%t: Thread \"name\" (LOG4J compatible -- actually the address of the thread in memory)\n"
841 "         -> %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)\n"
842 "\n"
843 "         -> %%F: file name where the log event was raised (LOG4J compatible)\n"
844 "         -> %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as in 'l'etter)\n"
845 "         -> %%L: line number where the log event was raised (LOG4J compatible)\n"
846 "         -> %%M: function name (LOG4J compatible -- called method name here of course).\n"
847 "                 Defined only when using gcc because there is no __FUNCTION__ elsewhere.\n"
848 "\n"
849 "         -> %%b: full backtrace (Called %%throwable in LOG4J). Defined only under windows or when using the GNU libc because\n"
850 "                 backtrace() is not defined elsewhere, and we only have a fallback for windows boxes, not mac ones for example.\n"
851 "         -> %%B: short backtrace (only the first line of the %%b). Called %%throwable{short} in LOG4J; defined where %%b is.\n"
852 "\n"
853 "         -> %%d: date (UNIX-like epoch)\n"
854 "         -> %%r: application age (time elapsed since the beginning of the application)\n"
855 "\n"
856 "   Miscellaneous:\n"
857 "      --help-log-categories    Display the current hierarchy of log categories.\n"
858 "      --log=no_loc             Don't print file names in messages (for tesh tests).\n"
859 "\n"
860     );
861 }
862
863 static int xbt_log_cat_cmp(const void *pa, const void *pb)
864 {
865   xbt_log_category_t a = *(xbt_log_category_t *)pa;
866   xbt_log_category_t b = *(xbt_log_category_t *)pb;
867   return strcmp(a->name, b->name);
868 }
869
870 static void xbt_log_help_categories_rec(xbt_log_category_t category, const char *prefix)
871 {
872   char *this_prefix;
873   char *child_prefix;
874   unsigned i;
875   xbt_log_category_t cat;
876
877   if (!category)
878     return;
879
880   if (category->parent) {
881     this_prefix = bprintf("%s \\_ ", prefix);
882     child_prefix = bprintf("%s |  ", prefix);
883   } else {
884     this_prefix = xbt_strdup(prefix);
885     child_prefix = xbt_strdup(prefix);
886   }
887
888   xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
889   for (cat = category ; cat != NULL; cat = cat->nextSibling)
890     xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
891
892   xbt_dynar_sort(dynar, xbt_log_cat_cmp);
893
894   xbt_dynar_foreach(dynar, i, cat){
895     if (i == xbt_dynar_length(dynar) - 1 && category->parent)
896       *strrchr(child_prefix, '|') = ' ';
897     printf("%s%s: %s\n", this_prefix, cat->name, cat->description);
898     xbt_log_help_categories_rec(cat->firstChild, child_prefix);
899   }
900
901   xbt_dynar_free(&dynar);
902   xbt_free(this_prefix);
903   xbt_free(child_prefix);
904 }
905
906 static void xbt_log_help_categories(void)
907 {
908   printf("Current log category hierarchy:\n");
909   xbt_log_help_categories_rec(&_XBT_LOGV(XBT_LOG_ROOT_CAT), "   ");
910   printf("\n");
911 }