1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

log: add support for glob style logger expressions

This commit is contained in:
Steffen Vogel 2021-07-09 15:29:31 +02:00
parent 3ae2285ea4
commit d617568b6a
2 changed files with 45 additions and 16 deletions

View file

@ -25,6 +25,7 @@
#pragma once
#include <string>
#include <list>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/dist_sink.h>
@ -49,6 +50,19 @@ public:
using DefaultSink = std::shared_ptr<spdlog::sinks::stderr_color_sink_mt>;
using DistSink = std::shared_ptr<spdlog::sinks::dist_sink_mt>;
class Expression {
public:
std::string name;
Level level;
Expression(const std::string &n, Level lvl) :
name(n),
level(lvl)
{ }
Expression(json_t *json);
};
protected:
DistSink sinks;
DefaultSink sink;
@ -58,6 +72,8 @@ protected:
std::string pattern; /**< Logging format. */
std::string prefix; /**< Prefix each line with this string. */
std::list<Expression> expressions;
public:
Log(Level level = Level::info);

View file

@ -23,6 +23,8 @@
#include <list>
#include <algorithm>
#include <fnmatch.h>
#include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
@ -74,6 +76,11 @@ Logger Log::get(const std::string &name)
logger->set_level(level);
logger->set_pattern(prefix + pattern);
for (auto &expr : expressions) {
if (!fnmatch(expr.name.c_str(), name.c_str(), FNM_EXTMATCH))
logger->set_level(expr.level);
}
spdlog::register_logger(logger);
}
@ -125,22 +132,8 @@ void Log::parse(json_t *json)
size_t i;
json_t *json_expression;
json_array_foreach(json_expressions, i, json_expression) {
const char *name;
const char *lvl;
ret = json_unpack_ex(json_expression, &err, JSON_STRICT, "{ s: s, s: s }",
"name", &name,
"level", &lvl
);
if (ret)
throw ConfigError(json_expression, err, "node-config-logging-expressions");
Logger logger = get(name);
auto level = spdlog::level::from_str(lvl);
logger->set_level(level);
}
json_array_foreach(json_expressions, i, json_expression)
expressions.emplace_back(json_expression);
}
}
@ -182,3 +175,23 @@ std::string Log::getLevelName() const
return std::string(sv.data());
}
Log::Expression::Expression(json_t *json)
{
int ret;
const char *nme;
const char *lvl;
json_error_t err;
ret = json_unpack_ex(json, &err, JSON_STRICT, "{ s: s, s: s }",
"name", &nme,
"level", &lvl
);
if (ret)
throw ConfigError(json, err, "node-config-logging-expressions");
level = spdlog::level::from_str(lvl);
name = nme;
}