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
 
template<Print::Text Source, typename... Args>
static void Publish (LogLevel level, const char *file, uint32_t line, Args &&... args)
 发布一条字面量日志 / Publish one literal log message
 
template<Detail::LoggerLiteral::Frontend Forced, Print::Text Source, typename... Args>
static void Publish (LogLevel level, const char *file, uint32_t line, Args &&... args)
 发布一条带显式前端标签的字面量日志 / Publish one literal log message with an explicit frontend tag
 

Static Private Member Functions

template<Detail::LoggerLiteral::Frontend FrontendMode, Print::Text Source, typename... Args>
static void PublishSelected (LogLevel level, const char *file, uint32_t line, Args &&... args)
 按已确定前端发布一条日志 Publish one log entry under the already selected frontend
 
static void PublishToTopic (LogData &data)
 把一条日志数据发布到内部日志 topic Publish one log record into the internal log topic
 

Static Private Attributes

static bool initialized_ = false
 是否已经完成日志 topic 初始化 / Whether logger-topic initialization has completed.
 

Detailed Description

日志管理器 / LibXR Logger Manager

Definition at line 56 of file logger.hpp.

Member Function Documentation

◆ Init()

void Logger::Init ( )
static

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

初始化 logger 的内部 topic 与终端订阅路径 Initialize the logger's internal topic and terminal subscriber path

Note
这里只做一次性初始化:创建 /xr/log topic,并注册一个把日志打印到 STDIO 的 callback。 This performs one-time initialization only: create the /xr/log topic and register one callback that prints logs to STDIO.

Definition at line 119 of file logger.cpp.

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}
static bool initialized_
是否已经完成日志 topic 初始化 / Whether logger-topic initialization has completed.
Definition logger.hpp:157
static Callback Create(Callable fun, BoundArg arg)
创建一个回调订阅句柄 / Create a callback subscription handle
Definition callback.hpp:438
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

◆ Publish() [1/2]

template<Print::Text Source, typename... Args>
static void LibXR::Logger::Publish ( LogLevel level,
const char * file,
uint32_t line,
Args &&... args )
inlinestatic

发布一条字面量日志 / Publish one literal log message

Parameters
level日志级别 / Log level
file来源文件名 / Source file name
line行号 / Line number
args格式参数 / Format arguments

Definition at line 72 of file logger.hpp.

73 {
74 constexpr auto frontend =
76 Source, Args...>();
77 PublishSelected<frontend, Source>(level, file, line, std::forward<Args>(args)...);
78 }
static void PublishSelected(LogLevel level, const char *file, uint32_t line, Args &&... args)
按已确定前端发布一条日志 Publish one log entry under the already selected frontend
Definition logger.hpp:119
consteval Frontend SelectFrontend()
Selects the final logger frontend after validating the resolution result.
Definition logger.hpp:198
@ Auto
select brace or printf automatically / 自动选择 brace 或 printf

◆ Publish() [2/2]

template<Detail::LoggerLiteral::Frontend Forced, Print::Text Source, typename... Args>
static void LibXR::Logger::Publish ( LogLevel level,
const char * file,
uint32_t line,
Args &&... args )
inlinestatic

发布一条带显式前端标签的字面量日志 / Publish one literal log message with an explicit frontend tag

Parameters
level日志级别 / Log level
file来源文件名 / Source file name
line行号 / Line number
args格式参数 / Format arguments

Definition at line 89 of file logger.hpp.

90 {
91 static_assert(Forced != Detail::LoggerLiteral::Frontend::Auto,
92 "LibXR::Logger: explicit literal tag must be XR_FMT(...) or XR_PRINTF(...)");
93
94 constexpr auto frontend =
95 Detail::LoggerLiteral::SelectFrontend<Forced, Source, Args...>();
96 PublishSelected<frontend, Source>(level, file, line, std::forward<Args>(args)...);
97 }

◆ PublishSelected()

template<Detail::LoggerLiteral::Frontend FrontendMode, Print::Text Source, typename... Args>
static void LibXR::Logger::PublishSelected ( LogLevel level,
const char * file,
uint32_t line,
Args &&... args )
inlinestaticprivate

按已确定前端发布一条日志 Publish one log entry under the already selected frontend

Template Parameters
FrontendMode已选定的日志字面量前端 / Already selected literal frontend
Source日志源串 / Log source literal
Args格式参数类型 / Format argument types
Parameters
level日志级别 / Log level
file来源文件名 / Source file name
line行号 / Line number
args格式参数 / Format arguments
Note
这里负责惰性初始化日志主题,并把格式化后的文本写进固定 LogData 缓冲区; 真正的 topic 发布留给 PublishToTopic()。 This path performs lazy logger-topic initialization and formats the final text into the fixed LogData buffer; the actual topic publish is delegated to PublishToTopic().

Definition at line 119 of file logger.hpp.

121 {
122 if (!initialized_)
123 {
124 Init();
125 }
126
127 LogData data;
128 data.level = level;
129 data.file = file;
130 data.line = line;
131
132 if constexpr (FrontendMode == Detail::LoggerLiteral::Frontend::Format)
133 {
134 constexpr LibXR::Format<Source> format{};
135 auto written = Print::FormatIntoBuffer(data.message, sizeof(data.message), format,
136 std::forward<Args>(args)...);
137 UNUSED(written);
138 }
139 else
140 {
141 constexpr auto format = Print::Printf::Build<Source>();
142 auto written = Print::FormatIntoBuffer(data.message, sizeof(data.message), format,
143 std::forward<Args>(args)...);
144 UNUSED(written);
145 }
146
147 PublishToTopic(data);
148 }
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 consteval Compiled< Source > Build()
在编译期解析并校验 printf 格式串 / Parse and validate a printf format at compile time
Definition printf.hpp:101
@ Format
force brace-style frontend / 强制使用 brace 风格前端

◆ PublishToTopic()

void Logger::PublishToTopic ( LogData & data)
staticprivate

把一条日志数据发布到内部日志 topic Publish one log record into the internal log topic

把一条日志发布到内部日志 topic Publish one log record into the internal log topic

Parameters
data待发布日志 / Log record to publish

Definition at line 134 of file logger.cpp.

134{ log_topic.Publish(data); }

Field Documentation

◆ initialized_

bool LibXR::Logger::initialized_ = false
inlinestaticprivate

是否已经完成日志 topic 初始化 / Whether logger-topic initialization has completed.

Definition at line 157 of file logger.hpp.


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