0.2.0
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16// Available log levels
17#define LOG_LEVEL_DATA 0
18#define LOG_LEVEL_TRACE 1
19#define LOG_LEVEL_DEBUG 2
20#define LOG_LEVEL_INFO 3
21#define LOG_LEVEL_WARN 4
22#define LOG_LEVEL_ERROR 5
23#define LOG_LEVEL_CRITICAL 6
24#define LOG_LEVEL_OFF 7
25
26// Active log level (passed as definition to cmake)
27#if LOG_LEVEL == LOG_LEVEL_DATA
29 #define LOG_DATA SPDLOG_TRACE
30 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
31#elif LOG_LEVEL == LOG_LEVEL_TRACE
32 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
33#elif LOG_LEVEL == LOG_LEVEL_DEBUG
34 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
35#elif LOG_LEVEL == LOG_LEVEL_INFO
36 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
37#elif LOG_LEVEL == LOG_LEVEL_WARN
38 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_WARN
39#elif LOG_LEVEL == LOG_LEVEL_ERROR
40 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
41#elif LOG_LEVEL == LOG_LEVEL_CRITICAL
42 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_CRITICAL
43#elif LOG_LEVEL == LOG_LEVEL_OFF
44 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF
45#endif
46
48#define SPDLOG_FMT_EXTERNAL 1
49
50#include "spdlog/spdlog.h"
51#include "spdlog/fmt/ostr.h"
52#include "spdlog/sinks/ringbuffer_sink.h"
53#include <fmt/std.h>
54
55#include <string>
56#include <stdexcept>
57
58// Macros are redefined in case SPDLOG is not available anymore and it needs to be switched to another Logger
59
60#ifndef LOG_DATA
62 #define LOG_DATA(...) (void)0
63#endif
65#define LOG_TRACE SPDLOG_TRACE
67#define LOG_DEBUG SPDLOG_DEBUG
69#define LOG_INFO SPDLOG_INFO
71#define LOG_WARN SPDLOG_WARN
73#define LOG_ERROR SPDLOG_ERROR
75#define LOG_CRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__), throw std::runtime_error(fmt::format(__VA_ARGS__))
76
87class Logger
88{
89 public:
92 explicit Logger(const std::string& logpath);
93
99 Logger(const Logger&) = delete;
101 Logger(Logger&&) = default;
103 Logger& operator=(const Logger&) = delete;
105 Logger& operator=(Logger&&) = default;
106
108 static const std::shared_ptr<spdlog::sinks::ringbuffer_sink_mt>& GetRingBufferSink();
109
110 private:
112 static inline std::shared_ptr<spdlog::sinks::ringbuffer_sink_mt> _ringBufferSink = nullptr;
113
115 static void writeSeparator() noexcept;
116
118 static void writeHeader() noexcept;
119
121 static void writeFooter() noexcept;
122};
Utility class for logging.
Definition Logger.hpp:88
Logger(const std::string &logpath)
Constructor.
Logger & operator=(Logger &&)=default
Move assignment operator.
Logger & operator=(const Logger &)=delete
Copy assignment operator.
Logger(Logger &&)=default
Move constructor.
Logger()
Default constructor.
static const std::shared_ptr< spdlog::sinks::ringbuffer_sink_mt > & GetRingBufferSink()
Returns the ring buffer sink.
~Logger()
Destructor.
Logger(const Logger &)=delete
Copy constructor.