Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[From Arnaud Giersch] In standard C, we are allowed to initialize a variable with...
[simgrid.git] / include / xbt / log.h
index f98ba80..1032716 100644 (file)
@@ -1,8 +1,7 @@
-/* $Id$ */
-
 /* log - a generic logging facility in the spirit of log4j                  */
 
-/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
+/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
+ * 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. */
@@ -12,7 +11,7 @@
  *
  *
  */
+
 /** \defgroup XBT_log_cats Existing log categories
  *  \ingroup XBT_log
  *  \brief (automatically extracted) 
@@ -43,21 +42,21 @@ SG_BEGIN_DECL()
  *
  * The different existing priorities.
 */
-typedef enum {
-  xbt_log_priority_none          = 0,  /* used internally (don't poke with) */
-  xbt_log_priority_trace         = 1,  /**< enter and return of some functions */
-  xbt_log_priority_debug         = 2,  /**< crufty output  */
-  xbt_log_priority_verbose       = 3,  /**< verbose output for the user wanting more */
-  xbt_log_priority_info          = 4,  /**< output about the regular functionning */
-  xbt_log_priority_warning       = 5,  /**< minor issue encountered */
-  xbt_log_priority_error         = 6,  /**< issue encountered */
-  xbt_log_priority_critical      = 7,  /**< major issue encountered */
-
-  xbt_log_priority_infinite      = 8,  /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
-
-  xbt_log_priority_uninitialized = -1  /* used internally (don't poke with) */
-} e_xbt_log_priority_t;
-             
+     typedef enum {
+       xbt_log_priority_none = 0,       /* used internally (don't poke with) */
+       xbt_log_priority_trace = 1,     /**< enter and return of some functions */
+       xbt_log_priority_debug = 2,     /**< crufty output  */
+       xbt_log_priority_verbose = 3,   /**< verbose output for the user wanting more */
+       xbt_log_priority_info = 4,      /**< output about the regular functionning */
+       xbt_log_priority_warning = 5,   /**< minor issue encountered */
+       xbt_log_priority_error = 6,     /**< issue encountered */
+       xbt_log_priority_critical = 7,  /**< major issue encountered */
+
+       xbt_log_priority_infinite = 8,  /**< value for XBT_LOG_STATIC_THRESHOLD to not log */
+
+       xbt_log_priority_uninitialized = -1      /* used internally (don't poke with) */
+     } e_xbt_log_priority_t;
+
 
 /*
  * define NLOG to disable at compilation time any logging request
@@ -87,21 +86,55 @@ typedef enum {
 #endif /* !defined(NLOG) */
 
 /* Transforms a category name to a global variable name. */
-#define _XBT_LOGV(cat)   _XBT_LOG_CONCAT(_simgrid_this_log_category_does_not_exist__, cat)
+#define _XBT_LOGV(cat)   _XBT_LOG_CONCAT(_simgrid_log_category__, cat)
 #define _XBT_LOG_CONCAT(x,y) x ## y
 
 /* The root of the category hierarchy. */
 #define XBT_LOG_ROOT_CAT   root
 
+/* The whole tree of categories is connected by setting the address of
+ * the parent category as a field of the child one.
+ *
+ * In strict ansi C, we are allowed to initialize a variable with "a
+ * pointer to an lvalue designating an object of static storage
+ * duration" [ISO/IEC 9899:1999, Section 6.6].
+ * 
+ * Unfortunately, Visual C builder does not target any standard
+ * compliance, and C99 is not an exception to this unfortunate rule.
+ * 
+ * So, we work this around by adding a XBT_LOG_CONNECT() macro,
+ * allowing to connect a child to its parent. It should be used
+ * during the initialization of the code, before the child category
+ * gets used.
+ * 
+ * When compiling with gcc, this is not necessary (XBT_LOG_CONNECT
+ * defines to nothing). When compiling with MSVC, this is needed if
+ * you don't want to see your child category become a child of root
+ * directly.
+ */
+#if defined(_MSC_VER)
+# define _XBT_LOG_PARENT_INITIALIZER(parent) NULL
+# define XBT_LOG_CONNECT(parent_cat,child)       _XBT_LOGV(child).parent = &_XBT_LOGV(parent_cat)
+#else
+# define _XBT_LOG_PARENT_INITIALIZER(parent) &_XBT_LOGV(parent)
+# define XBT_LOG_CONNECT(parent_cat,child)      /*  xbt_assert(_XBT_LOGV(child).parent == &_XBT_LOGV(parent_cat)) */
+#endif
+
 /* XBT_LOG_NEW_SUBCATEGORY_helper:
  * Implementation of XBT_LOG_NEW_SUBCATEGORY, which must declare "extern parent" in addition
  * to avoid an extra declaration of root when XBT_LOG_NEW_SUBCATEGORY is called by
  * XBT_LOG_NEW_CATEGORY */
 #define XBT_LOG_NEW_SUBCATEGORY_helper(catName, parent, desc) \
     XBT_EXPORT_NO_IMPORT(s_xbt_log_category_t) _XBT_LOGV(catName) = {       \
-        &_XBT_LOGV(parent), 0, 0,                    \
-        #catName, xbt_log_priority_uninitialized, 1, \
-        0, 0, 1                                      \
+        _XBT_LOG_PARENT_INITIALIZER(parent),            \
+        NULL /* firstChild */,                          \
+       NULL /* nextSibling */,                         \
+        #catName,                                       \
+        xbt_log_priority_uninitialized /* threshold */, \
+        1 /* isThreshInherited */,                      \
+        NULL /* appender */,                            \
+       NULL /* layout */,                              \
+       1 /* additivity */                              \
     }
 /**
  * \ingroup XBT_log
@@ -124,17 +157,9 @@ typedef enum {
  *
  * Creates a new subcategory of the root category.
  */
