AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
can_stack_logger.hpp
Go to the documentation of this file.
1//================================================================================================
9//================================================================================================
10#ifndef CAN_STACK_LOGGER_HPP
11#define CAN_STACK_LOGGER_HPP
12
13#include "isobus/utility/thread_synchronization.hpp"
14
15#include <memory>
16#include <string>
17
18namespace isobus
19{
20 //================================================================================================
27 //================================================================================================
29 {
30 public:
32 enum class LoggingLevel
33 {
34 Debug = 0,
35 Info,
36 Warning,
37 Error,
39 };
40
42 CANStackLogger() = default;
43
45 ~CANStackLogger() = default;
46
47#ifndef DISABLE_CAN_STACK_LOGGER
48
52 static void CAN_stack_log(LoggingLevel level, const std::string &logText);
53
58 template<typename... Args>
59 static void CAN_stack_log(LoggingLevel level, const std::string &format, Args... args)
60 {
61 int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
62 if (size_s > 0)
63 {
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)); // We don't want the '\0' inside
68 }
69 else if (size_s < 0)
70 {
71 CAN_stack_log(level, format); // If snprintf had some error, at least print the format string
72 }
73 }
74
77 static void debug(const std::string &logText);
78
82 template<typename... Args>
83 static void debug(const std::string &format, Args... args)
84 {
85 CAN_stack_log(LoggingLevel::Debug, format, args...);
86 }
87
90 static void info(const std::string &logText);
91
95 template<typename... Args>
96 static void info(const std::string &format, Args... args)
97 {
98 CAN_stack_log(LoggingLevel::Info, format, args...);
99 }
100
103 static void warn(const std::string &logText);
104
108 template<typename... Args>
109 static void warn(const std::string &format, Args... args)
110 {
111 CAN_stack_log(LoggingLevel::Warning, format, args...);
112 }
113
116 static void error(const std::string &logText);
117
121 template<typename... Args>
122 static void error(const std::string &format, Args... args)
123 {
124 CAN_stack_log(LoggingLevel::Error, format, args...);
125 }
126
129 static void critical(const std::string &logText);
130
134 template<typename... Args>
135 static void critical(const std::string &format, Args... args)
136 {
137 CAN_stack_log(LoggingLevel::Critical, format, args...);
138 }
139
140#endif
141
144 static void set_can_stack_logger_sink(CANStackLogger *logSink);
145
151
156 static void set_log_level(LoggingLevel newLogLevel);
157
161 virtual void sink_CAN_stack_log(LoggingLevel level, const std::string &logText);
162
163 private:
167 static bool get_can_stack_logger(CANStackLogger *&canStackLogger);
168
171 static Mutex loggerMutex;
172 };
173} // namespace isobus
174
176#ifdef DISABLE_CAN_STACK_LOGGER
179#define LOG_CRITICAL(...)
182#define LOG_ERROR(...)
185#define LOG_WARNING(...)
188#define LOG_INFO(...)
191#define LOG_DEBUG(...)
192#else
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__)
213#endif
215
216#endif // CAN_STACK_LOGGER_HPP
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.
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...