Logo AND Algorithmique Numérique Distribuée

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