WPILibC++ 2024.3.2
Logger.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPIUTIL_WPI_LOGGER_H_
6#define WPIUTIL_WPI_LOGGER_H_
7
8#include <functional>
9#include <utility>
10
11#include <fmt/format.h>
12
13namespace wpi {
14
25};
26
27class Logger {
28 public:
29 using LogFunc = std::function<void(unsigned int level, const char* file,
30 unsigned int line, const char* msg)>;
31
32 Logger() = default;
33 explicit Logger(LogFunc func) : m_func(std::move(func)) {}
34 Logger(LogFunc func, unsigned int min_level)
35 : m_func(std::move(func)), m_min_level(min_level) {}
36
37 void SetLogger(LogFunc func) { m_func = func; }
38
39 void set_min_level(unsigned int level) { m_min_level = level; }
40 unsigned int min_level() const { return m_min_level; }
41
42 void DoLog(unsigned int level, const char* file, unsigned int line,
43 const char* msg);
44
45 void LogV(unsigned int level, const char* file, unsigned int line,
47
48 template <typename... Args>
49 void Log(unsigned int level, const char* file, unsigned int line,
50 fmt::string_view format, Args&&... args) {
51 if (m_func && level >= m_min_level) {
52 LogV(level, file, line, format, fmt::make_format_args(args...));
53 }
54 }
55
56 bool HasLogger() const { return m_func != nullptr; }
57
58 private:
59 LogFunc m_func;
60 unsigned int m_min_level = 20;
61};
62
63// C++20 relaxed the number of arguments to variadics, but Apple Clang's
64// warnings haven't caught up yet: https://stackoverflow.com/a/67996331
65#ifdef __clang__
66#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
67#endif
68
69#define WPI_LOG(logger_inst, level, format, ...) \
70 if ((logger_inst).HasLogger() && level >= (logger_inst).min_level()) { \
71 (logger_inst) \
72 .Log(level, __FILE__, __LINE__, \
73 FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__); \
74 }
75
76#define WPI_ERROR(inst, format, ...) \
77 WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, format __VA_OPT__(, ) __VA_ARGS__)
78#define WPI_WARNING(inst, format, ...) \
79 WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, format __VA_OPT__(, ) __VA_ARGS__)
80#define WPI_INFO(inst, format, ...) \
81 WPI_LOG(inst, ::wpi::WPI_LOG_INFO, format __VA_OPT__(, ) __VA_ARGS__)
82#define WPI_DEBUG(inst, format, ...) \
83 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, format __VA_OPT__(, ) __VA_ARGS__)
84#define WPI_DEBUG1(inst, format, ...) \
85 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, format __VA_OPT__(, ) __VA_ARGS__)
86#define WPI_DEBUG2(inst, format, ...) \
87 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, format __VA_OPT__(, ) __VA_ARGS__)
88#define WPI_DEBUG3(inst, format, ...) \
89 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, format __VA_OPT__(, ) __VA_ARGS__)
90#define WPI_DEBUG4(inst, format, ...) \
91 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, format __VA_OPT__(, ) __VA_ARGS__)
92
93} // namespace wpi
94
95#endif // WPIUTIL_WPI_LOGGER_H_
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition: ThirdPartyNotices.txt:192
Definition: Logger.h:27
std::function< void(unsigned int level, const char *file, unsigned int line, const char *msg)> LogFunc
Definition: Logger.h:30
void Log(unsigned int level, const char *file, unsigned int line, fmt::string_view format, Args &&... args)
Definition: Logger.h:49
void LogV(unsigned int level, const char *file, unsigned int line, fmt::string_view format, fmt::format_args args)
Logger(LogFunc func, unsigned int min_level)
Definition: Logger.h:34
void set_min_level(unsigned int level)
Definition: Logger.h:39
bool HasLogger() const
Definition: Logger.h:56
Logger(LogFunc func)
Definition: Logger.h:33
unsigned int min_level() const
Definition: Logger.h:40
void SetLogger(LogFunc func)
Definition: Logger.h:37
void DoLog(unsigned int level, const char *file, unsigned int line, const char *msg)
Logger()=default
basic_string_view< char > string_view
Definition: core.h:501
constexpr auto make_format_args(T &... args) -> format_arg_store< Context, remove_cvref_t< T >... >
\rst Constructs a ~fmtformat_arg_store object that contains references to arguments and can be implic...
Definition: core.h:1824
basic_format_args< format_context > format_args
An alias to basic_format_args<format_context>.
Definition: core.h:1971
Definition: array.h:89
Definition: ntcore_cpp.h:26
LogLevel
Definition: Logger.h:15
@ WPI_LOG_CRITICAL
Definition: Logger.h:16
@ WPI_LOG_DEBUG2
Definition: Logger.h:22
@ WPI_LOG_DEBUG
Definition: Logger.h:20
@ WPI_LOG_ERROR
Definition: Logger.h:17
@ WPI_LOG_DEBUG1
Definition: Logger.h:21
@ WPI_LOG_DEBUG4
Definition: Logger.h:24
@ WPI_LOG_WARNING
Definition: Logger.h:18
@ WPI_LOG_DEBUG3
Definition: Logger.h:23
@ WPI_LOG_INFO
Definition: Logger.h:19
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108