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
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 }
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(
93 "LibXR::Logger: explicit literal tag must be XR_FMT(...) or XR_PRINTF(...)");
94
95 constexpr auto frontend =
96 Detail::LoggerLiteral::SelectFrontend<Forced, Source, Args...>();
97 PublishSelected<frontend, Source>(level, file, line, std::forward<Args>(args)...);
98 }
99
100 private:
118 template <Detail::LoggerLiteral::Frontend FrontendMode, Print::Text Source,
119 typename... Args>
120 static void PublishSelected(LogLevel level, const char* file, uint32_t line,
121 Args&&... args)
122 {
123 if (!initialized_)
124 {
125 Init();
127
128 LogData data;
129 data.level = level;
130 data.file = file;
131 data.line = line;
132
133 if constexpr (FrontendMode == Detail::LoggerLiteral::Frontend::Format)
134 {
135 constexpr LibXR::Format<Source> format{};
136 auto written = Print::FormatIntoBuffer(data.message, sizeof(data.message), format,
137 std::forward<Args>(args)...);
138 UNUSED(written);
139 }
140 else
141 {
142 constexpr auto format = Print::Printf::Build<Source>();
143 auto written = Print::FormatIntoBuffer(data.message, sizeof(data.message), format,
144 std::forward<Args>(args)...);
145 UNUSED(written);
146 }
147
148 PublishToTopic(data);
149 }
150
156 static void PublishToTopic(LogData& data);
157
158 static inline bool initialized_ =
159 false;
161};
162
163} // namespace LibXR
164
168#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:120
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_
Definition logger.hpp:158
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:123
consteval Frontend SelectFrontend()
Selects the final logger frontend after validating the resolution result.
Definition logger.hpp:199
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:17