libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
logger.cpp
1#include "logger.hpp"
2
3#include <cstdint>
4
5#include "libxr_def.hpp"
6#include "libxr_rw.hpp"
7#include "message.hpp"
8
9using namespace LibXR;
10
11namespace
12{
13static Topic log_topic;
14
22const char* GetLogColor(LogLevel level)
23{
24 switch (level)
25 {
26 case LogLevel::XR_LOG_LEVEL_DEBUG:
27 return LIBXR_FOREGROUND_STR[static_cast<uint8_t>(Foreground::MAGENTA)];
28 case LogLevel::XR_LOG_LEVEL_INFO:
29 return LIBXR_FOREGROUND_STR[static_cast<uint8_t>(Foreground::CYAN)];
30 case LogLevel::XR_LOG_LEVEL_PASS:
31 return LIBXR_FOREGROUND_STR[static_cast<uint8_t>(Foreground::GREEN)];
32 case LogLevel::XR_LOG_LEVEL_WARN:
33 return LIBXR_FOREGROUND_STR[static_cast<uint8_t>(Foreground::YELLOW)];
34 case LogLevel::XR_LOG_LEVEL_ERROR:
35 return LIBXR_FOREGROUND_STR[static_cast<uint8_t>(Foreground::RED)];
36 default:
37 return "";
38 }
39}
40
48const char* LogLevelToString(LogLevel level)
49{
50 switch (level)
51 {
52 case LogLevel::XR_LOG_LEVEL_DEBUG:
53 return "D";
54 case LogLevel::XR_LOG_LEVEL_INFO:
55 return "I";
56 case LogLevel::XR_LOG_LEVEL_PASS:
57 return "P";
58 case LogLevel::XR_LOG_LEVEL_WARN:
59 return "W";
60 case LogLevel::XR_LOG_LEVEL_ERROR:
61 return "E";
62 default:
63 return "UNKNOWN";
64 }
65}
66
77void PrintLogToTerminal(const LogData& data, MicrosecondTimestamp timestamp)
78{
79 const char* color = GetLogColor(data.level);
80 const uint32_t timestamp_ms =
81 static_cast<uint32_t>(static_cast<uint64_t>(timestamp) / 1000U);
82 STDIO::Print<"{}{} [{}]({}:{}) {}{}\r\n">(
83 color, LogLevelToString(data.level), timestamp_ms, data.file, data.line,
84 data.message,
85 LIBXR_TERMINAL_CONTROL_STR[static_cast<uint8_t>(TerminalControl::RESET)]);
86}
87
96void OnLogMessage(bool, Topic tp, const Topic::MessageView<LogData>& log_message)
97{
98 UNUSED(tp);
99
100 ASSERT(log_message.data != nullptr);
101
102 if (LIBXR_LOG_OUTPUT_LEVEL >= static_cast<uint8_t>(log_message.data->level) &&
104 {
105 PrintLogToTerminal(*log_message.data, log_message.timestamp);
106 }
107}
108} // namespace
109
120{
121 log_topic = Topic::CreateTopic<LogData>("/xr/log", nullptr, true);
122
123 auto log_cb = LibXR::Topic::Callback::Create(OnLogMessage, log_topic);
124 log_topic.RegisterCallback(log_cb);
125
126 initialized_ = true;
127}
128
134void Logger::PublishToTopic(LogData& data) { log_topic.Publish(data); }
static void PublishToTopic(LogData &data)
把一条日志数据发布到内部日志 topic Publish one log record into the internal log topic
Definition logger.cpp:134
static void Init()
初始化日志主题 / Initialize the log topic
Definition logger.cpp:119
static bool initialized_
是否已经完成日志 topic 初始化 / Whether logger-topic initialization has completed.
Definition logger.hpp:157
微秒时间戳 / Microsecond timestamp
static int Print(Args &&... args)
将一个编译期 brace 字面量打印到当前 STDIO 输出 / Print one compile-time brace literal to the active STDIO output
Definition stdio.hpp:196
static WritePort * write_
Write port instance. 写入端口。
Definition stdio.hpp:26
static Callback Create(Callable fun, BoundArg arg)
创建一个回调订阅句柄 / Create a callback subscription handle
Definition callback.hpp:438
发布订阅主题 / Publish-subscribe topic
Definition topic.hpp:57
void Publish(Data &data)
在普通上下文里发布一条消息,并自动取当前时间戳 / Publish one message in normal context and stamp it with the current time
Definition topic.hpp:409
static Topic CreateTopic(const char *name, Domain *domain=nullptr, bool multi_publisher=false)
用精确类型创建或查找一个 topic / Create or look up one topic using one exact payload type
Definition topic.hpp:327
void RegisterCallback(Callback &cb)
注册一个回调订阅者 / Register one callback subscriber
Definition callback.hpp:506
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
LibXR 命名空间
Definition ch32_can.hpp:14
constexpr const char * LIBXR_TERMINAL_CONTROL_STR[]
ANSI escape sequences for terminal controls / ANSI转义序列 - 终端控制
constexpr const char * LIBXR_FOREGROUND_STR[]
ANSI escape sequences for foreground colors / ANSI转义序列 - 前景色
LogLevel
日志级别枚举 / Log level enumeration
Definition logger.hpp:32
日志数据结构体 / Log data structure
Definition logger.hpp:45
uint32_t line
行号 / Line number
Definition logger.hpp:48
const char * file
文件名指针 / Source file name pointer
Definition logger.hpp:47
LogLevel level
日志级别 / Log level
Definition logger.hpp:46
char message[XR_LOG_MESSAGE_MAX_LEN]
日志消息内容 / Log message content
Definition logger.hpp:49
带时间戳和 payload 指针的只读消息视图 / Read-only message view carrying a timestamp and a payload pointer
Definition topic.hpp:126
Data * data
指向本次发布 payload 对象的指针。Pointer to the payload object of this publish.
Definition topic.hpp:129
MicrosecondTimestamp timestamp
消息时间戳。Message timestamp.
Definition topic.hpp:128