Logo AND Algorithmique Numérique Distribuée

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