10#ifndef CAN_STACK_LOGGER_HPP
11#define CAN_STACK_LOGGER_HPP
13#include "isobus/utility/thread_synchronization.hpp"
47#ifndef DISABLE_CAN_STACK_LOGGER
58 template<
typename... Args>
61 int size_s = std::snprintf(
nullptr, 0, format.c_str(), args...) + 1;
64 auto size =
static_cast<std::size_t
>(size_s);
65 std::unique_ptr<char[]> buf(
new char[size]);
66 std::snprintf(buf.get(), size, format.c_str(), args...);
67 CAN_stack_log(level, std::string(buf.get(), buf.get() + size - 1));
77 static void debug(
const std::string &logText);
82 template<
typename... Args>
83 static void debug(
const std::string &format, Args... args)
90 static void info(
const std::string &logText);
95 template<
typename... Args>
96 static void info(
const std::string &format, Args... args)
103 static void warn(
const std::string &logText);
108 template<
typename... Args>
109 static void warn(
const std::string &format, Args... args)
116 static void error(
const std::string &logText);
121 template<
typename... Args>
122 static void error(
const std::string &format, Args... args)
129 static void critical(
const std::string &logText);
134 template<
typename... Args>
135 static void critical(
const std::string &format, Args... args)
176#ifdef DISABLE_CAN_STACK_LOGGER
179#define LOG_CRITICAL(...)
182#define LOG_ERROR(...)
185#define LOG_WARNING(...)
191#define LOG_DEBUG(...)
196#define LOG_CRITICAL(...) isobus::CANStackLogger::critical(__VA_ARGS__)
200#define LOG_ERROR(...) isobus::CANStackLogger::error(__VA_ARGS__)
204#define LOG_WARNING(...) isobus::CANStackLogger::warn(__VA_ARGS__)
208#define LOG_INFO(...) isobus::CANStackLogger::info(__VA_ARGS__)
212#define LOG_DEBUG(...) isobus::CANStackLogger::debug(__VA_ARGS__)
A base class for a CAN logger, used to get diagnostic info from the CAN stack.
CANStackLogger()=default
The constructor for a CANStackLogger.
virtual void sink_CAN_stack_log(LoggingLevel level, const std::string &logText)
Override this to make a log sink for your application.
static void error(const std::string &logText)
Logs a string to the log sink with Error severity. Wraps sink_CAN_stack_log.
LoggingLevel
Enumerates the various log message severities.
@ Warning
Warnings indicate issues that do not stop normal operation, but should be noted for troubleshooting.
@ Critical
Critical issues are fundamental problems that must be solved for the stack to work properly.
@ Info
General status info messages and messages about how things are working under normal conditions.
@ Error
Errors are issues that interrupt normal operation.
@ Debug
Verbose information.
static void CAN_stack_log(LoggingLevel level, const std::string &format, Args... args)
Gets called from the CAN stack to log information. Wraps sink_CAN_stack_log.
static void info(const std::string &format, Args... args)
Logs a printf formatted string to the log sink with Info severity. Wraps sink_CAN_stack_log.
static void warn(const std::string &format, Args... args)
Logs a printf formatted string to the log sink with Warning severity. Wraps sink_CAN_stack_log.
static void critical(const std::string &format, Args... args)
Logs a printf formatted string to the log sink with Critical severity. Wraps sink_CAN_stack_log.
static void info(const std::string &logText)
Logs a string to the log sink with Info severity. Wraps sink_CAN_stack_log.
static void error(const std::string &format, Args... args)
Logs a printf formatted string to the log sink with Error severity. Wraps sink_CAN_stack_log.
static Mutex loggerMutex
A mutex that protects the logger so it can be used from multiple threads.
static void debug(const std::string &format, Args... args)
Logs a printf formatted string to the log sink with Debug severity. Wraps sink_CAN_stack_log.
static bool get_can_stack_logger(CANStackLogger *&canStackLogger)
Provides a pointer to the static instance of the logger, and returns if the pointer is valid.
static LoggingLevel currentLogLevel
The current log level. Logs for levels below the current one will be dropped.
static void set_can_stack_logger_sink(CANStackLogger *logSink)
Assigns a derived logger class to be used as the log sink.
static void debug(const std::string &logText)
Logs a string to the log sink with Debug severity. Wraps sink_CAN_stack_log.
~CANStackLogger()=default
The destructor for a CANStackLogger.
static CANStackLogger * logger
A static pointer to an instance of a logger.
static void critical(const std::string &logText)
Logs a string to the log sink with Critical severity. Wraps sink_CAN_stack_log.
static void set_log_level(LoggingLevel newLogLevel)
Sets the current logging level.
static void CAN_stack_log(LoggingLevel level, const std::string &logText)
Gets called from the CAN stack to log information. Wraps sink_CAN_stack_log.
static void warn(const std::string &logText)
Logs a string to the log sink with Warning severity. Wraps sink_CAN_stack_log.
static LoggingLevel get_log_level()
Returns the current logging level.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...