Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Introducing: KDSPDSetup

Initialize loggers with simple config files and focus on your code's functionality

#include <KDSpdSetup/kdspdsetup.h>

void doStuff() {
    auto log1 = spdlog::get("log1");
    log1->info("both console and basic file sink get this message");
    log1->debug("only basic file sink gets this message");
}

void doOtherStuff(std::shared_ptr<spdlog::logger> logger) {
    logger->error("both basic and daily sinks get this message");
    logger->info("only basic file sink gets this message");
}

int main() {
    KDSPDSetup::setupFrom("logConfig.toml");
    doStuff();
    doOtherStuff(spdlog::get("log2"));
}
[[pattern]]
name = "examplePattern1"
value = "[%^%l%$] %v"

[[pattern]]
name = "examplePattern2"
value = "%c-%L: %v"

[[sink]]
name = "consoleSinkSt"
type = "stdout_color_sink_st"
level = "info"

[[sink]]
name = "fileSinkMt"
type = "basic_file_sink_st"
filename = "logs/basicFile"
truncate = true
level = "debug"

[[sink]]
name = "dailySinkMt"
type = "daily_file_sink_mt"
base_filename = "logs/dailyFile"
rotation_hour = 4
rotation_minute = 0
level = "warn"

[[thread_pool]]
name = "threadpool"
queue_size = 2048
num_threads = 4

[[logger]]
name = "log1"
sinks = ["consoleSinkSt", "fileSinkMt"]
level = "trace"
pattern = "examplePattern1"

[[logger]]
name = "log2"
sinks = ["fileSinkMt", "dailySinkMt"]
overflow_policy = "overrun_oldest"
level = "debug"
pattern = "examplePattern2"
#include <spdlog/async.h>
#include <spdlog/async_logger.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

void initLoggers() {
    auto const examplePattern1 = "[%^%l%$] %v";
    auto const examplePattern2 = "%c-%L: %v";

    auto consoleSinkSt =
        std::make_shared<spdlog::sinks::stdout_color_sink_st>();

    consoleSinkSt->set_level(spdlog::level::info);

    auto fileSinkMt = std::make_shared<spdlog::sinks::basic_file_sink_st>(
        "logs/basicFile", true);

    fileSinkMt->set_level(spdlog::level::debug);

    auto dailySinkMt = std::make_shared<spdlog::sinks::daily_file_sink_mt>(
        "logs/dailyFile", 4, 0);

    dailySinkMt->set_level(spdlog::level::warn);

    auto static threadpool =
        std::make_shared<spdlog::details::thread_pool>(2048, 4);

    auto logger1 = std::make_shared<spdlog::logger>(
        spdlog::logger("log1", {consoleSinkSt, fileSinkMt}));

    logger1->set_level(spdlog::level::trace);
    logger1->set_pattern(examplePattern1);

    auto logger2 = std::make_shared<spdlog::async_logger>(
        spdlog::async_logger("log2", {fileSinkMt, dailySinkMt}, threadpool,
                             spdlog::async_overflow_policy::overrun_oldest));

    logger2->set_level(spdlog::level::debug);
    logger2->set_pattern(examplePattern2);

    spdlog::register_logger(logger1);
    spdlog::register_logger(logger2);
}

void doStuff() {
    auto log1 = spdlog::get("log1");
    log1->info("both console and basic file sink get this message");
    log1->debug("only basic file sink gets this message");
}

void doOtherStuff(std::shared_ptr<spdlog::logger> logger) {
    logger->error("both basic and daily sinks get this message");
    logger->info("only basic file sink gets this message");
}

int main() {
    initLoggers();
    doStuff();
    doOtherStuff(spdlog::get("log2"));
}

About KDAB