LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
11#include <memory>
12#include <mutex>
13
14#if defined(THREADS_STD)
15#else
17#endif
18
19#define LOG_DEBUG(msg) logger->debug([=]() { return msg; })
20#define LOG_INFO(msg) logger->info([=]() { return msg; })
21#define LOG_WARN(msg) logger->warn([=]() { return msg; })
22#define LOG_ERROR(msg) logger->error([=]() { return msg; })
23
24#define LOG_DEBUG_S(msg) LOG_DEBUG(std::string(msg))
25#define LOG_INFO_S(msg) LOG_INFO(std::string(msg))
26#define LOG_WARN_S(msg) LOG_WARN(std::string(msg))
27#define LOG_ERROR_S(msg) LOG_ERROR(std::string(msg))
28
29namespace okapi {
30class Logger {
31 public:
32 enum class LogLevel {
33 debug = 4,
34 info = 3,
35 warn = 2,
36 error = 1,
37 off = 0
38 };
39
43 Logger() noexcept;
44
54 Logger(std::unique_ptr<AbstractTimer> itimer,
55 std::string_view ifileName,
56 const LogLevel &ilevel) noexcept;
57
66 Logger(std::unique_ptr<AbstractTimer> itimer, FILE *ifile, const LogLevel &ilevel) noexcept;
67
69
70 constexpr bool isDebugLevelEnabled() const noexcept {
72 }
73
74 template <typename T> void debug(T ilazyMessage) noexcept {
75 if (isDebugLevelEnabled() && logfile && timer) {
76 std::scoped_lock lock(logfileMutex);
77 fprintf(logfile,
78 "%ld (%s) DEBUG: %s\n",
79 static_cast<long>(timer->millis().convert(millisecond)),
81 ilazyMessage().c_str());
82 }
83 }
84
85 constexpr bool isInfoLevelEnabled() const noexcept {
87 }
88
89 template <typename T> void info(T ilazyMessage) noexcept {
90 if (isInfoLevelEnabled() && logfile && timer) {
91 std::scoped_lock lock(logfileMutex);
92 fprintf(logfile,
93 "%ld (%s) INFO: %s\n",
94 static_cast<long>(timer->millis().convert(millisecond)),
96 ilazyMessage().c_str());
97 }
98 }
99
100 constexpr bool isWarnLevelEnabled() const noexcept {
102 }
103
104 template <typename T> void warn(T ilazyMessage) noexcept {
105 if (isWarnLevelEnabled() && logfile && timer) {
106 std::scoped_lock lock(logfileMutex);
107 fprintf(logfile,
108 "%ld (%s) WARN: %s\n",
109 static_cast<long>(timer->millis().convert(millisecond)),
111 ilazyMessage().c_str());
112 }
113 }
114
115 constexpr bool isErrorLevelEnabled() const noexcept {
117 }
118
119 template <typename T> void error(T ilazyMessage) noexcept {
120 if (isErrorLevelEnabled() && logfile && timer) {
121 std::scoped_lock lock(logfileMutex);
122 fprintf(logfile,
123 "%ld (%s) ERROR: %s\n",
124 static_cast<long>(timer->millis().convert(millisecond)),
126 ilazyMessage().c_str());
127 }
128 }
129
133 constexpr void close() noexcept {
134 if (logfile) {
135 fclose(logfile);
136 logfile = nullptr;
137 }
138 }
139
143 static std::shared_ptr<Logger> getDefaultLogger();
144
151 static void setDefaultLogger(std::shared_ptr<Logger> ilogger);
152
153 private:
154 const std::unique_ptr<AbstractTimer> timer;
155 const LogLevel logLevel;
156 FILE *logfile;
157 CrossplatformMutex logfileMutex;
158
159 static bool isSerialStream(std::string_view filename);
160};
161
162extern std::shared_ptr<Logger> defaultLogger;
163
166 if (count++ == 0) {
167 init();
168 }
169 }
171 if (--count == 0) {
172 cleanup();
173 }
174 }
175
176 static int count;
177
178 static void init() {
179#if defined(THREADS_STD)
180 defaultLogger = std::make_shared<Logger>();
181#else
183 std::make_shared<Logger>(std::make_unique<Timer>(), "/ser/sout", Logger::LogLevel::warn);
184#endif
185 }
186
187 static void cleanup() {
188 }
189};
190
191static DefaultLoggerInitializer defaultLoggerInitializer; // NOLINT(cert-err58-cpp)
192} // namespace okapi
static std::string getName()
constexpr void close() noexcept
Closes the connection to the log file.
Definition logging.hpp:133
constexpr bool isErrorLevelEnabled() const noexcept
Definition logging.hpp:115
static void setDefaultLogger(std::shared_ptr< Logger > ilogger)
Sets a new default logger.
void warn(T ilazyMessage) noexcept
Definition logging.hpp:104
void debug(T ilazyMessage) noexcept
Definition logging.hpp:74
constexpr bool isWarnLevelEnabled() const noexcept
Definition logging.hpp:100
void info(T ilazyMessage) noexcept
Definition logging.hpp:89
Logger() noexcept
A logger that does nothing.
void error(T ilazyMessage) noexcept
Definition logging.hpp:119
constexpr bool isDebugLevelEnabled() const noexcept
Definition logging.hpp:70
static std::shared_ptr< Logger > getDefaultLogger()
constexpr bool isInfoLevelEnabled() const noexcept
Definition logging.hpp:85
constexpr QTime millisecond
Definition QTime.hpp:18
constexpr auto toUnderlyingType(const E e) noexcept
Converts an enum to its value type.
Definition mathUtil.hpp:188
static DefaultLoggerInitializer defaultLoggerInitializer
Definition logging.hpp:191
std::shared_ptr< Logger > defaultLogger
STL namespace.