From dda3c203509614d7c0a960960e6235055f053b4d Mon Sep 17 00:00:00 2001 From: mquinson Date: Thu, 10 May 2007 16:20:17 +0000 Subject: [PATCH] Random cleanups in the log mecanisms, and make room for the layouts I need to finish the TESH (default settings for layouts work, and there is no way to change them so far ;) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3504 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/xbt/log.h | 70 ++++++++++---- src/Makefile.am | 2 +- src/Makefile.in | 66 +++++++------ src/xbt/log.c | 161 ++++++++++++++++++++------------ src/xbt/xbt_log_layout_simple.c | 66 +++++++++++++ 5 files changed, 260 insertions(+), 105 deletions(-) create mode 100644 src/xbt/xbt_log_layout_simple.c diff --git a/include/xbt/log.h b/include/xbt/log.h index 8bff5c9bb8..6947d4642b 100644 --- a/include/xbt/log.h +++ b/include/xbt/log.h @@ -225,6 +225,7 @@ XBT_PUBLIC(void) xbt_log_control_set(const char* cs); /* Forward declarations */ typedef struct xbt_log_appender_s s_xbt_log_appender_t,*xbt_log_appender_t; +typedef struct xbt_log_layout_s s_xbt_log_layout_t, *xbt_log_layout_t; typedef struct xbt_log_event_s s_xbt_log_event_t, *xbt_log_event_t; typedef struct xbt_log_category_s s_xbt_log_category_t,*xbt_log_category_t; @@ -232,23 +233,31 @@ typedef struct xbt_log_category_s s_xbt_log_category_t,*xbt_log_category_t; * Do NOT access any members of this structure directly. FIXME: move to private? */ struct xbt_log_category_s { - xbt_log_category_t parent; -/*@null@*/ xbt_log_category_t firstChild; -/*@null@*/ xbt_log_category_t nextSibling; - const char *name; - int threshold; - int isThreshInherited; -/*@null@*/ xbt_log_appender_t appender; - int willLogToParent; - /* TODO: Formats? */ + xbt_log_category_t parent; + xbt_log_category_t firstChild; + xbt_log_category_t nextSibling; + const char *name; + int threshold; + int isThreshInherited; + xbt_log_appender_t appender; + int additivity; }; struct xbt_log_appender_s { - void (*do_append) (xbt_log_appender_t thisLogAppender, - xbt_log_event_t event, const char *fmt); - void *appender_data; + void (*do_append) (xbt_log_appender_t this_appender, + char *event); + void (*free_) (xbt_log_appender_t this_); + xbt_log_layout_t layout; + void *data; }; +struct xbt_log_layout_s { + char *(*do_layout)(xbt_log_layout_t l, + xbt_log_event_t event, const char *fmt); + void (*free_) (xbt_log_layout_t l); + void *data; +} ; + struct xbt_log_event_s { xbt_log_category_t cat; e_xbt_log_priority_t priority; @@ -266,7 +275,7 @@ struct xbt_log_event_s { * Programatically alters a category's threshold priority (don't use). */ XBT_PUBLIC(void) xbt_log_threshold_set(xbt_log_category_t cat, - e_xbt_log_priority_t thresholdPriority); + e_xbt_log_priority_t thresholdPriority); /** * \ingroup XBT_log_implem @@ -283,12 +292,37 @@ XBT_PUBLIC(void) xbt_log_parent_set(xbt_log_category_t cat, * \param cat the category (not only its name, but the variable) * \param app the appender * - * Programatically sets the category's appender (don't use). + * Programatically sets the category's appender. + * (the prefered interface is throught xbt_log_control_set()) + * */ XBT_PUBLIC(void) xbt_log_appender_set(xbt_log_category_t cat, - xbt_log_appender_t app); + xbt_log_appender_t app); + +/** + * \ingroup XBT_log_implem + * \param cat the category (not only its name, but the variable) + * \param additivity whether logging actions must be passed to parent. + * + * Programatically sets whether the logging actions must be passed to + * the parent category. + * (the prefered interface is throught xbt_log_control_set()) + * + */ +XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat, + int additivity); -/* Functions that you shouldn't call. */ +/** @brief create a new simple layout + * + * This layout is not as flexible as the pattern one + */ +XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(void); +XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(xbt_log_layout_t layout); + + +/* ********************************** */ +/* Functions that you shouldn't call */ +/* ********************************** */ XBT_PUBLIC(void) _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) _XBT_GNUC_PRINTF(2,3); @@ -303,6 +337,10 @@ XBT_LOG_EXTERNAL_CATEGORY(GRAS); extern xbt_log_appender_t xbt_log_default_appender; +/* ********************** */ +/* Public functions again */ +/* ********************** */ + /** * \ingroup XBT_log * \param catName name of the category diff --git a/src/Makefile.am b/src/Makefile.am index 508e747185..bdac64a402 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -123,7 +123,7 @@ COMMON_SRC=\ \ xbt/sysdep.c \ xbt/asserts.c \ - xbt/log.c xbt/log_default_appender.c \ + xbt/log.c xbt/xbt_log_appender_file.c xbt/xbt_log_layout_simple.c \ xbt/mallocator.c \ xbt/dynar.c \ xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c \ diff --git a/src/Makefile.in b/src/Makefile.in index 99369193f3..fca66be72e 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -121,12 +121,12 @@ LTLIBRARIES = $(lib_LTLIBRARIES) libgras_la_LIBADD = am__libgras_la_SOURCES_DIST = xbt/snprintf.c xbt/xbt_str.c xbt/ex.c \ xbt_modinter.h gras_modinter.h xbt/sysdep.c xbt/asserts.c \ - xbt/log.c xbt/log_default_appender.c xbt/mallocator.c \ - xbt/dynar.c xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c \ - xbt/dict_multi.c xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c \ - xbt/set.c xbt/xbt_matrix.c xbt/xbt_peer.c xbt/xbt_main.c \ - xbt/config.c xbt/cunit.c xbt/graphxml_parse.c \ - gras_simix/gras_simix_gras.c \ + xbt/log.c xbt/xbt_log_appender_file.c \ + xbt/xbt_log_layout_simple.c xbt/mallocator.c xbt/dynar.c \ + xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c xbt/dict_multi.c \ + xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c xbt/set.c \ + xbt/xbt_matrix.c xbt/xbt_peer.c xbt/xbt_main.c xbt/config.c \ + xbt/cunit.c xbt/graphxml_parse.c gras_simix/gras_simix_gras.c \ gras_simix/Transport/gras_simix_transport.c \ gras_simix/Transport/gras_simix_transport_private.h \ gras_simix/Msg/gras_simix_msg.c \ @@ -187,11 +187,11 @@ am__libgras_la_SOURCES_DIST = xbt/snprintf.c xbt/xbt_str.c xbt/ex.c \ @USE_SIMIX_FALSE@ cbps.lo datadesc.lo ddt_parse.lo \ @USE_SIMIX_FALSE@ ddt_parse.yy.lo am__objects_3 = snprintf.lo xbt_str.lo ex.lo sysdep.lo asserts.lo \ - log.lo log_default_appender.lo mallocator.lo dynar.lo dict.lo \ - dict_elm.lo dict_cursor.lo dict_multi.lo heap.lo fifo.lo \ - swag.lo graph.lo set.lo xbt_matrix.lo xbt_peer.lo xbt_main.lo \ - config.lo cunit.lo graphxml_parse.lo $(am__objects_1) \ - $(am__objects_2) + log.lo xbt_log_appender_file.lo xbt_log_layout_simple.lo \ + mallocator.lo dynar.lo dict.lo dict_elm.lo dict_cursor.lo \ + dict_multi.lo heap.lo fifo.lo swag.lo graph.lo set.lo \ + xbt_matrix.lo xbt_peer.lo xbt_main.lo config.lo cunit.lo \ + graphxml_parse.lo $(am__objects_1) $(am__objects_2) am__objects_4 = rl_stubs.lo xbt_thread.lo rl_transport.lo \ transport_plugin_file.lo transport_plugin_tcp.lo rl_emul.lo \ rl_process.lo rl_time.lo rl_dns.lo rl_msg.lo @@ -210,12 +210,12 @@ libgras_la_OBJECTS = $(am_libgras_la_OBJECTS) libsimgrid_la_LIBADD = am__libsimgrid_la_SOURCES_DIST = xbt/snprintf.c xbt/xbt_str.c xbt/ex.c \ xbt_modinter.h gras_modinter.h xbt/sysdep.c xbt/asserts.c \ - xbt/log.c xbt/log_default_appender.c xbt/mallocator.c \ - xbt/dynar.c xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c \ - xbt/dict_multi.c xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c \ - xbt/set.c xbt/xbt_matrix.c xbt/xbt_peer.c xbt/xbt_main.c \ - xbt/config.c xbt/cunit.c xbt/graphxml_parse.c \ - gras_simix/gras_simix_gras.c \ + xbt/log.c xbt/xbt_log_appender_file.c \ + xbt/xbt_log_layout_simple.c xbt/mallocator.c xbt/dynar.c \ + xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c xbt/dict_multi.c \ + xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c xbt/set.c \ + xbt/xbt_matrix.c xbt/xbt_peer.c xbt/xbt_main.c xbt/config.c \ + xbt/cunit.c xbt/graphxml_parse.c gras_simix/gras_simix_gras.c \ gras_simix/Transport/gras_simix_transport.c \ gras_simix/Transport/gras_simix_transport_private.h \ gras_simix/Msg/gras_simix_msg.c \ @@ -562,12 +562,12 @@ VERSION_INFO = -version-info 1:0:0 lib_LTLIBRARIES = libsimgrid.la libgras.la COMMON_SRC = xbt/snprintf.c xbt/xbt_str.c xbt/ex.c xbt_modinter.h \ gras_modinter.h xbt/sysdep.c xbt/asserts.c xbt/log.c \ - xbt/log_default_appender.c xbt/mallocator.c xbt/dynar.c \ - xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c xbt/dict_multi.c \ - xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c xbt/set.c \ - xbt/xbt_matrix.c xbt/xbt_peer.c xbt/xbt_main.c xbt/config.c \ - xbt/cunit.c xbt/graphxml_parse.c $(am__append_9) \ - $(am__append_12) + xbt/xbt_log_appender_file.c xbt/xbt_log_layout_simple.c \ + xbt/mallocator.c xbt/dynar.c xbt/dict.c xbt/dict_elm.c \ + xbt/dict_cursor.c xbt/dict_multi.c xbt/heap.c xbt/fifo.c \ + xbt/swag.c xbt/graph.c xbt/set.c xbt/xbt_matrix.c \ + xbt/xbt_peer.c xbt/xbt_main.c xbt/config.c xbt/cunit.c \ + xbt/graphxml_parse.c $(am__append_9) $(am__append_12) RL_SRC = \ gras/rl_stubs.c xbt/xbt_thread.c \ \ @@ -849,7 +849,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/host.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lagrange.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log_default_appender.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/m_process.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mallocator.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/maxmin.Plo@am__quote@ @@ -916,6 +915,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/transport_plugin_tcp.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/workstation.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/workstation_KCCFLN05.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_log_appender_file.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_log_layout_simple.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_main.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_matrix.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_peer.Plo@am__quote@ @@ -985,12 +986,19 @@ log.lo: xbt/log.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o log.lo `test -f 'xbt/log.c' || echo '$(srcdir)/'`xbt/log.c -log_default_appender.lo: xbt/log_default_appender.c -@am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT log_default_appender.lo -MD -MP -MF "$(DEPDIR)/log_default_appender.Tpo" -c -o log_default_appender.lo `test -f 'xbt/log_default_appender.c' || echo '$(srcdir)/'`xbt/log_default_appender.c; \ -@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/log_default_appender.Tpo" "$(DEPDIR)/log_default_appender.Plo"; else rm -f "$(DEPDIR)/log_default_appender.Tpo"; exit 1; fi -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='xbt/log_default_appender.c' object='log_default_appender.lo' libtool=yes @AMDEPBACKSLASH@ +xbt_log_appender_file.lo: xbt/xbt_log_appender_file.c +@am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT xbt_log_appender_file.lo -MD -MP -MF "$(DEPDIR)/xbt_log_appender_file.Tpo" -c -o xbt_log_appender_file.lo `test -f 'xbt/xbt_log_appender_file.c' || echo '$(srcdir)/'`xbt/xbt_log_appender_file.c; \ +@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/xbt_log_appender_file.Tpo" "$(DEPDIR)/xbt_log_appender_file.Plo"; else rm -f "$(DEPDIR)/xbt_log_appender_file.Tpo"; exit 1; fi +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='xbt/xbt_log_appender_file.c' object='xbt_log_appender_file.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o xbt_log_appender_file.lo `test -f 'xbt/xbt_log_appender_file.c' || echo '$(srcdir)/'`xbt/xbt_log_appender_file.c + +xbt_log_layout_simple.lo: xbt/xbt_log_layout_simple.c +@am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT xbt_log_layout_simple.lo -MD -MP -MF "$(DEPDIR)/xbt_log_layout_simple.Tpo" -c -o xbt_log_layout_simple.lo `test -f 'xbt/xbt_log_layout_simple.c' || echo '$(srcdir)/'`xbt/xbt_log_layout_simple.c; \ +@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/xbt_log_layout_simple.Tpo" "$(DEPDIR)/xbt_log_layout_simple.Plo"; else rm -f "$(DEPDIR)/xbt_log_layout_simple.Tpo"; exit 1; fi +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='xbt/xbt_log_layout_simple.c' object='xbt_log_layout_simple.lo' libtool=yes @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o log_default_appender.lo `test -f 'xbt/log_default_appender.c' || echo '$(srcdir)/'`xbt/log_default_appender.c +@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o xbt_log_layout_simple.lo `test -f 'xbt/xbt_log_layout_simple.c' || echo '$(srcdir)/'`xbt/xbt_log_layout_simple.c mallocator.lo: xbt/mallocator.c @am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT mallocator.lo -MD -MP -MF "$(DEPDIR)/mallocator.Tpo" -c -o mallocator.lo `test -f 'xbt/mallocator.c' || echo '$(srcdir)/'`xbt/mallocator.c; \ diff --git a/src/xbt/log.c b/src/xbt/log.c index 2183371b79..f9746cd38c 100644 --- a/src/xbt/log.c +++ b/src/xbt/log.c @@ -323,12 +323,13 @@ category performs the following actions: - if the category has an appender, the message is passed to the appender's doAppend() function, - - if 'willLogToParent' is true for the category, the message is passed - to the category's parent. + - if additivity is true for the category (which is the case by + default, and can be controlled by xbt_log_additivity_set()), the + message is passed to the category's parent. -By default, only the root category have an appender, and 'willLogToParent' -is true for any other category. This situation causes all messages to be -logged by the root category's appender. +By default, only the root category have an appender, and any other category has +its additivity set to true. This causes all messages to be logged by the root +category's appender. The default appender function currently prints to stderr, and no other one exist, even if more would be needed, like the one able to send the logs to a @@ -340,6 +341,8 @@ welcome here, too. */ +xbt_log_appender_t xbt_log_default_appender = NULL; /* set in log_init */ + typedef struct { char *catname; e_xbt_log_priority_t thresh; @@ -348,10 +351,10 @@ typedef struct { static xbt_dynar_t xbt_log_settings=NULL; static void _free_setting(void *s) { - xbt_log_setting_t set=(xbt_log_setting_t)s; + xbt_log_setting_t set=*(xbt_log_setting_t*)s; if (set) { free(set->catname); - //free(set); /*FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */ + free(set); } } @@ -386,34 +389,61 @@ void xbt_log_init(int *argc,char **argv) { int i,j; char *opt; + /* create the default appender and install it in the root category, + which were already created (damnit. Too slow little beetle)*/ + xbt_log_default_appender = + xbt_log_appender_file_new(xbt_log_layout_simple_new()); + _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender; + /* Set logs and init log submodule */ for (i=1; i<*argc; i++){ - if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log=")) || - !strncmp(argv[i],"--surf-log=",strlen("--surf-log=")) || - !strncmp(argv[i],"--msg-log=",strlen("--msg-log=")) || - !strncmp(argv[i],"--simix-log=",strlen("--simix-log=")) || - !strncmp(argv[i],"--xbt-log=",strlen("--xbt-log="))){ - - opt=strchr(argv[i],'='); - opt++; - xbt_log_control_set(opt); - DEBUG1("Did apply '%s' as log setting",opt); - /*remove this from argv*/ + if (!strncmp(argv[i],"--log=",strlen("--log=")) || + !strncmp(argv[i],"--gras-log=",strlen("--gras-log=")) || + !strncmp(argv[i],"--surf-log=",strlen("--surf-log=")) || + !strncmp(argv[i],"--msg-log=",strlen("--msg-log=")) || + !strncmp(argv[i],"--simix-log=",strlen("--simix-log=")) || + !strncmp(argv[i],"--xbt-log=",strlen("--xbt-log="))){ - for (j=i+1; j<*argc; j++){ - argv[j-1] = argv[j]; - } - - argv[j-1] = NULL; - (*argc)--; - i--; /* compensate effect of next loop incrementation */ + opt=strchr(argv[i],'='); + opt++; + xbt_log_control_set(opt); + DEBUG1("Did apply '%s' as log setting",opt); + /*remove this from argv*/ + + for (j=i+1; j<*argc; j++){ + argv[j-1] = argv[j]; + } + + argv[j-1] = NULL; + (*argc)--; + i--; /* compensate effect of next loop incrementation */ } } } +static void log_shutdown_category(xbt_log_category_t cat) { + xbt_log_category_t child; + + if (cat->appender) { + if (cat->appender->layout) { + if (cat->appender->layout->free_) + cat->appender->layout->free_(cat->appender->layout); + free(cat->appender->layout); + } + if (cat->appender->free_) + cat->appender->free_(cat->appender); + free(cat->appender); + } + + + for(child=cat->firstChild ; child != NULL; child = child->nextSibling) + log_shutdown_category(child); +} + void xbt_log_exit(void) { VERB0("Exiting log"); xbt_dynar_free(&xbt_log_settings); + log_shutdown_category(&_XBT_LOGV(XBT_LOG_ROOT_CAT)); VERB0("Exited log"); } @@ -425,10 +455,12 @@ void _xbt_log_event_log( xbt_log_event_t ev, const char *fmt, ...) { while(1) { xbt_log_appender_t appender = cat->appender; if (appender != NULL) { - - appender->do_append(appender, ev, fmt); + + char *str= appender->layout->do_layout(appender->layout, + ev, fmt); + appender->do_append(appender, str); } - if (!cat->willLogToParent) + if (!cat->additivity) break; cat = cat->parent; @@ -476,7 +508,6 @@ int _xbt_log_cat_init(xbt_log_category_t category, xbt_log_threshold_set(category, setting->thresh); xbt_dynar_cursor_rm(xbt_log_settings,&cursor); - free(setting); if (category->threshold <= xbt_log_priority_debug) { _log_ev.cat = category; @@ -629,7 +660,7 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char* control_string) { } set->catname=(char*)xbt_malloc(dot - name+1); - strncpy(set->catname,name,dot-name); + memcpy(set->catname,name,dot-name); set->catname[dot-name]='\0'; /* Just in case */ DEBUG1("This is for cat '%s'", set->catname); @@ -661,41 +692,51 @@ static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat,char *na * * - thres: category's threshold priority. Possible values: * TRACE,DEBUG,VERBOSE,INFO,WARNING,ERROR,CRITICAL + * - add or additivity: whether the logging actions must be passed to + * the parent category. + * Possible values: 0, 1, yes, no. + * Default value: yes. * */ void xbt_log_control_set(const char* control_string) { xbt_log_setting_t set; - char *cs; - char *p; - int done = 0; - if (!control_string || control_string[0] == '\0') + /* To split the string in commands, and the cursors */ + xbt_dynar_t set_strings; + char *str; + int cpt; + + if (!control_string) return; DEBUG1("Parse log settings '%s'",control_string); - if (control_string == NULL) - return; + + /* some initialization if this is the first time that this get called */ if (xbt_log_settings == NULL) xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), - _free_setting); - - cs=xbt_strdup(control_string); + _free_setting); + + /* split the string, and remove empty entries */ + set_strings=xbt_str_split(control_string," "); + xbt_dynar_foreach(set_strings,cpt,str) { + xbt_str_trim(str,NULL); + if (str[0]=='\0') { + xbt_dynar_cursor_rm(set_strings,&cpt); + } + } - xbt_str_strip_spaces(cs); + if (xbt_dynar_length(set_strings) == 0) { /* vicious user! */ + xbt_dynar_free(&set_strings); + return; + } - while (!done) { + /* Parse each entry and either use it right now (if the category was already + created), or store it for further use */ + xbt_dynar_foreach(set_strings,cpt,str) { xbt_log_category_t cat=NULL; int found=0; xbt_ex_t e; - p=strrchr(cs,' '); - if (p) { - *p='\0'; - p++; - } else { - p=cs; - done = 1; - } - set = _xbt_log_parse_setting(p); + set = _xbt_log_parse_setting(str); TRY { cat = _xbt_log_cat_searchsub(&_XBT_LOGV(root),set->catname); @@ -705,25 +746,27 @@ void xbt_log_control_set(const char* control_string) { RETHROW; xbt_ex_free(e); found = 0; - - DEBUG0("Store for further application"); - DEBUG1("push %p to the settings",(void*)set); - xbt_dynar_push(xbt_log_settings,&set); } if (found) { DEBUG0("Apply directly"); xbt_log_threshold_set(cat,set->thresh); - _free_setting((void*)set); - free(set); + _free_setting((void*)&set); + } else { + + DEBUG0("Store for further application"); + DEBUG1("push %p to the settings",(void*)set); + xbt_dynar_push(xbt_log_settings,&set); } } - free(cs); + xbt_dynar_free(&set_strings); } void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app) { - cat->appender = app; - +} + +void xbt_log_additivity_set(xbt_log_category_t cat, int additivity) { + cat->additivity = additivity; } diff --git a/src/xbt/xbt_log_layout_simple.c b/src/xbt/xbt_log_layout_simple.c new file mode 100644 index 0000000000..ec68d8b6d4 --- /dev/null +++ b/src/xbt/xbt_log_layout_simple.c @@ -0,0 +1,66 @@ +/* $Id$ */ + +/* layout_simple - a dumb log layout */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "gras/virtu.h" +#include + +extern const char *xbt_log_priority_names[7]; + + +static char *xbt_log_layout_simple_doit(xbt_log_layout_t l, + xbt_log_event_t ev, + const char *fmt) { + static char res[1024]; + static double begin_of_time = -1; + char *p; + + xbt_assert0(ev->priority>=0, + "Negative logging priority naturally forbidden"); + xbt_assert1(ev->prioritypriority); + + if (begin_of_time<0) + begin_of_time=gras_os_time(); + + p = res; + p += sprintf(res,"[");; + /* Display the proc info if available */ + if(strlen(xbt_procname())) + p += sprintf(p,"%s:%s:(%ld) ", + gras_os_myname(), xbt_procname(),gras_os_getpid()); + + /* Display the date */ + p += sprintf(p,"%f] ", gras_os_time()-begin_of_time); + + + /* Display file position if not INFO*/ + if (ev->priority != xbt_log_priority_info) + p += sprintf(p, "%s:%d: ", ev->fileName, ev->lineNum); + + /* Display category name */ + p += sprintf(p, "[%s/%s] ", + ev->cat->name, xbt_log_priority_names[ev->priority] ); + + /* Display user-provided message */ + p += vsprintf(p, fmt, ev->ap); + + /* End it */ + p += sprintf(p, "\n"); + + return res; +} + +xbt_log_layout_t xbt_log_layout_simple_new() { + xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1); + res->do_layout = xbt_log_layout_simple_doit; + return res; +} -- 2.20.1