-/*#if (defined(_WIN32) && !defined(DLL_STATIC)) KILLME?
-  # define XBT_LOG_NEW_CATEGORY(catName,desc)  \
-       XBT_EXPORT_NO_IMPORT(s_xbt_log_category_t) _XBT_LOGV(catName) = {       \
-        0, 0, 0,                    \
-               #catName, xbt_log_priority_uninitialized, 1, \
-        0, 1                                          \
-    }
-#else*/
-# define XBT_LOG_NEW_CATEGORY(catName,desc)  XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)  
-//#endif
+# define XBT_LOG_NEW_CATEGORY(catName,desc)  \
+   XBT_LOG_NEW_SUBCATEGORY_helper(catName, XBT_LOG_ROOT_CAT, desc)
+
 
 /**
  * \ingroup XBT_log  
@@ -144,11 +169,11 @@ typedef enum {
  * Indicates which category is the default one.
  */
 
-#if defined(XBT_LOG_MAYDAY) /*|| defined (NLOG) * turning logging off */
+#if defined(XBT_LOG_MAYDAY) || defined(SUPERNOVAE_MODE)     /*|| defined (NLOG) * turning logging off */
 # define XBT_LOG_DEFAULT_CATEGORY(cname)
 #else
 # define XBT_LOG_DEFAULT_CATEGORY(cname) \
-        static xbt_log_category_t _XBT_LOGV(default) _XBT_GNUC_UNUSED = &_XBT_LOGV(cname) 
+        static xbt_log_category_t _XBT_LOGV(default) _XBT_GNUC_UNUSED = &_XBT_LOGV(cname)
 #endif
 
 /**
@@ -160,28 +185,9 @@ typedef enum {
  * Creates a new subcategory of the root category and makes it the default
  * (used by macros that don't explicitly specify a category).
  */
-
-  /*
-#if (defined(_WIN32) && !defined(DLL_STATIC))
-# define XBT_LOG_NEW_ROOT_SUBCATEGORY(cname,desc) \
-       XBT_EXPORT_NO_IMPORT(s_xbt_log_category_t) _XBT_LOGV(cname) = {       \
-        0, 0, 0,                    \
-               #cname, xbt_log_priority_uninitialized, 1, \
-        0, 1                                          \
-    }
-# define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc)        \
-       XBT_LOG_NEW_ROOT_SUBCATEGORY(cname,desc); \
-       XBT_LOG_DEFAULT_CATEGORY(cname)
-    
-#else
-*/
 # define XBT_LOG_NEW_DEFAULT_CATEGORY(cname,desc)        \
     XBT_LOG_NEW_CATEGORY(cname,desc);                   \
     XBT_LOG_DEFAULT_CATEGORY(cname)
-   
-//#endif
-
-
 
 /**
  * \ingroup XBT_log  
@@ -223,51 +229,50 @@ typedef enum {
 
 /* Functions you may call */
 
-XBT_PUBLIC(void) xbt_log_control_set(const charcs);
+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;
+     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;
 
 /*
  * Do NOT access any members of this structure directly. FIXME: move to private?
  */
