Add file logger and fix log level output #9

Merged
moritz merged 3 commits from improve-logging into master 2023-11-19 13:27:21 +00:00
8 changed files with 41 additions and 15 deletions

View File

@@ -14,7 +14,7 @@ jobs:
- name: install meson - name: install meson
run: apt-get -yq install meson run: apt-get -yq install meson
- name: Install build dependencies - name: Install build dependencies
run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev gcovr
- name: setup builddir - name: setup builddir
run: meson setup builddir -Db_coverage=true run: meson setup builddir -Db_coverage=true
- name: build all targets - name: build all targets

View File

@@ -19,7 +19,7 @@ pacman -S libusb spdlog pango cairo meson gcovr
Install dependencies on Debian/Ubuntu Install dependencies on Debian/Ubuntu
``` bash ``` bash
apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr apt-get install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev meson gcovr
``` ```
## Build ## Build

View File

@@ -6,6 +6,7 @@ project('ptprnt', 'cpp',
usb_dep = dependency('libusb-1.0') usb_dep = dependency('libusb-1.0')
log_dep = dependency('spdlog') log_dep = dependency('spdlog')
fmt_dep = dependency('fmt')
pangocairo_dep = dependency('pangocairo') pangocairo_dep = dependency('pangocairo')
# CLI11 # CLI11
@@ -23,7 +24,7 @@ ptprnt_exe = executable(
'ptprnt', 'ptprnt',
'src/main.cpp', 'src/main.cpp',
install: true, install: true,
dependencies : [usb_dep, log_dep, pangocairo_dep, cli11_dep], dependencies : [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep],
include_directories: incdir, include_directories: incdir,
sources: [ptprnt_srcs], sources: [ptprnt_srcs],
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'], cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],

View File

@@ -16,14 +16,25 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#define SPDLOG_DEBUG_ON
#define SPDLOG_TRACE_ON
#include "PtouchPrint.hpp" #include "PtouchPrint.hpp"
#include <CLI/App.hpp> #include <CLI/App.hpp>
#include <fmt/core.h>
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <functional> #include <functional>
#include <memory>
#include <vector>
#include "CLI/Option.hpp" #include "CLI/Option.hpp"
#include "P700Printer.hpp" #include "P700Printer.hpp"
@@ -45,9 +56,9 @@ int PtouchPrint::init(int argc, char** argv) {
} }
if (mVerboseFlag) { if (mVerboseFlag) {
setupLogger(spdlog::level::trace); setupLogger(spdlog::level::debug);
} else { } else {
setupLogger(spdlog::level::critical); setupLogger(spdlog::level::err);
} }
if (!mUsbDeviceFactory.init()) { if (!mUsbDeviceFactory.init()) {
@@ -59,10 +70,12 @@ int PtouchPrint::init(int argc, char** argv) {
} }
int PtouchPrint::run() { int PtouchPrint::run() {
spdlog::debug("ptprnt version {}", mVersionString);
SPDLOG_TRACE("testing trace");
auto numFoundPrinters = getCompatiblePrinters(); auto numFoundPrinters = getCompatiblePrinters();
if (numFoundPrinters == 0) { if (numFoundPrinters == 0) {
spdlog::error( 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; return -1;
} else if (numFoundPrinters > 1) { } else if (numFoundPrinters > 1) {
spdlog::warn("Found more than one compatible printer. Currently not supported."); spdlog::warn("Found more than one compatible printer. Currently not supported.");
@@ -84,7 +97,7 @@ int PtouchPrint::run() {
//printer->printText("wurst", 1); //printer->printText("wurst", 1);
for (auto& cmd : mCommands) { for (auto& cmd : mCommands) {
std::cout << cmd.second << std::endl; spdlog::debug("Command: {}", cmd.second);
} }
return 0; return 0;
@@ -113,13 +126,22 @@ unsigned int PtouchPrint::getCompatiblePrinters() {
} }
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) { void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
spdlog::set_level(lvl); auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
spdlog::debug("Starting ptprnt {}", mVersionString); consoleSink->set_level(lvl);
consoleSink->set_pattern("%^%L:%$ %v");
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("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<spdlog::sink_ptr> sinks{consoleSink, fileSink};
auto logger = std::make_shared<spdlog::logger>("default_logger", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
spdlog::set_default_logger(logger);
} }
void PtouchPrint::setupCliParser() { void PtouchPrint::setupCliParser() {
auto printVersion = [this](std::size_t) { auto printVersion = [this](std::size_t) {
std::cout << "ptprnt version: " << mVersionString << std::endl; fmt::print("ptprnt version: {}\n", mVersionString);
}; };
// General options // General options

View File

@@ -30,6 +30,7 @@ namespace ptprnt {
class IPrinterDriver { class IPrinterDriver {
public: public:
virtual ~IPrinterDriver() = default;
virtual const std::string_view getDriverName() = 0; virtual const std::string_view getDriverName() = 0;
virtual const std::string_view getName() = 0; virtual const std::string_view getName() = 0;
virtual const std::string_view getVersion() = 0; virtual const std::string_view getVersion() = 0;

View File

@@ -29,8 +29,8 @@
namespace libusbwrap { namespace libusbwrap {
class UsbDevice : public IUsbDevice { class UsbDevice : public IUsbDevice {
public: public:
UsbDevice(libusb_context* ctx, libusb_device* dev); explicit UsbDevice(libusb_context* ctx, libusb_device* dev);
~UsbDevice(); ~UsbDevice() override;
// delete copy ctor and assignment // delete copy ctor and assignment
UsbDevice(const UsbDevice&) = delete; UsbDevice(const UsbDevice&) = delete;

View File

@@ -24,8 +24,9 @@ enum class Speed {
class IUsbDevice { class IUsbDevice {
public: public:
virtual bool open() = 0; virtual ~IUsbDevice() = default;
virtual void close() = 0; virtual bool open() = 0;
virtual void close() = 0;
// libusb wrappers // libusb wrappers
virtual bool detachKernelDriver(int interfaceNo) = 0; virtual bool detachKernelDriver(int interfaceNo) = 0;

View File

@@ -9,7 +9,8 @@
namespace libusbwrap { namespace libusbwrap {
class IUsbDeviceFactory { class IUsbDeviceFactory {
public: public:
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0; virtual ~IUsbDeviceFactory() = default;
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0; virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
}; };
} // namespace libusbwrap } // namespace libusbwrap