Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some work to get it ansi compliant, still much to doo
[simgrid.git] / src / xbt / log.c
index a9c834a..e054e55 100644 (file)
@@ -8,15 +8,13 @@
 /* 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 "gros_interface.h"
 #include "gras_private.h"
 #include <stdarg.h>
 #include <assert.h>
 #include <ctype.h>
 
-#ifndef MIN
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-#endif
-
 typedef struct {
   char *catname;
   gras_log_priority_t thresh;
@@ -26,13 +24,14 @@ static gras_dynar_t *gras_log_settings=NULL;
 static void _free_setting(void *s) {
   gras_log_setting_t *set=(gras_log_setting_t*)s;
   if (set) {
-    free(set->catname);
-    free(set);
+    gras_free(set->catname);
+/*    free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
   }
 }
 
 const char *gras_log_priority_names[8] = {
   "NONE",
+  "TRACE",
   "DEBUG",
   "VERBOSE",
   "INFO",
@@ -46,8 +45,10 @@ gras_log_category_t _GRAS_LOGV(GRAS_LOG_ROOT_CAT) = {
   "root", gras_log_priority_uninitialized, 0,
   NULL, 0
 };
-GRAS_LOG_NEW_SUBCATEGORY(GRAS,GRAS_LOG_ROOT_CAT);
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,GRAS);
+
+GRAS_LOG_NEW_SUBCATEGORY(gras,GRAS_LOG_ROOT_CAT,"All GRAS categories");
+GRAS_LOG_NEW_SUBCATEGORY(gros,GRAS_LOG_ROOT_CAT,"All GROS categories (gras toolbox)");
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,gros,"Loggings from the logging mecanism itself");
 
 
 static void _apply_control(gras_log_category_t* cat) {
@@ -63,7 +64,7 @@ static void _apply_control(gras_log_category_t* cat) {
 
   gras_dynar_foreach(gras_log_settings,cursor,setting) {
     gras_assert0(setting,"Damnit, NULL cat in the list");
-    gras_assert1(setting->catname,"NULL setting(=%p)->catname",setting);
+    gras_assert1(setting->catname,"NULL setting(=%p)->catname",(void*)setting);
 
     if (!strcmp(setting->catname,cat->name)) {
       found = 1;
@@ -73,30 +74,32 @@ static void _apply_control(gras_log_category_t* cat) {
 
       if (cat->threshold <= gras_log_priority_verbose) {
        gras_log_event_t _log_ev = 
-         {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
-          "Apply settings for category '%s': set threshold to %s (=%d)",};
-       _gras_log_event_log(&_log_ev, cat->name,
-                           gras_log_priority_names[cat->threshold], cat->threshold);
+         {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
+       _gras_log_event_log(&_log_ev,
+                "Apply settings for category '%s': set threshold to %s (=%d)",
+                cat->name, 
+                gras_log_priority_names[cat->threshold], cat->threshold);
       }
     }
   }
   if (!found && cat->threshold <= gras_log_priority_verbose) {
     gras_log_event_t _log_ev = 
-      {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__,
-       "Category '%s': inherited threshold = %s (=%d)",};
-    _gras_log_event_log(&_log_ev, cat->name,
+      {cat,gras_log_priority_verbose,__FILE__,__FUNCTION__,__LINE__};
+    _gras_log_event_log(&_log_ev,
+                       "Category '%s': inherited threshold = %s (=%d)",
+                       cat->name,
                        gras_log_priority_names[cat->threshold], cat->threshold);
   }
 
 }
 
-void _gras_log_event_log( gras_log_event_t* ev, ...) {
+void _gras_log_event_log( gras_log_event_t* ev, const char *fmt, ...) {
   gras_log_category_t* cat = ev->cat;
-  va_start(ev->ap, ev);
+  va_start(ev->ap, fmt);
   while(1) {
     gras_log_appender_t* appender = cat->appender;
     if (appender != NULL) {
-      appender->do_append(appender, ev);
+      appender->do_append(appender, ev, fmt);
     }
     if (!cat->willLogToParent)
       break;
@@ -135,7 +138,7 @@ void gras_log_parent_set(gras_log_category_t* cat,
   gras_assert0(cat,"NULL category to be given a parent");
   gras_assert1(parent,"The parent category of %s is NULL",cat->name);
 
-  // unlink from current parent
+  /* unlink from current parent */
   if (cat->threshold != gras_log_priority_uninitialized) {
     gras_log_category_t** cpp = &parent->firstChild;
     while(*cpp != cat && *cpp != NULL) {
@@ -145,20 +148,20 @@ void gras_log_parent_set(gras_log_category_t* cat,
     *cpp = cat->nextSibling;
   }
 
-  // Set new parent
+  /* Set new parent */
   cat->parent = parent;
   cat->nextSibling = parent->firstChild;
   parent->firstChild = cat;
 
-  // Make sure parent is initialized
+  /* Make sure parent is initialized */
   if (parent->threshold == gras_log_priority_uninitialized) {
     _cat_init(parent);
   }
     
-  // Reset priority
+  /* Reset priority */
   cat->threshold = parent->threshold;
   cat->isThreshInherited = 1;
-} // log_setParent
+} /* log_setParent */
 
 static void _set_inherited_thresholds(gras_log_category_t* cat) {
   gras_log_category_t* child = cat->firstChild;
@@ -200,9 +203,9 @@ static gras_error_t _gras_log_parse_setting(const char* control_string,
   gras_assert1(*dot == '.' && *eq == '=',
               "Invalid control string '%s'",control_string);
 
-  if (!strncmp(dot + 1, "thresh", MIN(eq - dot - 1,strlen("thresh")))) {
+  if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
     int i;
-    char *neweq=strdup(eq+1);
+    char *neweq=(char*)strdup(eq+1);
     char *p=neweq-1;
     
     while (*(++p) != '\0') {
@@ -212,27 +215,28 @@ static gras_error_t _gras_log_parse_setting(const char* control_string,
     }
     
     DEBUG1("New priority name = %s",neweq);
-    for (i=0; i<6; i++) {
+    for (i=0; i<gras_log_priority_infinite-1; i++) {
       if (!strncmp(gras_log_priority_names[i],neweq,p-eq)) {
        DEBUG1("This is priority %d",i);
        break;
       }
     }
-    if (i<6) {
+    if (i<gras_log_priority_infinite-1) {
       set->thresh=i;
     } else {
       gras_assert1(FALSE,"Unknown priority name: %s",eq+1);
     }
-    free(neweq);
+    gras_free(neweq);
   } else {
     char buff[512];
-    snprintf(buff,MIN(512,eq - dot - 1),"%s",dot+1);
+    snprintf(buff,min(512,eq - dot - 1),"%s",dot+1);
     gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
   }
-  if (!(set->catname=malloc(dot - name+1)))
+  if (!(set->catname=(char*)gras_malloc(dot - name+1)))
     RAISE_MALLOC;
     
-  strncat(set->catname,name,dot-name);
+  strncpy(set->catname,name,dot-name);
+  set->catname[dot-name]='\0'; /* Just in case */
   DEBUG1("This is for cat '%s'", set->catname);
   return no_error;
 }
@@ -315,12 +319,13 @@ gras_error_t gras_log_control_set(const char* control_string) {
   if (control_string == NULL)
     return no_error;
   if (gras_log_settings == NULL)
-    TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),_free_setting));
+    TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
+                      _free_setting));
 
