2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#ifndef WPIUTIL_WPI_LOGGER_H_
|
|
|
|
|
#define WPIUTIL_WPI_LOGGER_H_
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
#include <functional>
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
2016-09-10 21:51:16 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include "fmt/format.h"
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
|
|
|
|
|
enum LogLevel {
|
|
|
|
|
WPI_LOG_CRITICAL = 50,
|
|
|
|
|
WPI_LOG_ERROR = 40,
|
|
|
|
|
WPI_LOG_WARNING = 30,
|
|
|
|
|
WPI_LOG_INFO = 20,
|
|
|
|
|
WPI_LOG_DEBUG = 10,
|
|
|
|
|
WPI_LOG_DEBUG1 = 9,
|
|
|
|
|
WPI_LOG_DEBUG2 = 8,
|
|
|
|
|
WPI_LOG_DEBUG3 = 7,
|
|
|
|
|
WPI_LOG_DEBUG4 = 6
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
public:
|
2018-09-26 00:09:25 -07:00
|
|
|
using LogFunc = std::function<void(unsigned int level, const char* file,
|
|
|
|
|
unsigned int line, const char* msg)>;
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2017-07-28 18:08:31 -07:00
|
|
|
Logger() = default;
|
2020-12-28 10:12:52 -08:00
|
|
|
explicit Logger(LogFunc func) : m_func(std::move(func)) {}
|
|
|
|
|
Logger(LogFunc func, unsigned int min_level)
|
|
|
|
|
: m_func(std::move(func)), m_min_level(min_level) {}
|
2017-07-28 18:08:31 -07:00
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
void SetLogger(LogFunc func) { m_func = func; }
|
|
|
|
|
|
|
|
|
|
void set_min_level(unsigned int level) { m_min_level = level; }
|
|
|
|
|
unsigned int min_level() const { return m_min_level; }
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void DoLog(unsigned int level, const char* file, unsigned int line,
|
|
|
|
|
const char* msg);
|
|
|
|
|
|
|
|
|
|
void LogV(unsigned int level, const char* file, unsigned int line,
|
|
|
|
|
fmt::string_view format, fmt::format_args args);
|
|
|
|
|
|
|
|
|
|
template <typename S, typename... Args>
|
2016-09-25 17:23:39 -07:00
|
|
|
void Log(unsigned int level, const char* file, unsigned int line,
|
2021-06-06 16:13:58 -07:00
|
|
|
const S& format, Args&&... args) {
|
|
|
|
|
if (m_func && level >= m_min_level) {
|
|
|
|
|
LogV(level, file, line, format,
|
|
|
|
|
fmt::make_args_checked<Args...>(format, args...));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HasLogger() const { return m_func != nullptr; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LogFunc m_func;
|
|
|
|
|
unsigned int m_min_level = 20;
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_LOG(logger_inst, level, format, ...) \
|
|
|
|
|
logger_inst.Log(level, __FILE__, __LINE__, FMT_STRING(format), __VA_ARGS__)
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_ERROR(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_WARNING(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_INFO(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_INFO, format, __VA_ARGS__)
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG(inst, format, ...) \
|
|
|
|
|
do { \
|
2017-10-21 20:31:20 -07:00
|
|
|
} while (0)
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG1(inst, format, ...) \
|
|
|
|
|
do { \
|
2017-10-21 20:31:20 -07:00
|
|
|
} while (0)
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG2(inst, format, ...) \
|
|
|
|
|
do { \
|
2017-10-21 20:31:20 -07:00
|
|
|
} while (0)
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG3(inst, format, ...) \
|
|
|
|
|
do { \
|
2017-10-21 20:31:20 -07:00
|
|
|
} while (0)
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG4(inst, format, ...) \
|
|
|
|
|
do { \
|
2017-10-21 20:31:20 -07:00
|
|
|
} while (0)
|
2016-09-25 17:23:39 -07:00
|
|
|
#else
|
2021-06-06 16:13:58 -07:00
|
|
|
#define WPI_DEBUG(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_DEBUG1(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_DEBUG2(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_DEBUG3(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, format, __VA_ARGS__)
|
|
|
|
|
#define WPI_DEBUG4(inst, format, ...) \
|
|
|
|
|
WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, format, __VA_ARGS__)
|
2016-09-25 17:23:39 -07:00
|
|
|
#endif
|
|
|
|
|
|
2017-10-21 20:31:20 -07:00
|
|
|
} // namespace wpi
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#endif // WPIUTIL_WPI_LOGGER_H_
|