Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "Assert that the log categories are unique"
[simgrid.git] / src / xbt / log.cpp
index e4525c3..4819878 100644 (file)
@@ -1,6 +1,6 @@
 /* log - a generic logging facility in the spirit of log4j                  */
 
-/* Copyright (c) 2004-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2021. 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. */
@@ -37,9 +37,14 @@ struct xbt_log_setting_t {
   xbt_log_appender_t appender = nullptr;
 };
 
-static std::vector<xbt_log_setting_t> xbt_log_settings;
+// This function is here to avoid static initialization order fiasco
+static auto& xbt_log_settings()
+{
+  static std::vector<xbt_log_setting_t> value;
+  return value;
+}
 
-const std::array<const char*, 8> xbt_log_priority_names{
+constexpr std::array<const char*, 8> xbt_log_priority_names{
     {"NONE", "TRACE", "DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "CRITICAL"}};
 
 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
@@ -161,7 +166,7 @@ void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
       xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
 
       /* First, try with a static buffer */
-      int done = 0;
+      bool done = false;
       std::array<char, XBT_LOG_STATIC_BUFFER_SIZE> buff;
       ev->buffer      = buff.data();
       ev->buffer_size = buff.size();
@@ -286,11 +291,11 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
   }
 
   /* Apply the control */
-  auto iset = std::find_if(begin(xbt_log_settings), end(xbt_log_settings),
+  auto iset = std::find_if(begin(xbt_log_settings()), end(xbt_log_settings()),
                            [category](const xbt_log_setting_t& s) { return s.catname == category->name; });
-  if (iset != xbt_log_settings.end()) {
+  if (iset != xbt_log_settings().end()) {
     _xbt_log_cat_apply_set(category, *iset);
-    xbt_log_settings.erase(iset);
+    xbt_log_settings().erase(iset);
   } else {
     XBT_DEBUG("Category '%s': inherited threshold = %s (=%d)", category->name,
               xbt_log_priority_names[category->threshold], category->threshold);
@@ -500,7 +505,7 @@ void xbt_log_control_set(const char *control_string)
     } else {
       XBT_DEBUG("Store for further application");
       XBT_DEBUG("push %p to the settings", &set);
-      xbt_log_settings.emplace_back(std::move(set));
+      xbt_log_settings().emplace_back(std::move(set));
     }
   }
   xbt_dynar_free(&set_strings);