-  if (!(set = malloc(sizeof(gras_log_setting_t))))
+  if (! (set = gras_new(gras_log_setting_t,1)) )
     RAISE_MALLOC;
 
-  if (!(cs=strdup(control_string)))
+  if (!(cs=(char*)strdup(control_string)))
     RAISE_MALLOC;
   _cleanup_double_spaces(cs);
 
@@ -337,28 +342,29 @@ gras_error_t gras_log_control_set(const char* control_string) {
     }
     errcode = _gras_log_parse_setting(p,set);
     if (errcode != no_error) {
-      free(set);
-      free(cs);
+      gras_free(set);
+      gras_free(cs);
     }
     
-    TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),mismatch_error);
+    TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),
+            mismatch_error);
     if (errcode == mismatch_error) {
       DEBUG0("Store for further application");
-      DEBUG1("push %p to the settings",set);
+      DEBUG1("push %p to the settings",(void*)set);
       TRY(gras_dynar_push(gras_log_settings,&set));
       /* malloc in advance the next slot */
-      if (!(set = malloc(sizeof(gras_log_setting_t)))) { 
-       free(cs);
+      if (!(set = gras_new(gras_log_setting_t,1))) { 
+       gras_free(cs);
        RAISE_MALLOC;
       }
     } else {
       DEBUG0("Apply directly");
-      free(set->catname);
+      gras_free(set->catname);
       gras_log_threshold_set(cat,set->thresh);
     }
   }
-  free(set);
-  free(cs);
+  gras_free(set);
+  gras_free(cs);
   return no_error;
 } 
 
@@ -366,7 +372,8 @@ void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {
   cat->appender = app;
 }
 
-void gras_log_finalize(void);
-void gras_log_finalize(void) {
+void gras_log_exit(void) {
+  VERB0("Exiting log");
   gras_dynar_free(gras_log_settings);
+  VERB0("Exited log");
 }