Logo AND Algorithmique Numérique Distribuée

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