2023-08-28 12:31:18 +02:00
|
|
|
/* Logging.
|
2018-08-21 00:25:44 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +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
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-09 15:29:31 +02:00
|
|
|
#include <list>
|
2023-09-07 13:19:19 +02:00
|
|
|
#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>
|
2023-09-07 13:19:19 +02:00
|
|
|
#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
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Forward declarations
|
2018-10-19 14:33:10 +02:00
|
|
|
using Logger = std::shared_ptr<spdlog::logger>;
|
|
|
|
class Log {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
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
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
class Expression {
|
|
|
|
public:
|
|
|
|
std::string name;
|
|
|
|
Level level;
|
2021-07-09 15:29:31 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Expression(const std::string &n, Level lvl) : name(n), level(lvl) {}
|
2021-07-09 15:29:31 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Expression(json_t *json);
|
|
|
|
};
|
2021-07-09 15:29:31 +02:00
|
|
|
|
2018-10-19 14:33:10 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
DistSink sinks;
|
|
|
|
DefaultSink sink;
|
|
|
|
Formatter formatter;
|
2018-10-19 14:33:10 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Level level;
|
2018-11-04 17:12:12 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string pattern; // Logging format.
|
|
|
|
std::string prefix; // Prefix each line with this string.
|
2018-10-19 14:33:10 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::list<Expression> expressions;
|
2021-07-09 15:29:31 +02:00
|
|
|
|
2018-10-19 14:33:10 +02:00
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
Log(Level level = Level::info);
|
2018-10-19 14:33:10 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Get the real usable log output width which fits into one line.
|
|
|
|
int getWidth();
|
2018-10-19 14:33:10 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void parse(json_t *json);
|
2018-10-19 14:33:10 +02:00
|
|
|
|
2024-07-29 12:35:42 +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
|
|
|
|
2023-09-07 13:19:19 +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
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Level getLevel() const;
|
|
|
|
std::string getLevelName() const;
|
2023-02-28 12:55:38 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void addSink(std::shared_ptr<spdlog::sinks::sink> sink) {
|
|
|
|
sink->set_formatter(formatter->clone());
|
|
|
|
sink->set_level(level);
|
2023-04-03 10:04:42 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
sinks->add_sink(sink);
|
|
|
|
}
|
2023-04-03 10:04:42 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void replaceStdSink(std::shared_ptr<spdlog::sinks::sink> sink) {
|
|
|
|
sink->set_formatter(formatter->clone());
|
|
|
|
sink->set_level(level);
|
2023-04-03 10:04:42 +00:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
sinks->sinks()[0] = sink;
|
|
|
|
}
|
2018-10-19 14:33:10 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace villas
|