Logo AND Algorithmique Numérique Distribuée

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