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

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

180 lines
3.9 KiB
C++
Raw Normal View History

2018-10-19 14:33:10 +02:00
/** Logging and debugging routines
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:26 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
2018-10-19 14:33:10 +02:00
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <list>
#include <algorithm>
2018-10-19 14:33:10 +02:00
#include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <villas/log.hpp>
#include <villas/exceptions.hpp>
using namespace villas;
/** The global log instance */
Log villas::logging;
2018-12-04 14:12:22 +01:00
Log::Log(Level lvl) :
2019-01-23 02:49:44 +01:00
level(lvl),
pattern("%H:%M:%S %^%l%$ %n: %v")
2018-10-19 14:33:10 +02:00
{
char *p = getenv("VILLAS_LOG_PREFIX");
if (p)
prefix = p;
2018-12-04 14:12:22 +01:00
sinks = std::make_shared<DistSink::element_type>();
2019-01-23 02:49:44 +01:00
setLevel(level);
setPattern(pattern);
/* Default sink */
2019-01-23 02:49:44 +01:00
sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
2018-10-19 14:33:10 +02:00
sinks->add_sink(sink);
}
int Log::getWidth()
{
2019-01-23 02:49:44 +01:00
int width = Terminal::getCols() - 50;
2018-10-19 14:33:10 +02:00
if (!prefix.empty())
width -= prefix.length();
return width;
}
Logger Log::get(const std::string &name)
{
2018-11-30 21:43:37 +01:00
Logger logger = spdlog::get(name);
2018-10-19 14:33:10 +02:00
2018-12-04 14:12:22 +01:00
if (not logger) {
2019-01-23 02:49:44 +01:00
logger = std::make_shared<Logger::element_type>(name, sink);
2018-10-19 14:33:10 +02:00
2018-12-04 14:12:22 +01:00
logger->set_level(level);
logger->set_pattern(prefix + pattern);
spdlog::register_logger(logger);
}
2018-10-19 14:33:10 +02:00
return logger;
}
void Log::parse(json_t *cfg)
{
const char *level = NULL;
const char *path = NULL;
const char *pattern = NULL;
int syslog;
int ret;
json_error_t err;
2018-12-04 14:12:22 +01:00
json_t *json_expressions = nullptr;
2018-10-19 14:33:10 +02:00
ret = json_unpack_ex(cfg, &err, JSON_STRICT, "{ s?: s, s?: s, s?: s, s?: b, s?: s }",
2018-10-19 14:33:10 +02:00
"level", &level,
"file", &path,
"expressions", &json_expressions,
"syslog", &syslog,
"pattern", &pattern
);
if (ret)
throw JsonError(err);
2018-10-19 14:33:10 +02:00
if (level)
setLevel(level);
if (path) {
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(path);
2018-12-04 14:12:22 +01:00
sinks->add_sink(sink);
2018-10-19 14:33:10 +02:00
}
if (syslog) {
auto sink = std::make_shared<spdlog::sinks::syslog_sink_mt>("villas", LOG_PID, LOG_DAEMON);
2018-12-04 14:12:22 +01:00
sinks->add_sink(sink);
2018-10-19 14:33:10 +02:00
}
if (json_expressions) {
if (!json_is_array(json_expressions))
throw ConfigError(json_expressions, "node-config.html#node-config-logging-expressions", "The 'expressions' setting must be a list of objects.");
2018-10-19 14:33:10 +02:00
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 }",
2018-10-19 14:33:10 +02:00
"name", &name,
"level", &lvl
);
if (ret)
throw JsonError(err);
2018-10-19 14:33:10 +02:00
2018-11-30 21:43:37 +01:00
Logger logger = get(name);
2018-10-19 14:33:10 +02:00
auto level = spdlog::level::from_str(lvl);
logger->set_level(level);
}
}
}
2018-11-04 17:09:25 +01:00
void Log::setPattern(const std::string &pat)
{
pattern = pat;
spdlog::set_pattern(pattern, spdlog::pattern_time_type::utc);
2019-01-23 02:49:44 +01:00
//sinks.set_pattern(pattern);
2018-11-04 17:09:25 +01:00
}
2018-10-19 14:33:10 +02:00
void Log::setLevel(Level lvl)
{
level = lvl;
2018-10-19 14:33:10 +02:00
spdlog::set_level(lvl);
2019-01-23 02:49:44 +01:00
//sinks.set_level(lvl);
2018-10-19 14:33:10 +02:00
}
void Log::setLevel(const std::string &lvl)
{
std::list<std::string> l = SPDLOG_LEVEL_NAMES;
auto it = std::find(l.begin(), l.end(), lvl);
if (it == l.end())
throw RuntimeError("Invalid log level {}", lvl);
setLevel(spdlog::level::from_str(lvl));
2018-10-19 14:33:10 +02:00
}
2018-11-01 14:30:32 +01:00
Log::Level Log::getLevel() const
{
return level;
}
2018-12-02 02:50:55 +01:00
std::string Log::getLevelName() const
{
return std::string(spdlog::level::to_c_str(level));
}