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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.3 KiB
C++
Raw Permalink Normal View History

/* Logging.
2018-08-21 00:25:44 +02:00
*
* Author: Daniel Krebs <github@daniel-krebs.net>
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
2018-08-21 00:25:44 +02:00
#pragma once
#include <list>
#include <string>
2018-08-21 00:25:44 +02:00
2018-10-19 14:33:10 +02:00
#include <spdlog/sinks/dist_sink.h>
2019-01-23 02:49:44 +01:00
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
2018-08-21 00:25:44 +02:00
2018-10-19 14:33:10 +02:00
#include <jansson.h>
2018-08-21 00:25:44 +02:00
2018-10-19 14:33:10 +02:00
namespace villas {
2018-08-21 00:25:44 +02:00
// Forward declarations
2018-10-19 14:33:10 +02:00
using Logger = std::shared_ptr<spdlog::logger>;
class Log {
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>;
2018-10-19 14:33:10 +02:00
class Expression {
public:
std::string name;
Level level;
Expression(const std::string &n, Level lvl) : name(n), level(lvl) {}
Expression(json_t *json);
};
2018-10-19 14:33:10 +02:00
protected:
DistSink sinks;
DefaultSink sink;
Formatter formatter;
2018-10-19 14:33:10 +02:00
Level level;
std::string pattern; // Logging format.
std::string prefix; // Prefix each line with this string.
2018-10-19 14:33:10 +02:00
std::list<Expression> expressions;
2018-10-19 14:33:10 +02:00
public:
Log(Level level = Level::info);
2018-10-19 14:33:10 +02:00
// Get the real usable log output width which fits into one line.
int getWidth();
2018-10-19 14:33:10 +02:00
void parse(json_t *json);
2018-10-19 14:33:10 +02:00
static Log &getInstance() {
// This will "leak" memory. Because we don't have a complex destructor it's okay
// that this will be cleaned up implicitly on program exit.
static auto log = new Log();
return *log;
};
Logger getNewLogger(const std::string &name);
static Logger get(const std::string &name) {
static auto &log = getInstance();
return log.getNewLogger(name);
}
2018-10-19 14:33:10 +02:00
void setFormatter(const std::string &pattern, const std::string &pfx = "");
void setLevel(Level lvl);
void setLevel(const std::string &lvl);
2018-11-01 14:30:32 +01:00
Level getLevel() const;
std::string getLevelName() const;
void addSink(std::shared_ptr<spdlog::sinks::sink> sink) {
sink->set_formatter(formatter->clone());
sink->set_level(level);
sinks->add_sink(sink);
}
void replaceStdSink(std::shared_ptr<spdlog::sinks::sink> sink) {
sink->set_formatter(formatter->clone());
sink->set_level(level);
sinks->sinks()[0] = sink;
}
2018-10-19 14:33:10 +02:00
};
} // namespace villas