Logo AND Algorithmique Numérique Distribuée

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