parselink-old/source/logging/logging.cpp

63 lines
1.7 KiB
C++

//-----------------------------------------------------------------------------
// ___ __ _ _
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
// / ___/ (_| | | \__ \ __/ /__| | | | | <
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
//
//-----------------------------------------------------------------------------
// Author: Kurt Sassenrath
// Module: Logging
//
// Logging implementation.
//
// Copyright (c) 2023 Kurt Sassenrath.
//
// License TBD.
//-----------------------------------------------------------------------------
#include "parselink/logging.h"
#include <fmt/chrono.h>
using namespace parselink::logging;
namespace {
struct console_endpoint : public endpoint {
static constexpr std::string_view format_string =
"{:%Y-%m-%d %H:%M:%S}.{:03} [{:<8}] {:>20} | {}\n";
bool colored() const noexcept override { return true; }
void write(message const& msg) override {
using namespace std::chrono_literals;
fmt::print(format_string, fmt::gmtime(msg.time),
(msg.time.time_since_epoch() % 1000ms) / 1ms,
themed_arg{enum_name_only{msg.lvl}}, msg.name, msg.message);
}
};
} // namespace
auto& console() {
static auto console = std::make_shared<console_endpoint>();
return console;
}
logger::logger(std::string_view name)
: name_{name} {
endpoints_.emplace_back(console());
}
logger::logger(
std::string_view name, std::vector<std::shared_ptr<endpoint>> eps)
: name_{name}
, endpoints_{std::move(eps)} {}
void logger::set_threshold(level new_threshold) noexcept {
for (auto& ep : endpoints_) {
ep->threshold = new_threshold;
}
}