Logo AND Algorithmique Numérique Distribuée

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