From dd496a325b562e0b029ada0f3dc90b1f0825309a Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sun, 19 Nov 2023 14:09:53 +0100 Subject: [PATCH] Add file logger and fix log level output --- src/PtouchPrint.cpp | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index f3528b4..9853e4a 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -16,14 +16,25 @@ along with this program. If not, see . */ +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE +#define SPDLOG_DEBUG_ON +#define SPDLOG_TRACE_ON #include "PtouchPrint.hpp" #include +#include #include +#include +#include +#include +#include +#include #include #include +#include +#include #include "CLI/Option.hpp" #include "P700Printer.hpp" @@ -45,9 +56,9 @@ int PtouchPrint::init(int argc, char** argv) { } if (mVerboseFlag) { - setupLogger(spdlog::level::trace); + setupLogger(spdlog::level::debug); } else { - setupLogger(spdlog::level::critical); + setupLogger(spdlog::level::err); } if (!mUsbDeviceFactory.init()) { @@ -59,10 +70,12 @@ int PtouchPrint::init(int argc, char** argv) { } int PtouchPrint::run() { + spdlog::debug("ptprnt version {}", mVersionString); + SPDLOG_TRACE("testing trace"); auto numFoundPrinters = getCompatiblePrinters(); if (numFoundPrinters == 0) { spdlog::error( - "No compatible printers found, please make sure if they are turned on and connected"); + "No compatible printers found, please make sure that they are turned on and connected"); return -1; } else if (numFoundPrinters > 1) { spdlog::warn("Found more than one compatible printer. Currently not supported."); @@ -84,7 +97,7 @@ int PtouchPrint::run() { //printer->printText("wurst", 1); for (auto& cmd : mCommands) { - std::cout << cmd.second << std::endl; + spdlog::debug("Command: {}", cmd.second); } return 0; @@ -113,13 +126,22 @@ unsigned int PtouchPrint::getCompatiblePrinters() { } void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) { - spdlog::set_level(lvl); - spdlog::debug("Starting ptprnt {}", mVersionString); + auto consoleSink = std::make_shared(); + consoleSink->set_level(lvl); + consoleSink->set_pattern("%^%L:%$ %v"); + + auto fileSink = std::make_shared("ptprnt.log", true); + fileSink->set_level(spdlog::level::trace); + fileSink->set_pattern("%Y-%m-%d %H:%m:%S:%e [pid:%P tid:%t] [%^%l%$] %v (%@)"); + std::vector sinks{consoleSink, fileSink}; + auto logger = std::make_shared("default_logger", sinks.begin(), sinks.end()); + logger->set_level(spdlog::level::trace); + spdlog::set_default_logger(logger); } void PtouchPrint::setupCliParser() { auto printVersion = [this](std::size_t) { - std::cout << "ptprnt version: " << mVersionString << std::endl; + fmt::println("ptprnt version: {}", mVersionString); }; // General options