Commit 1bd1e755 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

log: improve readability of log outputs

parent dd9304ca
......@@ -108,7 +108,7 @@ protected:
std::stringstream ss;
ss << std::runtime_error::what() << std::endl;
ss << " Please consult the user documentation for details: " << std::endl;
ss << std::endl << " Please consult the user documentation for details: " << std::endl;
ss << " " << docUri();
if (error.position >= 0) {
......
......@@ -49,6 +49,7 @@ public:
using Level = spdlog::level::level_enum;
using DefaultSink = std::shared_ptr<spdlog::sinks::stderr_color_sink_mt>;
using DistSink = std::shared_ptr<spdlog::sinks::dist_sink_mt>;
using Formatter = std::shared_ptr<spdlog::pattern_formatter>;
class Expression {
public:
......@@ -66,6 +67,7 @@ public:
protected:
DistSink sinks;
DefaultSink sink;
Formatter formatter;
Level level;
......@@ -85,7 +87,7 @@ public:
Logger get(const std::string &name);
void setPattern(const std::string &pattern);
void setFormatter(const std::string &pattern);
void setLevel(Level lvl);
void setLevel(const std::string &lvl);
......
......@@ -22,6 +22,7 @@
#include <list>
#include <algorithm>
#include <map>
#include <fnmatch.h>
......@@ -37,9 +38,40 @@ using namespace villas;
/** The global log instance */
Log villas::logging;
static std::map<spdlog::level::level_enum, std::string> levelNames = {
{ spdlog::level::trace, "trc" },
{ spdlog::level::debug, "dbg" },
{ spdlog::level::info, "info" },
{ spdlog::level::warn, "warn" },
{ spdlog::level::err, "err" },
{ spdlog::level::critical, "crit" },
{ spdlog::level::off, "off" }
};
class CustomLevelFlag : public spdlog::custom_flag_formatter {
public:
void format(const spdlog::details::log_msg &msg, const std::tm &, spdlog::memory_buf_t &dest) override
{
auto lvl = levelNames[msg.level];
auto lvlpad = std::string(padinfo_.width_ - lvl.size(), ' ') + lvl;
dest.append(lvlpad.data(), lvlpad.data() + lvlpad.size());
}
spdlog::details::padding_info get_padding_info()
{
return padinfo_;
}
std::unique_ptr<custom_flag_formatter> clone() const override
{
return spdlog::details::make_unique<CustomLevelFlag>();
}
};
Log::Log(Level lvl) :
level(lvl),
pattern("%H:%M:%S %^%l%$ %n: %v")
pattern("%H:%M:%S %^%-4t%$ %-16n %v")
{
char *p = getenv("VILLAS_LOG_PREFIX");
if (p)
......@@ -48,7 +80,7 @@ Log::Log(Level lvl) :
sinks = std::make_shared<DistSink::element_type>();
setLevel(level);
setPattern(pattern);
setFormatter(pattern);
/* Default sink */
sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
......@@ -74,7 +106,7 @@ Logger Log::get(const std::string &name)
logger = std::make_shared<Logger::element_type>(name, sink);
logger->set_level(level);
logger->set_pattern(prefix + pattern);
logger->set_formatter(formatter->clone());
for (auto &expr : expressions) {
int flags = 0;
......@@ -142,12 +174,16 @@ void Log::parse(json_t *json)
}
}
void Log::setPattern(const std::string &pat)
void Log::setFormatter(const std::string &pat)
{
pattern = pat;
spdlog::set_pattern(pattern, spdlog::pattern_time_type::utc);
sinks->set_pattern(pattern);
formatter = std::make_shared<spdlog::pattern_formatter>(spdlog::pattern_time_type::utc);
formatter->add_flag<CustomLevelFlag>('t');
formatter->set_pattern(pattern);
spdlog::set_formatter(formatter->clone());
sinks->set_formatter(formatter->clone());
}
void Log::setLevel(Level lvl)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment