Logo AND Algorithmique Numérique Distribuée

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