libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
logger.hpp
1#pragma once
2
3#include <cstdint>
4#include <utility>
5
6#include "libxr_color.hpp"
7#include "libxr_rw.hpp"
8#include "libxr_time.hpp"
9
10namespace LibXR
11{
12
25#include "literal.hpp"
26
31enum class LogLevel : uint8_t
32{
38};
39
44struct LogData
45{
47 const char* file;
48 uint32_t line;
49 char message[XR_LOG_MESSAGE_MAX_LEN];
50};
51
56class Logger
57{
58 public:
62 static void Init();
63
71 template <Print::Text Source, typename... Args>
72 static void Publish(LogLevel level, const char* file, uint32_t line, Args&&... args)
73 {
74 constexpr auto frontend =
76 Source, Args...>();
77 PublishSelected<frontend, Source>(level, file, line, std::forward<Args>(args)...);
78 }
79
88 template <Detail::LoggerLiteral::Frontend Forced, Print::Text Source, typename... Args>
89 static void Publish(LogLevel level, const char* file, uint32_t line, Args&&... args)
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 }
98
99 private:
117 template <Detail::LoggerLiteral::Frontend FrontendMode, Print::Text Source,
118 typename... Args>
119 static void PublishSelected(LogLevel level, const char* file, uint32_t line,
120 Args&&... args)
121 {
122 if (!initialized_)
123 {
124 Init();
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 }
149
155 static void PublishToTopic(LogData& data);
156
157 static inline bool initialized_ = false;
158};
159
160} // namespace LibXR
161
165#include "macros.hpp"
日志管理器 / LibXR Logger Manager
Definition logger.hpp:57
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
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 void Publish(LogLevel level, const char *file, uint32_t line, Args &&... args)
发布一条字面量日志 / Publish one literal log message
Definition logger.hpp:72
static bool initialized_
是否已经完成日志 topic 初始化 / Whether logger-topic initialization has completed.
Definition logger.hpp:157
static void Publish(LogLevel level, const char *file, uint32_t line, Args &&... args)
发布一条带显式前端标签的字面量日志 / Publish one literal log message with an explicit frontend tag
Definition logger.hpp:89
static consteval Compiled< Source > Build()
在编译期解析并校验 printf 格式串 / Parse and validate a printf format at compile time
Definition printf.hpp:101
consteval Frontend SelectFrontend()
Selects the final logger frontend after validating the resolution result.
Definition logger.hpp:198
Frontend
Logger literal frontend selection mode.
Definition logger.hpp:20
@ Auto
select brace or printf automatically / 自动选择 brace 或 printf
@ Format
force brace-style frontend / 强制使用 brace 风格前端
LibXR 命名空间
Definition ch32_can.hpp:14
LogLevel
日志级别枚举 / Log level enumeration
Definition logger.hpp:32
@ 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
日志数据结构体 / 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
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
Definition printf.hpp:16