-struct xbt_log_category_s {
-  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;
-  xbt_log_layout_t layout;
-  int additivity;
-};
-
-struct xbt_log_appender_s {
-  void (*do_append) (xbt_log_appender_t this_appender,
-                    char *event);
-  void (*free_) (xbt_log_appender_t this_);
-  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;
-  const char* fileName;
-  const char* functionName;
-  int lineNum;
-  va_list ap;
-};
+#ifdef _XBT_WIN32
+#define XBT_LOG_BUFF_SIZE  16384        /* Size of the static string in which we build the log string */
+#else
+#define XBT_LOG_BUFF_SIZE 2048  /* Size of the static string in which we build the log string */
+#endif
+     struct xbt_log_category_s {
+       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;
+       xbt_log_layout_t layout;
+       int additivity;
+     };
+
+     struct xbt_log_event_s {
+       xbt_log_category_t cat;
+       e_xbt_log_priority_t priority;
+       const char *fileName;
+       const char *functionName;
+       int lineNum;
+       va_list ap;
+       va_list ap_copy;         /* need a copy to launch dynamic layouts when the static ones overflowed */
+#ifdef _XBT_WIN32
+       char *buffer;
+#else
+       char buffer[XBT_LOG_BUFF_SIZE];
+#endif
+     };
 
 /**
  * \ingroup XBT_log_implem
@@ -277,17 +282,8 @@ 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);
-
-/**
- * \ingroup XBT_log_implem  
- * \param cat the category (not only its name, but the variable)
- * \param parent the parent cat
- *
- * Programatically alter a category's parent (don't use).
- */
-XBT_PUBLIC(void) xbt_log_parent_set(xbt_log_category_t cat,
-                               xbt_log_category_t parent);
+                                       e_xbt_log_priority_t
+                                       thresholdPriority);
 
 /**
  * \ingroup XBT_log_implem  
@@ -299,7 +295,7 @@ XBT_PUBLIC(void) xbt_log_parent_set(xbt_log_category_t cat,
  *
  */
 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)
@@ -309,8 +305,8 @@ XBT_PUBLIC(void) xbt_log_appender_set(xbt_log_category_t cat,
  * (the prefered interface is throught xbt_log_control_set())
  *
  */
-XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat, 
-                                   xbt_log_layout_t lay);
+XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat,
+                                    xbt_log_layout_t lay);
 
 /**
  * \ingroup XBT_log_implem  
@@ -323,34 +319,33 @@ XBT_PUBLIC(void) xbt_log_layout_set(xbt_log_category_t cat,
  *
  */
 XBT_PUBLIC(void) xbt_log_additivity_set(xbt_log_category_t cat,
-                                       int additivity);
+                                        int additivity);
 
 /** @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(char*arg);
-XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char*arg);
-XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char*arg);
+XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_simple_new(char *arg);
+XBT_PUBLIC(xbt_log_layout_t) xbt_log_layout_format_new(char *arg);
+XBT_PUBLIC(xbt_log_appender_t) xbt_log_appender_file_new(char *arg);
 
 
 /* ********************************** */
 /* 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);
+                                    const char *fmt,
+                                    ...) _XBT_GNUC_PRINTF(2, 3);
 
-XBT_PUBLIC(int) _xbt_log_cat_init(xbt_log_category_t   category,
-                                 e_xbt_log_priority_t priority);
+XBT_PUBLIC(int) _xbt_log_cat_init(xbt_log_category_t category,
+                                  e_xbt_log_priority_t priority);
 
 
-extern XBT_IMPORT_NO_EXPORT(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT);
+XBT_PUBLIC_DATA(s_xbt_log_category_t) _XBT_LOGV(XBT_LOG_ROOT_CAT);
 
-XBT_LOG_EXTERNAL_CATEGORY(GRAS);
 
-extern xbt_log_appender_t xbt_log_default_appender;
-extern xbt_log_layout_t xbt_log_default_layout;
+     extern xbt_log_appender_t xbt_log_default_appender;
+     extern xbt_log_layout_t xbt_log_default_layout;
 
 /* ********************** */
 /* Public functions again */
@@ -395,14 +390,23 @@ extern xbt_log_layout_t xbt_log_default_layout;
  * code. 
  * Setting the LogEvent's valist member is done inside _log_logEvent.
  */
