Logo AND Algorithmique Numérique Distribuée

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