Logo AND Algorithmique Numérique Distribuée

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