0.5.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
9/// @file Logger.hpp
10/// @brief Utility class for logging to console and file
11/// @author T. Topp (topp@ins.uni-stuttgart.de)
12/// @date 2020-05-08
13
14#pragma once
15
16// Available log levels
17#define LOG_LEVEL_DATA 0 ///< All output which occurs repeatedly every time observations are received
18#define LOG_LEVEL_TRACE 1 ///< Detailled info to trace the execution of the program. Should not be called on functions which receive observations (spamming)
19#define LOG_LEVEL_DEBUG 2 ///< All output needed to debug functions. Should not be called on functions which receive observations (spamming)
20#define LOG_LEVEL_INFO 3 ///< All output informing the user about normal operation.
21#define LOG_LEVEL_WARN 4 ///< All output informing the user about abnormal operation, but the code can still function or can switch to a fallback option.
22#define LOG_LEVEL_ERROR 5 ///< All output informing the user about abnormal operation, which results in the code not working anymore
23#define LOG_LEVEL_CRITICAL 6 ///< A critical event occurred which results in termination of the program
24#define LOG_LEVEL_OFF 7 ///< Logging turned off
25
26// Active log level (passed as definition to cmake)
27#if LOG_LEVEL == LOG_LEVEL_DATA
28 /// All output which occurs repeatedly every time observations are received
29 #define LOG_DATA SPDLOG_TRACE
30 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE ///< Set the active SPDLOG level
31#elif LOG_LEVEL == LOG_LEVEL_TRACE
32 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE ///< Set the active SPDLOG level
33#elif LOG_LEVEL == LOG_LEVEL_DEBUG
34 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG ///< Set the active SPDLOG level
35#elif LOG_LEVEL == LOG_LEVEL_INFO
36 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO ///< Set the active SPDLOG level
37#elif LOG_LEVEL == LOG_LEVEL_WARN
38 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_WARN ///< Set the active SPDLOG level
39#elif LOG_LEVEL == LOG_LEVEL_ERROR
40 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR ///< Set the active SPDLOG level
41#elif LOG_LEVEL == LOG_LEVEL_CRITICAL
42 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_CRITICAL ///< Set the active SPDLOG level
43#elif LOG_LEVEL == LOG_LEVEL_OFF
44 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF ///< Set the active SPDLOG level
45#endif
46
47/// External usage of FMT, as the internal one is missing some files
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
61 /// All output which occurs repeatedly every time observations are received
62 #define LOG_DATA(...) (void)0
63#endif
64/// Detailled info to trace the execution of the program. Should not be called on functions which receive observations (spamming)
65#define LOG_TRACE SPDLOG_TRACE
66/// Debug information. Should not be called on functions which receive observations (spamming)
67#define LOG_DEBUG SPDLOG_DEBUG
68/// Info to the user on the state of the program
69#define LOG_INFO SPDLOG_INFO
70/// Error occurred, but a fallback option exists and program continues to work normally
71#define LOG_WARN SPDLOG_WARN
72/// Error occurred, which stops part of the program to work, but not everything
73#define LOG_ERROR SPDLOG_ERROR
74/// Critical Event, which causes the program to work entirely and throws an exception
75#define LOG_CRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__), throw std::runtime_error(fmt::format(__VA_ARGS__))
76
77/// @brief Utility class for logging
78///
79/// Use the Macros to do logging, as they can be turned off during compilation
80/// - LOG_DATA("Message {} {}", variable1, variable 2);
81/// - LOG_TRACE("Message {} {}", variable1, variable 2);
82/// - LOG_DEBUG("Message {} {}", variable1, variable 2);
83/// - LOG_INFO("Message {} {}", variable1, variable 2);
84/// - LOG_WARN("Message {} {}", variable1, variable 2);
85/// - LOG_ERROR("Message {} {}", variable1, variable 2);
86/// - LOG_CRITICAL("Message {} {}", variable1, variable 2);
87class Logger
88{
89 public:
90 /// @brief Constructor
91 /// @param[in] logpath Relative filepath to the logfile
92 explicit Logger(const std::string& logpath);
93
94 /// @brief Default constructor
95 Logger();
96 /// @brief Destructor
97 ~Logger();
98 /// @brief Copy constructor
99 Logger(const Logger&) = delete;
100 /// @brief Move constructor
101 Logger(Logger&&) = default;
102 /// @brief Copy assignment operator
103 Logger& operator=(const Logger&) = delete;
104 /// @brief Move assignment operator
105 Logger& operator=(Logger&&) = default;
106
107 /// @brief Returns the ring buffer sink
108 static const std::shared_ptr<spdlog::sinks::ringbuffer_sink_mt>& GetRingBufferSink();
109
110 private:
111 /// @brief Ring buffer sink
112 static inline std::shared_ptr<spdlog::sinks::ringbuffer_sink_mt> _ringBufferSink = nullptr;
113
114 /// @brief Writes a separation line to the console only
115 static void writeSeparator() noexcept;
116
117 /// @brief Writes the logging header
118 static void writeHeader() noexcept;
119
120 /// @brief Writes the logging footer
121 static void writeFooter() noexcept;
122};
static const std::shared_ptr< spdlog::sinks::ringbuffer_sink_mt > & GetRingBufferSink()
Returns the ring buffer sink.
Definition Logger.cpp:181
Logger(const std::string &logpath)
Constructor.
Definition Logger.cpp:56
static void writeFooter() noexcept
Writes the logging footer.
Definition Logger.cpp:206
Logger & operator=(Logger &&)=default
Move assignment operator.
static void writeHeader() noexcept
Writes the logging header.
Definition Logger.cpp:191
Logger & operator=(const Logger &)=delete
Copy assignment operator.
static std::shared_ptr< spdlog::sinks::ringbuffer_sink_mt > _ringBufferSink
Ring buffer sink.
Definition Logger.hpp:112
Logger(Logger &&)=default
Move constructor.
Logger()
Default constructor.
Definition Logger.cpp:149
~Logger()
Destructor.
Definition Logger.cpp:174
Logger(const Logger &)=delete
Copy constructor.
static void writeSeparator() noexcept
Writes a separation line to the console only.
Definition Logger.cpp:186