Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace macros by C++ constructs.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 16 Jan 2019 20:21:16 +0000 (21:21 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 16 Jan 2019 22:02:11 +0000 (23:02 +0100)
src/xbt/log.cpp
src/xbt/xbt_log_appender_file.cpp
src/xbt/xbt_log_layout_format.cpp
src/xbt/xbt_log_layout_simple.cpp

index 38dbba0..b82a8b6 100644 (file)
@@ -5,11 +5,12 @@
 /* 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 <ctype.h>
+#include <algorithm>
+#include <cctype>
+#include <cstdarg>
+#include <cstdio> /* snprintf */
+#include <cstdlib>
 #include <mutex>
-#include <stdarg.h>
-#include <stdio.h>  /* snprintf */
-#include <stdlib.h> /* snprintf */
 
 #include "src/internal_config.h"
 
 #include "xbt/str.h"
 #include "xbt/sysdep.h"
 
-#ifndef MIN
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#endif
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
 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;
 
@@ -38,8 +32,8 @@ static std::recursive_mutex* log_cat_init_mutex = nullptr;
  *  For more information, please refer to @ref outcomes_logs Section.
  */
 
-xbt_log_appender_t xbt_log_default_appender = NULL;     /* set in log_init */
-xbt_log_layout_t xbt_log_default_layout = NULL; /* set in log_init */
+xbt_log_appender_t xbt_log_default_appender = nullptr; /* set in log_init */
+xbt_log_layout_t xbt_log_default_layout     = nullptr; /* set in log_init */
 
 typedef struct {
   char *catname;
@@ -51,7 +45,7 @@ typedef struct {
 
 typedef s_xbt_log_setting_t* xbt_log_setting_t;
 
-static xbt_dynar_t xbt_log_settings = NULL;
+static xbt_dynar_t xbt_log_settings = nullptr;
 
 static void _free_setting(void *s)
 {
@@ -77,12 +71,17 @@ const char *xbt_log_priority_names[8] = {
 };
 
 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
-  NULL /*parent */ , NULL /* firstChild */ , NULL /* nextSibling */ ,
-      "root", "The common ancestor for all categories",
-      0 /*initialized */, xbt_log_priority_uninitialized /* threshold */ ,
-      0 /* isThreshInherited */ ,
-      NULL /* appender */ , NULL /* layout */ ,
-      0                         /* additivity */
+    nullptr /*parent */,
+    nullptr /* firstChild */,
+    nullptr /* nextSibling */,
+    "root",
+    "The common ancestor for all categories",
+    0 /*initialized */,
+    xbt_log_priority_uninitialized /* threshold */,
+    0 /* isThreshInherited */,
+    nullptr /* appender */,
+    nullptr /* layout */,
+    0 /* additivity */
 };
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism itself");
@@ -91,8 +90,8 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(log, xbt, "Loggings from the logging mechanism i
    which were already created (damnit. Too slow little beetle) */
 void xbt_log_preinit(void)
 {
-  xbt_log_default_appender = xbt_log_appender_file_new(NULL);
-  xbt_log_default_layout = xbt_log_layout_simple_new(NULL);
+  xbt_log_default_appender             = xbt_log_appender_file_new(nullptr);
+  xbt_log_default_layout               = xbt_log_layout_simple_new(nullptr);
   _XBT_LOGV(XBT_LOG_ROOT_CAT).appender = xbt_log_default_appender;
   _XBT_LOGV(XBT_LOG_ROOT_CAT).layout = xbt_log_default_layout;
   log_cat_init_mutex                   = new std::recursive_mutex();
@@ -130,7 +129,7 @@ void xbt_log_init(int *argc, char **argv)
     }
   }
   if (j < *argc) {
-    argv[j] = NULL;
+    argv[j] = nullptr;
     *argc = j;
   }
 
@@ -158,7 +157,7 @@ static void log_cat_exit(xbt_log_category_t cat)
     free(cat->layout);
   }
 
-  for (child = cat->firstChild; child != NULL; child = child->nextSibling)
+  for (child = cat->firstChild; child != nullptr; child = child->nextSibling)
     log_cat_exit(child);
 }
 
@@ -171,10 +170,10 @@ void xbt_log_postexit(void)
 }
 
 /* Size of the static string in which we build the log string */
-#define XBT_LOG_STATIC_BUFFER_SIZE 2048
+static constexpr size_t XBT_LOG_STATIC_BUFFER_SIZE = 2048;
 /* Minimum size of the dynamic string in which we build the log string
    (should be greater than XBT_LOG_STATIC_BUFFER_SIZE) */
-#define XBT_LOG_DYNAMIC_BUFFER_SIZE 4096
+static constexpr size_t XBT_LOG_DYNAMIC_BUFFER_SIZE = 4096;
 
 void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
 {
@@ -187,7 +186,7 @@ void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
   while (1) {
     xbt_log_appender_t appender = cat->appender;
 
-    if (appender != NULL) {
+    if (appender != nullptr) {
       xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name);
 
       /* First, try with a static buffer */
@@ -234,8 +233,7 @@ void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
  * To circumvent the problem, we define the macro DISABLE_XBT_LOG_CAT_INIT() to hide the real _xbt_log_cat_init(). The
  * macro has to be called at the beginning of the affected functions.
  */
-static int fake_xbt_log_cat_init(xbt_log_category_t XBT_ATTRIB_UNUSED category,
-                                 e_xbt_log_priority_t XBT_ATTRIB_UNUSED priority)
+static int fake_xbt_log_cat_init(xbt_log_category_t, e_xbt_log_priority_t)
 {
   return 0;
 }
@@ -267,7 +265,7 @@ static void _xbt_log_cat_apply_set(xbt_log_category_t category, xbt_log_setting_
   if (setting->appender) {
     xbt_log_appender_set(category, setting->appender);
     if (!category->layout)
-      xbt_log_layout_set(category, xbt_log_layout_simple_new(NULL));
+      xbt_log_layout_set(category, xbt_log_layout_simple_new(nullptr));
     category->additivity = 0;
     XBT_DEBUG("Set %p as appender of category '%s'", setting->appender, category->name);
   }
@@ -280,17 +278,17 @@ 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 (log_cat_init_mutex != NULL)
+  if (log_cat_init_mutex != nullptr)
     log_cat_init_mutex->lock();
 
   if (category->initialized) {
-    if (log_cat_init_mutex != NULL)
+    if (log_cat_init_mutex != nullptr)
       log_cat_init_mutex->unlock();
     return priority >= category->threshold;
   }
 
   unsigned int cursor;
-  xbt_log_setting_t setting = NULL;
+  xbt_log_setting_t setting = nullptr;
 
   XBT_DEBUG("Initializing category '%s' (firstChild=%s, nextSibling=%s)", category->name,
          (category->firstChild ? category->firstChild->name : "none"),
@@ -311,7 +309,7 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
 
     if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
       char *buf;
-      char *res = NULL;
+      char* res              = nullptr;
       xbt_log_category_t cpp = category->parent->firstChild;
       while (cpp) {
         if (res) {
@@ -354,7 +352,7 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
   }
 
   category->initialized = 1;
-  if (log_cat_init_mutex != NULL)
+  if (log_cat_init_mutex != nullptr)
     log_cat_init_mutex->unlock();
   return priority >= category->threshold;
 }
@@ -368,7 +366,7 @@ void xbt_log_parent_set(xbt_log_category_t cat, xbt_log_category_t parent)
   if (cat->initialized) {
     xbt_log_category_t *cpp = &cat->parent->firstChild;
 
-    while (*cpp != cat && *cpp != NULL) {
+    while (*cpp != cat && *cpp != nullptr) {
       cpp = &(*cpp)->nextSibling;
     }
 
@@ -393,7 +391,7 @@ static void _set_inherited_thresholds(xbt_log_category_t cat)
 {
   xbt_log_category_t child = cat->firstChild;
 
-  for (; child != NULL; child = child->nextSibling) {
+  for (; child != nullptr; child = child->nextSibling) {
     if (child->isThreshInherited) {
       if (cat != &_XBT_LOGV(log))
         XBT_VERB("Set category threshold of %s to %s (=%d)",
@@ -417,11 +415,11 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
   const char *orig_control_string = control_string;
   xbt_log_setting_t set = xbt_new(s_xbt_log_setting_t, 1);
 
-  set->catname = NULL;
+  set->catname    = nullptr;
   set->thresh = xbt_log_priority_uninitialized;
-  set->fmt = NULL;
+  set->fmt        = nullptr;
   set->additivity = -1;
-  set->appender = NULL;
+  set->appender   = nullptr;
 
   if (!*control_string)
     return set;
@@ -503,7 +501,7 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string)
     set->fmt = xbt_strdup(eq + 1);
   } else {
     char buff[512];
-    snprintf(buff, MIN(512, eq - dot), "%s", dot + 1);
+    snprintf(buff, std::min<int>(512, eq - dot), "%s", dot + 1);
     xbt_die("Unknown setting of the log category: '%s'", buff);
   }
   set->catname = (char *) xbt_malloc(dot - name + 1);
@@ -526,14 +524,14 @@ static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, char *n
   if (!strcmp(cat->name, name))
     return cat;
 
-  for (child = cat->firstChild; child != NULL; child = child->nextSibling) {
+  for (child = cat->firstChild; child != nullptr; child = child->nextSibling) {
     XBT_DEBUG("Dig into %s", child->name);
     res = _xbt_log_cat_searchsub(child, name);
     if (res)
       return res;
   }
 
-  return NULL;
+  return nullptr;
 }
 
 /**
@@ -574,7 +572,7 @@ void xbt_log_control_set(const char *control_string)
     return;
   }
   /* some initialization if this is the first time that this get called */
-  if (xbt_log_settings == NULL)
+  if (xbt_log_settings == nullptr)
     xbt_log_settings = xbt_dynar_new(sizeof(xbt_log_setting_t), _free_setting);
 
   /* split the string, and remove empty entries */
@@ -618,7 +616,7 @@ void xbt_log_layout_set(xbt_log_category_t cat, xbt_log_layout_t lay)
   DISABLE_XBT_LOG_CAT_INIT();
   if (!cat->appender) {
     XBT_VERB ("No appender to category %s. Setting the file appender as default", cat->name);
-    xbt_log_appender_set(cat, xbt_log_appender_file_new(NULL));
+    xbt_log_appender_set(cat, xbt_log_appender_file_new(nullptr));
   }
   if (cat->layout) {
     if (cat->layout->free_) {
@@ -715,8 +713,8 @@ static void xbt_log_help_categories_rec(xbt_log_category_t category, const char
     child_prefix = xbt_strdup(prefix);
   }
 
-  xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), NULL);
-  for (cat = category ; cat != NULL; cat = cat->nextSibling)
+  xbt_dynar_t dynar = xbt_dynar_new(sizeof(xbt_log_category_t), nullptr);
+  for (cat = category; cat != nullptr; cat = cat->nextSibling)
     xbt_dynar_push_as(dynar, xbt_log_category_t, cat);
 
   xbt_dynar_sort(dynar, xbt_log_cat_cmp);
index 7d9313e..c082050 100644 (file)
@@ -9,9 +9,9 @@
 #include "src/internal_config.h"
 #include "src/xbt/log_private.hpp"
 #include "xbt/sysdep.h"
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
+#include <cerrno>
+#include <cstdio>
+#include <cstring>
 
 static void append_file(xbt_log_appender_t this_, char *str) {
   fputs(str, (FILE *) this_->data);
@@ -29,7 +29,7 @@ xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
   res->free_             = &free_;
   if (arg) {
     res->data = (void *) fopen(arg, "w");
-    if (res->data == NULL)
+    if (res->data == nullptr)
       xbt_die("Cannot open file: %s: %s", arg, strerror(errno));
   } else {
     res->data = (void *) stderr;
@@ -45,15 +45,15 @@ struct xbt_log_append2_file_s {
 };
 typedef struct xbt_log_append2_file_s* xbt_log_append2_file_t;
 
-#define APPEND2_END_TOKEN             "\n[End of log]\n"
-#define APPEND2_END_TOKEN_CLEAR "\n                   "
+static constexpr const char* APPEND2_END_TOKEN       = "\n[End of log]\n";
+static constexpr const char* APPEND2_END_TOKEN_CLEAR = "\n                   ";
 
 static void open_append2_file(xbt_log_append2_file_t data){
   if(data->count<0) {
     //Roll
     if (!data->file) {
       data->file= fopen(data->filename, "w");
-      if (data->file == NULL)
+      if (data->file == nullptr)
         xbt_die("Cannot open file: %s: %s", data->filename, strerror(errno));
     } else {
       fputs(APPEND2_END_TOKEN_CLEAR,data->file);
@@ -73,7 +73,7 @@ static void open_append2_file(xbt_log_append2_file_t data){
     snprintf(newname,511,"%s%i%s",pre,data->count,post);
     data->count++;
     data->file= fopen(newname, "w");
-    if (data->file == NULL)
+    if (data->file == nullptr)
       xbt_die("Cannot open file: %s: %s", newname, strerror(errno));
     xbt_free(pre);
   }
@@ -114,7 +114,7 @@ xbt_log_appender_t xbt_log_appender2_file_new(char *arg,int roll) {
   xbt_assert(arg);
   char* buf=xbt_strdup(arg);
   char* sep=strchr(buf,':');
-  xbt_assert(sep != NULL);
+  xbt_assert(sep != nullptr);
   data->filename=xbt_strdup(sep+1);
   *sep='\0';
   char *endptr;
index 3243b77..5bbf144 100644 (file)
@@ -9,26 +9,20 @@
 #include "simgrid/msg.h" /* MSG_get_clock */
 #include "src/xbt/log_private.hpp"
 #include "xbt/sysdep.h"
-#include <stdio.h>
-
-#ifndef MIN
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#endif
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
+#include <algorithm>
+#include <cstdio>
 
 extern const char *xbt_log_priority_names[8];
 
-#define ERRMSG                                                          \
-  "Unknown %%%c sequence in layout format (%s).\n"                      \
-  "Known sequences:\n"                                                  \
-  "  what:        %%m: user message  %%c: log category  %%p: log priority\n" \
-  "  where:\n"                                                          \
-  "    source:    %%F: file          %%L: line          %%M: function  %%l: location (%%F:%%L)\n" \
-  "    runtime:   %%h: hostname      %%t: thread        %%P: process   %%i: PID\n" \
-  "  when:        %%d: date          %%r: app. age\n"                   \
-  "  other:       %%%%: %%             %%n: new line      %%e: plain space\n"
+static constexpr const char* ERRMSG =
+    "Unknown %%%c sequence in layout format (%s).\n"
+    "Known sequences:\n"
+    "  what:        %%m: user message  %%c: log category  %%p: log priority\n"
+    "  where:\n"
+    "    source:    %%F: file          %%L: line          %%M: function  %%l: location (%%F:%%L)\n"
+    "    runtime:   %%h: hostname      %%t: thread        %%P: process   %%i: PID\n"
+    "  when:        %%d: date          %%r: app. age\n"
+    "  other:       %%%%: %%             %%n: new line      %%e: plain space\n";
 
 #define check_overflow(len)                                             \
   if ((rem_size -= (len)) > 0) {                                        \
@@ -73,7 +67,7 @@ extern const char *xbt_log_priority_names[8];
 #define show_int(data)    show_it(data, "d")
 #define show_double(data) show_it(data, "f")
 
-static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const char *msg_fmt)
+static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, const charmsg_fmt)
 {
   char *p = ev->buffer;
   int rem_size = ev->buffer_size;
@@ -139,7 +133,7 @@ static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, co
             int sz;
             set_sz_from_precision();
             int len = snprintf(p, sz, "%s:%d", ev->fileName, ev->lineNum);
-            check_overflow(MIN(sz, len));
+            check_overflow(std::min(sz, len));
             break;
           }
           case 'L': /* line number; LOG4J compliant */
@@ -161,7 +155,7 @@ static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, co
             va_copy(ap, ev->ap);
             int len = vsnprintf(p, sz, msg_fmt, ap);
             va_end(ap);
-            check_overflow(MIN(sz, len));
+            check_overflow(std::min(sz, len));
             break;
           }
           default:
index f4179ae..ae1ab91 100644 (file)
@@ -10,7 +10,7 @@
 
 #include "simgrid/host.h" /* sg_host_self_get_name */
 #include "simgrid/msg.h"  /* MSG_get_clock */
-#include <stdio.h>
+#include <cstdio>
 
 extern const char *xbt_log_priority_names[8];
 extern int xbt_log_no_loc;
@@ -21,7 +21,7 @@ extern int xbt_log_no_loc;
   } else                                                                \
     return 0
 
-static int xbt_log_layout_simple_doit(XBT_ATTRIB_UNUSED xbt_log_layout_t l, xbt_log_event_t ev, const char* fmt)
+static int xbt_log_layout_simple_doit(xbt_log_layout_t, xbt_log_event_t ev, const char* fmt)
 {
   char *p = ev->buffer;
   int rem_size = ev->buffer_size;
@@ -68,7 +68,7 @@ static int xbt_log_layout_simple_doit(XBT_ATTRIB_UNUSED xbt_log_layout_t l, xbt_
   return 1;
 }
 
-xbt_log_layout_t xbt_log_layout_simple_new(XBT_ATTRIB_UNUSED char* arg)
+xbt_log_layout_t xbt_log_layout_simple_new(char*)
 {
   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t, 1);
   res->do_layout       = &xbt_log_layout_simple_doit;