-
+#ifdef _XBT_WIN32
 #define _XBT_LOG_PRE(catv, priority) do {                       \
      if (_XBT_LOG_ISENABLEDV(catv, priority)) {                  \
          s_xbt_log_event_t _log_ev =                             \
-             {NULL,priority,__FILE__,_XBT_FUNCTION,__LINE__};    \
+             {NULL,priority,__FILE__,_XBT_FUNCTION,__LINE__}; \
+                _log_ev.cat = &(catv);                           \
+                               _log_ev.buffer = (char*) calloc(XBT_LOG_BUFF_SIZE + 1, sizeof(char)); \
+              _xbt_log_event_log(&_log_ev
+#else
+#define _XBT_LOG_PRE(catv, priority) do {                       \
+     if (_XBT_LOG_ISENABLEDV(catv, priority)) {                  \
+         s_xbt_log_event_t _log_ev =                             \
+             {NULL,priority,__FILE__,_XBT_FUNCTION,__LINE__}; \
                 _log_ev.cat = &(catv);                           \
               _xbt_log_event_log(&_log_ev                       \
-              
+
+#endif
 
 #define _XBT_LOG_POST                          \
                         );                      \
@@ -413,19 +417,19 @@ extern xbt_log_layout_t xbt_log_default_layout;
 
 #ifdef XBT_LOG_MAYDAY
 # define CLOG0(c, p, f)                   fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__)
-# define CLOG1(c, p, f,a1)                fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1)               
-# define CLOG2(c, p, f,a1,a2)             fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2)            
-# define CLOG3(c, p, f,a1,a2,a3)          fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3)         
-# define CLOG4(c, p, f,a1,a2,a3,a4)       fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4)      
-# define CLOG5(c, p, f,a1,a2,a3,a4,a5)    fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5)   
+# define CLOG1(c, p, f,a1)                fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1)
+# define CLOG2(c, p, f,a1,a2)             fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2)
+# define CLOG3(c, p, f,a1,a2,a3)          fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3)
+# define CLOG4(c, p, f,a1,a2,a3,a4)       fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4)
+# define CLOG5(c, p, f,a1,a2,a3,a4,a5)    fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5)
 # define CLOG6(c, p, f,a1,a2,a3,a4,a5,a6) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6)
 # define CLOG7(c, p, f,a1,a2,a3,a4,a5,a6,a7) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7)
 # define CLOG8(c, p, f,a1,a2,a3,a4,a5,a6,a7,a8) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7,a8)
 # define CLOG9(c, p, f,a1,a2,a3,a4,a5,a6,a7,a8,a9) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7,a8,a9)
 # define CLOG10(c, p, f,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)
 #else
-# define CLOG0(c, p, f)                   _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f _XBT_LOG_POST                   
-# define CLOG1(c, p, f,a1)                _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f,a1 _XBT_LOG_POST                
+# define CLOG0(c, p, f)                   _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f _XBT_LOG_POST
+# define CLOG1(c, p, f,a1)                _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f,a1 _XBT_LOG_POST
 # define CLOG2(c, p, f,a1,a2)             _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f,a1,a2 _XBT_LOG_POST
 # define CLOG3(c, p, f,a1,a2,a3)          _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f,a1,a2,a3 _XBT_LOG_POST
 # define CLOG4(c, p, f,a1,a2,a3,a4)       _XBT_LOG_PRE(_XBT_LOGV(c),p) ,f,a1,a2,a3,a4 _XBT_LOG_POST
@@ -546,11 +550,11 @@ extern xbt_log_layout_t xbt_log_default_layout;
 
 #ifdef XBT_LOG_MAYDAY
 # define LOG0(p, f)                   fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__)
-# define LOG1(p, f,a1)                fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1)               
-# define LOG2(p, f,a1,a2)             fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2)            
-# define LOG3(p, f,a1,a2,a3)          fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3)         
-# define LOG4(p, f,a1,a2,a3,a4)       fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4)      
-# define LOG5(p, f,a1,a2,a3,a4,a5)    fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5)   
+# define LOG1(p, f,a1)                fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1)
+# define LOG2(p, f,a1,a2)             fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2)
+# define LOG3(p, f,a1,a2,a3)          fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3)
+# define LOG4(p, f,a1,a2,a3,a4)       fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4)
+# define LOG5(p, f,a1,a2,a3,a4,a5)    fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5)
 # define LOG6(p, f,a1,a2,a3,a4,a5,a6) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6)
 # define LOG7(p, f,a1,a2,a3,a4,a5,a6,a7) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7)
 # define LOG8(p, f,a1,a2,a3,a4,a5,a6,a7,a8) fprintf(stderr,"%s:%d:" f "\n",__FILE__,__LINE__,a1,a2,a3,a4,a5,a6,a7,a8)
@@ -705,5 +709,4 @@ extern xbt_log_layout_t xbt_log_default_layout;
 
 
 SG_END_DECL()
-
 #endif /* ! _XBT_LOG_H_ */