diff --git a/common/include/villas/log.hpp b/common/include/villas/log.hpp index bd38701b1..ac04c4ba6 100644 --- a/common/include/villas/log.hpp +++ b/common/include/villas/log.hpp @@ -25,6 +25,7 @@ #pragma once #include +#include #include #include @@ -49,6 +50,19 @@ public: using DefaultSink = std::shared_ptr; using DistSink = std::shared_ptr; + 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 expressions; + public: Log(Level level = Level::info); diff --git a/common/lib/log.cpp b/common/lib/log.cpp index 5f5ca8c86..9f97604bf 100644 --- a/common/lib/log.cpp +++ b/common/lib/log.cpp @@ -23,6 +23,8 @@ #include #include +#include + #include #include @@ -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; +}