Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[simgrid.git] / src / xbt / log.c
index fb86a02..eb15e53 100644 (file)
@@ -9,7 +9,7 @@
    under the terms of the license (GNU LGPL) which comes with this package. */
 
 
-#include "gros_interface.h"
+#include "xbt_interface.h"
 #include "gras_private.h"
 #include <stdarg.h>
 #include <assert.h>
@@ -24,8 +24,8 @@ 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); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log
+    gras_free(set->catname);
+/*    free(set); FIXME: uncommenting this leads to segfault when more than one chunk is passed as gras-log */
   }
 }
 
@@ -45,9 +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_SUBCATEGORY(tbx,GRAS);
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(log,tbx);
+
+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;
@@ -137,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) {
@@ -147,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;
@@ -182,13 +183,13 @@ void gras_log_threshold_set(gras_log_category_t* cat,
   _set_inherited_thresholds(cat);
 }
 
-static gras_error_t _gras_log_parse_setting(const char* control_string,
-                                           gras_log_setting_t *set) {
+static void _gras_log_parse_setting(const char* control_string,
+                                   gras_log_setting_t *set) {
   const char *name, *dot, *eq;
   
   set->catname=NULL;
   if (!*control_string) 
-    return no_error;
+    return;
   DEBUG1("Parse log setting '%s'",control_string);
 
   control_string += strspn(control_string, " ");
@@ -204,7 +205,7 @@ static gras_error_t _gras_log_parse_setting(const char* control_string,
 
   if (!strncmp(dot + 1, "thresh", min(eq - dot - 1,strlen("thresh")))) {
     int i;
-    char *neweq=strdup(eq+1);
+    char *neweq=gras_strdup(eq+1);
     char *p=neweq-1;
     
     while (*(++p) != '\0') {
@@ -225,19 +226,17 @@ static gras_error_t _gras_log_parse_setting(const char* control_string,
     } 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);
     gras_assert1(FALSE,"Unknown setting of the log category: %s",buff);
   }
-  if (!(set->catname=malloc(dot - name+1)))
-    RAISE_MALLOC;
+  set->catname=(char*)gras_malloc(dot - name+1);
     
   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;
 }
 
 static gras_error_t _gras_log_cat_searchsub(gras_log_category_t *cat,char *name,gras_log_category_t**whereto) {
@@ -291,7 +290,6 @@ static void _cleanup_double_spaces(char *s) {
 /**
  * gras_log_control_set:
  * @cs: What to parse
- * @Returns: malloc_error or no_error
  *
  * Typically passed a command-line argument. The string has the syntax:
  *
@@ -307,7 +305,7 @@ static void _cleanup_double_spaces(char *s) {
  * This routine may only be called once and that must be before any other
  * logging command! Typically, this is done from main().
  */
-gras_error_t gras_log_control_set(const char* control_string) {
+void gras_log_control_set(const char* control_string) {
   gras_error_t errcode;
   gras_log_setting_t *set;
   char *cs;
@@ -316,16 +314,14 @@ gras_error_t gras_log_control_set(const char* control_string) {
   
   DEBUG1("Parse log settings '%s'",control_string);
   if (control_string == NULL)
-    return no_error;
+    return;
   if (gras_log_settings == NULL)
-    TRY(gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
-                      _free_setting));
+    gras_dynar_new(&gras_log_settings,sizeof(gras_log_setting_t*),
+                  _free_setting);
 
-  if (!(set = malloc(sizeof(gras_log_setting_t))))
-    RAISE_MALLOC;
+  set = gras_new(gras_log_setting_t,1);
+  cs=gras_strdup(control_string);
 
-  if (!(cs=strdup(control_string)))
-    RAISE_MALLOC;
   _cleanup_double_spaces(cs);
 
   while (!done) {
@@ -339,32 +335,23 @@ gras_error_t gras_log_control_set(const char* control_string) {
       p=cs;
       done = 1;
     }
-    errcode = _gras_log_parse_setting(p,set);
-    if (errcode != no_error) {
-      free(set);
-      free(cs);
-    }
+    _gras_log_parse_setting(p,set);
     
-    TRYCATCH(_gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat),
-            mismatch_error);
+    errcode = _gras_log_cat_searchsub(&_GRAS_LOGV(root),set->catname,&cat);
     if (errcode == mismatch_error) {
       DEBUG0("Store for further application");
-      DEBUG1("push %p to the settings",set);
-      TRY(gras_dynar_push(gras_log_settings,&set));
+      DEBUG1("push %p to the settings",(void*)set);
+      gras_dynar_push(gras_log_settings,&set);
       /* malloc in advance the next slot */
-      if (!(set = malloc(sizeof(gras_log_setting_t)))) { 
-       free(cs);
-       RAISE_MALLOC;
-      }
+      set = gras_new(gras_log_setting_t,1);
     } else {
       DEBUG0("Apply directly");
-      free(set->catname);
+      gras_free(set->catname);
       gras_log_threshold_set(cat,set->thresh);
     }
   }
-  free(set);
-  free(cs);
-  return no_error;
+  gras_free(set);
+  gras_free(cs);
 } 
 
 void gras_log_appender_set(gras_log_category_t* cat, gras_log_appender_t* app) {