WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Logger.hpp
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#pragma once
6
7#include <functional>
8#include <utility>
9
10#include <fmt/format.h>
11
12namespace wpi::util {
13
25
26class Logger {
27 public:
28 using LogFunc = std::function<void(unsigned int level, const char* file,
29 unsigned int line, const char* msg)>;
30
31 Logger() = default;
32 explicit Logger(LogFunc func) : m_func(std::move(func)) {}
33 Logger(LogFunc func, unsigned int min_level)
34 : m_func(std::move(func)), m_min_level(min_level) {}
35
36 void SetLogger(LogFunc func) { m_func = func; }
37
38 void set_min_level(unsigned int level) { m_min_level = level; }
39 unsigned int min_level() const { return m_min_level; }
40
41 void DoLog(unsigned int level, const char* file, unsigned int line,
42 const char* msg);
43
44 void LogV(unsigned int level, const char* file, unsigned int line,
45 fmt::string_view format, fmt::format_args args);
46
47 template <typename... Args>
48 void Log(unsigned int level, const char* file, unsigned int line,
49 fmt::string_view format, Args&&... args) {
50 if (m_func && level >= m_min_level) {
51 LogV(level, file, line, format, fmt::make_format_args(args...));
52 }
53 }
54
55 bool HasLogger() const { return m_func != nullptr; }
56
57 private:
58 LogFunc m_func;
59 unsigned int m_min_level = 20;
60};
61
62// C++20 relaxed the number of arguments to variadics, but Apple Clang's
63// warnings haven't caught up yet: https://stackoverflow.com/a/67996331
64#ifdef __clang__
65#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
66#endif
67
68#define WPI_LOG(logger_inst, level, format, ...) \
69 if ((logger_inst).HasLogger() && level >= (logger_inst).min_level()) { \
70 (logger_inst) \
71 .Log(level, __FILE__, __LINE__, format __VA_OPT__(, ) __VA_ARGS__); \
72 }
73
74#define WPI_ERROR(inst, format, ...) \
75 WPI_LOG(inst, ::wpi::util::WPI_LOG_ERROR, format __VA_OPT__(, ) __VA_ARGS__)
76#define WPI_WARNING(inst, format, ...) \
77 WPI_LOG(inst, ::wpi::util::WPI_LOG_WARNING, format __VA_OPT__(, ) __VA_ARGS__)
78#define WPI_INFO(inst, format, ...) \
79 WPI_LOG(inst, ::wpi::util::WPI_LOG_INFO, format __VA_OPT__(, ) __VA_ARGS__)
80#define WPI_DEBUG(inst, format, ...) \
81 WPI_LOG(inst, ::wpi::util::WPI_LOG_DEBUG, format __VA_OPT__(, ) __VA_ARGS__)
82#define WPI_DEBUG1(inst, format, ...) \
83 WPI_LOG(inst, ::wpi::util::WPI_LOG_DEBUG1, format __VA_OPT__(, ) __VA_ARGS__)
84#define WPI_DEBUG2(inst, format, ...) \
85 WPI_LOG(inst, ::wpi::util::WPI_LOG_DEBUG2, format __VA_OPT__(, ) __VA_ARGS__)
86#define WPI_DEBUG3(inst, format, ...) \
87 WPI_LOG(inst, ::wpi::util::WPI_LOG_DEBUG3, format __VA_OPT__(, ) __VA_ARGS__)
88#define WPI_DEBUG4(inst, format, ...) \
89 WPI_LOG(inst, ::wpi::util::WPI_LOG_DEBUG4, format __VA_OPT__(, ) __VA_ARGS__)
90
91} // namespace wpi::util
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition ThirdPartyNotices.txt:204
Logger(LogFunc func, unsigned int min_level)
Definition Logger.hpp:33
bool HasLogger() const
Definition Logger.hpp:55
void LogV(unsigned int level, const char *file, unsigned int line, fmt::string_view format, fmt::format_args args)
void set_min_level(unsigned int level)
Definition Logger.hpp:38
unsigned int min_level() const
Definition Logger.hpp:39
void DoLog(unsigned int level, const char *file, unsigned int line, const char *msg)
void Log(unsigned int level, const char *file, unsigned int line, fmt::string_view format, Args &&... args)
Definition Logger.hpp:48
Logger(LogFunc func)
Definition Logger.hpp:32
std::function< void(unsigned int level, const char *file, unsigned int line, const char *msg)> LogFunc
Definition Logger.hpp:28
void SetLogger(LogFunc func)
Definition Logger.hpp:36
FMT_INLINE auto format(locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4305
Definition StringMap.hpp:773
Definition raw_os_ostream.hpp:19
LogLevel
Definition Logger.hpp:14
@ WPI_LOG_WARNING
Definition Logger.hpp:17
@ WPI_LOG_CRITICAL
Definition Logger.hpp:15
@ WPI_LOG_DEBUG
Definition Logger.hpp:19
@ WPI_LOG_DEBUG4
Definition Logger.hpp:23
@ WPI_LOG_ERROR
Definition Logger.hpp:16
@ WPI_LOG_DEBUG3
Definition Logger.hpp:22
@ WPI_LOG_DEBUG1
Definition Logger.hpp:20
@ WPI_LOG_INFO
Definition Logger.hpp:18
@ WPI_LOG_DEBUG2
Definition Logger.hpp:21