-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcan_stack_logger.cpp
85 lines (70 loc) · 2.23 KB
/
can_stack_logger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//================================================================================================
/// @file can_stack_logger.cpp
///
/// @brief A class that acts as a logging sink. The intent is that someone could make their own
/// derived class of logger and inject it into the CAN stack to get helpful debug logging.
/// @author Adrian Del Grosso
///
/// @copyright 2022 The Open-Agriculture Developers
//================================================================================================
#include "can_stack_logger.hpp"
#include <iostream>
namespace isobus
{
CANStackLogger *CANStackLogger::logger = nullptr;
CANStackLogger::LoggingLevel CANStackLogger::currentLogLevel = LoggingLevel::Info;
Mutex CANStackLogger::loggerMutex;
#ifndef DISABLE_CAN_STACK_LOGGER
void CANStackLogger::CAN_stack_log(LoggingLevel level, const std::string &logText)
{
LOCK_GUARD(Mutex, loggerMutex);
CANStackLogger *canStackLogger = nullptr;
if ((get_can_stack_logger(canStackLogger)) &&
(level >= get_log_level()))
{
canStackLogger->sink_CAN_stack_log(level, logText);
}
}
void CANStackLogger::debug(const std::string &logText)
{
CAN_stack_log(LoggingLevel::Debug, logText);
}
void CANStackLogger::info(const std::string &logText)
{
CAN_stack_log(LoggingLevel::Info, logText);
}
void CANStackLogger::warn(const std::string &logText)
{
CAN_stack_log(LoggingLevel::Warning, logText);
}
void CANStackLogger::error(const std::string &logText)
{
CAN_stack_log(LoggingLevel::Error, logText);
}
void CANStackLogger::critical(const std::string &logText)
{
CAN_stack_log(LoggingLevel::Critical, logText);
}
#endif // DISABLE_CAN_STACK_LOGGER
void CANStackLogger::set_can_stack_logger_sink(CANStackLogger *logSink)
{
logger = logSink;
}
CANStackLogger::LoggingLevel CANStackLogger::get_log_level()
{
return currentLogLevel;
}
void CANStackLogger::set_log_level(LoggingLevel newLogLevel)
{
currentLogLevel = newLogLevel;
}
void CANStackLogger::sink_CAN_stack_log(LoggingLevel, const std::string &)
{
// Override this function to use the log sink
}
bool CANStackLogger::get_can_stack_logger(CANStackLogger *&canStackLogger)
{
canStackLogger = logger;
return (nullptr != canStackLogger);
}
} // namespace isobus