WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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
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,
46 fmt::string_view format, fmt::format_args args);
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__, format __VA_OPT__(, ) __VA_ARGS__); \
73 }
74
75#define WPI_ERROR(inst, format, ...) \
76 WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, format __VA_OPT__(, ) __VA_ARGS__)
77#define WPI_WARNING(inst, format, ...) \
78 WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, format __VA_OPT__(, ) __VA_ARGS__)
79#define WPI_INFO(inst, format, ...) \
80 WPI_LOG(inst, ::wpi::WPI_LOG_INFO, format __VA_OPT__(, ) __VA_ARGS__)
81#define WPI_DEBUG(inst, format, ...) \
82 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, format __VA_OPT__(, ) __VA_ARGS__)
83#define WPI_DEBUG1(inst, format, ...) \
84 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, format __VA_OPT__(, ) __VA_ARGS__)
85#define WPI_DEBUG2(inst, format, ...) \
86 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, format __VA_OPT__(, ) __VA_ARGS__)
87#define WPI_DEBUG3(inst, format, ...) \
88 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, format __VA_OPT__(, ) __VA_ARGS__)
89#define WPI_DEBUG4(inst, format, ...) \
90 WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, format __VA_OPT__(, ) __VA_ARGS__)
91
92} // namespace wpi
93
94#endif // WPIUTIL_WPI_LOGGER_H_
Definition Logger.h:27
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
std::function< void(unsigned int level, const char *file, unsigned int line, const char *msg)> LogFunc
Definition Logger.h:29
void SetLogger(LogFunc func)
Definition Logger.h:37
void DoLog(unsigned int level, const char *file, unsigned int line, const char *msg)
Logger()=default
FMT_INLINE auto format(detail::locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4146
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
Foonathan namespace.
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