Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Objectify MSG task send
[simgrid.git] / src / xbt / log.cpp
index b82a8b6..891f370 100644 (file)
@@ -5,24 +5,14 @@
 /* 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 <algorithm>
-#include <cctype>
-#include <cstdarg>
-#include <cstdio> /* snprintf */
-#include <cstdlib>
-#include <mutex>
-
-#include "src/internal_config.h"
-
 #include "src/xbt_modinter.h"
-
 #include "src/xbt/log_private.hpp"
 #include "xbt/asserts.h"
 #include "xbt/dynar.h"
-#include "xbt/ex.h"
-#include "xbt/misc.h"
 #include "xbt/str.h"
-#include "xbt/sysdep.h"
+
+#include <mutex>
+#include <string>
 
 int xbt_log_no_loc = 0; /* if set to true (with --log=no_loc), file localization will be omitted (for tesh tests) */
 static std::recursive_mutex* log_cat_init_mutex = nullptr;
@@ -51,9 +41,9 @@ static void _free_setting(void *s)
 {
   xbt_log_setting_t set = *(xbt_log_setting_t *) s;
   if (set) {
-    free(set->catname);
-    free(set->fmt);
-    free(set);
+    xbt_free(set->catname);
+    xbt_free(set->fmt);
+    xbt_free(set);
   }
 }
 
@@ -149,12 +139,12 @@ static void log_cat_exit(xbt_log_category_t cat)
   if (cat->appender) {
     if (cat->appender->free_)
       cat->appender->free_(cat->appender);
-    free(cat->appender);
+    xbt_free(cat->appender);
   }
   if (cat->layout) {
     if (cat->layout->free_)
       cat->layout->free_(cat->layout);
-    free(cat->layout);
+    xbt_free(cat->layout);
   }
 
   for (child = cat->firstChild; child != nullptr; child = child->nextSibling)
@@ -278,15 +268,12 @@ static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_
 int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority)
 {
   DISABLE_XBT_LOG_CAT_INIT();
+  if (category->initialized)
+    return priority >= category->threshold;
+
   if (log_cat_init_mutex != nullptr)
     log_cat_init_mutex->lock();
 
-  if (category->initialized) {
-    if (log_cat_init_mutex != nullptr)
-      log_cat_init_mutex->unlock();
-    return priority >= category->threshold;
-  }
-
   unsigned int cursor;
   xbt_log_setting_t setting = nullptr;
 
@@ -308,24 +295,15 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
     xbt_log_parent_set(category, category->parent);
 
     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
-      char *buf;
-      char* res              = nullptr;
+      std::string res;
       xbt_log_category_t cpp = category->parent->firstChild;
       while (cpp) {
-        if (res) {
-          buf = bprintf("%s %s", res, cpp->name);
-          free(res);
-          res = buf;
-        } else {
-          res = xbt_strdup(cpp->name);
-        }
+        res += std::string(" ") + cpp->name;
         cpp = cpp->nextSibling;
       }
 
-      XBT_DEBUG("Children of %s: %s; nextSibling: %s", category->parent->name, res,
-             (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
-
-      free(res);
+      XBT_DEBUG("Children of %s:%s; nextSibling: %s", category->parent->name, res.c_str(),
+                (category->parent->nextSibling ? category->parent->nextSibling->name : "none"));
     }
   }
 
@@ -466,7 +444,7 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
       THROWF(arg_error, 0,
              "Unknown priority name: %s (must be one of: trace,debug,verbose,info,warning,error,critical)", eq + 1);
     }
-    free(neweq);
+    xbt_free(neweq);
   } else if (!strncmp(dot + 1, "add", (size_t) (eq - dot - 1)) ||
              !strncmp(dot + 1, "additivity", (size_t) (eq - dot - 1))) {
     char *neweq = xbt_strdup(eq + 1);
@@ -482,7 +460,7 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
     } else {
       set->additivity = 0;
     }
-    free(neweq);
+    xbt_free(neweq);
   } else if (!strncmp(dot + 1, "app", (size_t) (eq - dot - 1)) ||
              !strncmp(dot + 1, "appender", (size_t) (eq - dot - 1))) {
     char *neweq = xbt_strdup(eq + 1);
@@ -496,7 +474,7 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
     } else {
       THROWF(arg_error, 0, "Unknown appender log type: '%s'", neweq);
     }
-    free(neweq);
+    xbt_free(neweq);
   } else if (!strncmp(dot + 1, "fmt", (size_t) (eq - dot - 1))) {
     set->fmt = xbt_strdup(eq + 1);
   } else {
@@ -606,7 +584,7 @@ void xbt_log_appender_set(xbt_log_category_t cat, xbt_log_appender_t app)
   if (cat->appender) {
     if (cat->appender->free_)
       cat->appender->free_(cat->appender);
-    free(cat->appender);
+    xbt_free(cat->appender);
   }
   cat->appender = app;
 }
@@ -622,7 +600,7 @@ void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
     if (cat->layout->free_) {
       cat->layout->free_(cat->layout);
     }
-    free(cat->layout);
+    xbt_free(cat->layout);
   }
   cat->layout = lay;
   xbt_log_additivity_set(cat, 0);