libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Logger Class Reference

日志管理器 / LibXR Logger Manager More...

#include <logger.hpp>

Static Public Member Functions

static void Init ()
 初始化日志主题 / Initialize the log topic
 
static void Publish (LogLevel level, const char *file, uint32_t line, const char *fmt,...)
 发布一条日志 / Publish a log message
 

Static Private Member Functions

static void PrintToTerminal (const LogData &data)
 打印日志到终端 / Print log to terminal
 
static const char * GetColor (LogLevel level)
 根据日志级别获取显示颜色 / Get color code based on log level
 
static const char * LevelToString (LogLevel level)
 将日志级别转换为字符串 / Convert log level to string
 

Static Private Attributes

static bool initialized_ = false
 

Detailed Description

日志管理器 / LibXR Logger Manager

Definition at line 44 of file logger.hpp.

Member Function Documentation

◆ GetColor()

const char * Logger::GetColor ( LogLevel level)
staticprivate

根据日志级别获取显示颜色 / Get color code based on log level

Parameters
level日志级别 / Log level
Returns
颜色字符串 / Color string

Definition at line 73 of file logger.cpp.

74{
75 switch (level)
76 {
78 return LIBXR_FONT_STR[static_cast<uint8_t>(Font::MAGENTA)];
80 return LIBXR_FONT_STR[static_cast<uint8_t>(Font::CYAN)];
82 return LIBXR_FONT_STR[static_cast<uint8_t>(Font::GREEN)];
84 return LIBXR_FONT_STR[static_cast<uint8_t>(Font::YELLOW)];
86 return LIBXR_FONT_STR[static_cast<uint8_t>(Font::RED)];
87 default:
88 return "";
89 }
90}
constexpr const char * LIBXR_FONT_STR[]
ANSI escape sequences for font color / ANSI转义序列 - 字体颜色
@ XR_LOG_LEVEL_INFO
一般信息 / Informational message
@ XR_LOG_LEVEL_DEBUG
调试信息 / Debug message
@ XR_LOG_LEVEL_WARN
警告信息 / Warning message
@ XR_LOG_LEVEL_ERROR
错误信息 / Error message
@ XR_LOG_LEVEL_PASS
通过信息 / Pass message

◆ Init()

void Logger::Init ( )
static

初始化日志主题 / Initialize the log topic

Definition at line 15 of file logger.cpp.

16{
17 log_topic = Topic::CreateTopic<LogData>("/xr/log", nullptr, false, true);
18
19#if LIBXR_PRINTF_BUFFER_SIZE > 0
20 void (*log_cb_fun)(bool in_isr, Topic, RawData &log_data) =
21 [](bool, Topic tp, LibXR::RawData &log_data)
22 {
23 UNUSED(tp);
24
25 auto log = reinterpret_cast<LogData *>(log_data.addr_);
26
27 if (LIBXR_LOG_OUTPUT_LEVEL >= static_cast<uint8_t>(log->level) && STDIO::write_ &&
29 {
30 PrintToTerminal(*log);
31 }
32 };
33
34 auto log_cb = LibXR::Topic::Callback::Create(log_cb_fun, log_topic);
35 log_topic.RegisterCallback(log_cb);
36#endif
37
38 initialized_ = true;
39}
static Callback Create(FunType fun, ArgType arg)
Definition libxr_cb.hpp:142
static void PrintToTerminal(const LogData &data)
打印日志到终端 / Print log to terminal
Definition logger.cpp:64
原始数据封装类。 A class for encapsulating raw data.
void * addr_
数据存储地址。 The storage address of the data.
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:531
主题(Topic)管理类 / Topic management class
Definition message.hpp:28
static Topic CreateTopic(const char *name, Domain *domain=nullptr, bool cache=false, bool check_length=true)
创建一个新的主题 Creates a new topic
Definition message.hpp:494
void RegisterCallback(Callback &cb)
注册回调函数 Registers a callback function
Definition message.cpp:51
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
Definition libxr_rw.cpp:208
日志数据结构体 / Log data structure
Definition logger.hpp:32

◆ LevelToString()

const char * Logger::LevelToString ( LogLevel level)
staticprivate

将日志级别转换为字符串 / Convert log level to string

Parameters
level日志级别 / Log level
Returns
日志级别字符串 / Log level string

Definition at line 92 of file logger.cpp.

93{
94 switch (level)
95 {
97 return "D";
99 return "I";
101 return "P";
103 return "W";
105 return "E";
106 default:
107 return "UNKNOWN";
108 }
109}

◆ PrintToTerminal()

void Logger::PrintToTerminal ( const LogData & data)
staticprivate

打印日志到终端 / Print log to terminal

Parameters
data日志数据 / Log data

Definition at line 64 of file logger.cpp.

65{
66 const char *color = GetColor(data.level);
67
68 STDIO::Printf("%s%s [%u](%s:%u) %s%s\r\n", color, LevelToString(data.level),
69 static_cast<uint32_t>(data.timestamp), data.file, data.line, data.message,
70 LIBXR_FORMAT_STR[static_cast<uint8_t>(Format::RESET)]);
71}
static const char * LevelToString(LogLevel level)
将日志级别转换为字符串 / Convert log level to string
Definition logger.cpp:92
static const char * GetColor(LogLevel level)
根据日志级别获取显示颜色 / Get color code based on log level
Definition logger.cpp:73
static int Printf(const char *fmt,...)
Prints a formatted string to the write port (like printf).
Definition libxr_rw.cpp:335
constexpr const char * LIBXR_FORMAT_STR[]
ANSI escape sequences for text format / ANSI转义序列 - 文本格式
uint32_t line
行号 / Line number
Definition logger.hpp:36
const char * file
文件名指针 / Source file name pointer
Definition logger.hpp:35
LogLevel level
日志级别 / Log level
Definition logger.hpp:34
MillisecondTimestamp timestamp
时间戳 / Timestamp
Definition logger.hpp:33
char message[XR_LOG_MESSAGE_MAX_LEN]
日志消息内容 / Log message content
Definition logger.hpp:37

◆ Publish()

void Logger::Publish ( LogLevel level,
const char * file,
uint32_t line,
const char * fmt,
... )
static

发布一条日志 / Publish a log message

Parameters
level日志级别 / Log level
file来源文件名 / Source file name
line行号 / Line number
fmt格式化字符串 / Format string
...可变参数 / Variable arguments

Definition at line 42 of file logger.cpp.

44{
45 if (!initialized_)
46 {
47 Init();
48 }
49 LogData data;
51 data.level = level;
52 data.file = file;
53 data.line = line;
54
55 va_list args;
56 va_start(args, fmt);
57 auto ans = vsnprintf(data.message, sizeof(data.message), fmt, args);
58 UNUSED(ans);
59 va_end(args);
60
61 log_topic.Publish(data);
62}
static void Init()
初始化日志主题 / Initialize the log topic
Definition logger.cpp:15
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:41
void Publish(Data &data)
发布数据 Publishes data
Definition message.hpp:554

Field Documentation

◆ initialized_

bool LibXR::Logger::initialized_ = false
inlinestaticprivate

Definition at line 85 of file logger.hpp.


The documentation for this class was generated from the following files: