Commit 47ee3e42 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

fix logging

parent a012938e
......@@ -48,7 +48,7 @@ class Log {
public:
using Level = spdlog::level::level_enum;
using DistSink = spdlog::sinks::dist_sink_mt;
using DistSink = std::shared_ptr<spdlog::sinks::dist_sink_mt>;
protected:
Logger logger = logging.get("log");
......@@ -59,6 +59,8 @@ protected:
std::string pattern; /**< Logging format. */
std::string prefix; /**< Prefix each line with this string. */
void init();
public:
Log(Level level = Level::info);
......
......@@ -134,7 +134,8 @@ public:
{
// CRTP
derivedAlloc = static_cast<DerivedAllocator*>(this);
logger = logging.get(derivedAlloc->getName());
std::string loggerName = fmt::format("memory:", derivedAlloc->getName());
logger = logging.get(loggerName);
// default deallocation callback
free = [&](MemoryBlock* mem) {
......
......@@ -81,6 +81,7 @@ target_link_libraries(villas-common PUBLIC
${CMAKE_DL_LIBS}
spdlog
fmt
stdc++
)
target_compile_definitions(villas-common PUBLIC
......
......@@ -35,7 +35,11 @@ using namespace villas;
/** The global log instance */
Log villas::logging;
Log::Log(Level level)
Log::Log(Level lvl) :
level(lvl)
{ }
void Log::init()
{
char *p = getenv("VILLAS_LOG_PREFIX");
if (p)
......@@ -44,9 +48,11 @@ Log::Log(Level level)
setLevel(level);
setPattern("%H:%M:%S %^%l%$ %n: %v");
sinks = std::make_shared<DistSink::element_type>();
// Default sink
auto sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
sink->set_pattern(prefix + pattern);
sinks->add_sink(sink);
}
......@@ -64,9 +70,18 @@ Logger Log::get(const std::string &name)
{
Logger logger = spdlog::get(name);
if (not logger)
if (not sinks)
init();
if (not logger) {
logger = std::make_shared<Logger::element_type>(name, sinks);
logger->set_level(level);
logger->set_pattern(prefix + pattern);
spdlog::register_logger(logger);
}
return logger;
}
......@@ -80,7 +95,7 @@ void Log::parse(json_t *cfg)
int ret;
json_error_t err;
json_t *json_expressions;
json_t *json_expressions = nullptr;
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: s, s?: s, s?: b, s?: s }",
"level", &level,
......@@ -98,13 +113,13 @@ void Log::parse(json_t *cfg)
if (path) {
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(path);
sinks.add_sink(sink);
sinks->add_sink(sink);
}
if (syslog) {
auto sink = std::make_shared<spdlog::sinks::syslog_sink_mt>("villas", LOG_PID, LOG_DAEMON);
sinks.add_sink(sink);
sinks->add_sink(sink);
}
if (json_expressions) {
......
Supports Markdown
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