/* 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 <mutex>
-#include <stdarg.h>
-#include <stdio.h> /* snprintf */
-#include <stdlib.h> /* snprintf */
-
-#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"
-#ifndef MIN
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#endif
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
+#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;
* 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;
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)
{
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);
}
}
};
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");
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();
}
}
if (j < *argc) {
- argv[j] = NULL;
+ argv[j] = nullptr;
*argc = j;
}
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 != NULL; child = child->nextSibling)
+ for (child = cat->firstChild; child != nullptr; child = child->nextSibling)
log_cat_exit(child);
}
}
/* 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, ...)
{
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 */
* 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;
}
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);
}
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)
- log_cat_init_mutex->lock();
-
- if (category->initialized) {
- if (log_cat_init_mutex != NULL)
- log_cat_init_mutex->unlock();
+ if (category->initialized)
return priority >= category->threshold;
- }
+
+ if (log_cat_init_mutex != nullptr)
+ log_cat_init_mutex->lock();
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"),
xbt_log_parent_set(category, category->parent);
if (XBT_LOG_ISENABLED(log, xbt_log_priority_debug)) {
- char *buf;
- char *res = NULL;
+ 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"));
}
}
}
category->initialized = 1;
- if (log_cat_init_mutex != NULL)
+ if (log_cat_init_mutex != nullptr)
log_cat_init_mutex->unlock();
return priority >= category->threshold;
}
if (cat->initialized) {
xbt_log_category_t *cpp = &cat->parent->firstChild;
- while (*cpp != cat && *cpp != NULL) {
+ while (*cpp != cat && *cpp != nullptr) {
cpp = &(*cpp)->nextSibling;
}
{
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)",
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;
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);
} 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);
} 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 {
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);
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;
}
/**
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 */
if (cat->appender) {
if (cat->appender->free_)
cat->appender->free_(cat->appender);
- free(cat->appender);
+ xbt_free(cat->appender);
}
cat->appender = app;
}
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_) {
cat->layout->free_(cat->layout);
}
- free(cat->layout);
+ xbt_free(cat->layout);
}
cat->layout = lay;
xbt_log_additivity_set(cat, 0);
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);