SimpleKernel 1.17.0
Loading...
Searching...
No Matches
klog::detail Namespace Reference

Classes

struct  LogEntry
 存储于 MPMC 队列中的日志条目 More...
 

Enumerations

enum class  Level : uint8_t { kDebug = 0 , kInfo = 1 , kWarn = 2 , kErr = 3 }
 日志级别 More...
 

Functions

__always_inline auto PutStr (const char *s) -> void
 通过 etl_putchar 输出字符串(空指针安全)
 
auto TryDrain () -> void
 将队列中所有条目输出至串口
 
template<Level Lvl, typename... Args>
__always_inline auto Log (etl::format_string< Args... > fmt, Args &&... args) -> void
 核心实现:格式化消息并入队,随后尝试排空
 

Variables

constexpr auto kMinLevel
 编译期最低日志级别
 
constexpr const char * kLevelLabel []
 日志级别标签(定宽,对齐输出)
 
constexpr const char * kLevelColor []
 日志级别对应的颜色(按 Level 枚举索引)
 
mpmc_queue::MPMCQueue< LogEntry, 256 > log_queue
 全局日志队列(64 KB 静态内存,constexpr 构造)
 
std::atomic< uint64_t > log_seq {0}
 用于跨核心排序的单调递增序列计数器
 
std::atomic_flag drain_flag = ATOMIC_FLAG_INIT
 单消费者排空保护(非阻塞 try-lock)
 
std::atomic< uint64_t > dropped_count {0}
 队列满时丢弃的条目计数
 
ANSI 转义码
constexpr auto kReset = "\033[0m"
 
constexpr auto kRed = "\033[31m"
 
constexpr auto kGreen = "\033[32m"
 
constexpr auto kYellow = "\033[33m"
 
constexpr auto kCyan = "\033[36m"
 

Enumeration Type Documentation

◆ Level

enum class klog::detail::Level : uint8_t
strong

日志级别

Enumerator
kDebug 
kInfo 
kWarn 
kErr 

Definition at line 29 of file kernel_log.hpp.

29 : uint8_t {
30 kDebug = 0,
31 kInfo = 1,
32 kWarn = 2,
33 kErr = 3,
34};

Function Documentation

◆ Log()

template<Level Lvl, typename... Args>
__always_inline auto klog::detail::Log ( etl::format_string< Args... >  fmt,
Args &&...  args 
) -> void

核心实现:格式化消息并入队,随后尝试排空

Template Parameters
Lvl编译期日志级别,低于 kMinLevel 时整个函数被编译器消除
Args可变格式化参数类型

Definition at line 143 of file kernel_log.hpp.

144 {
145 if constexpr (Lvl < kMinLevel) {
146 return;
147 }
148
149 LogEntry entry{};
150 entry.seq = log_seq.fetch_add(1, std::memory_order_relaxed);
151 entry.core_id = cpu_io::GetCurrentCoreId();
152 entry.level = Lvl;
153 auto* end = etl::format_to_n(entry.msg, sizeof(entry.msg) - 1, fmt,
154 static_cast<Args&&>(args)...);
155 *end = '\0';
156
157 if (!log_queue.push(entry)) {
158 // 队列满:尝试排空后重试一次
159 TryDrain();
160 if (!log_queue.push(entry)) {
161 dropped_count.fetch_add(1, std::memory_order_relaxed);
162 return;
163 }
164 }
165
166 TryDrain();
167}
void * end[]
内核结束
auto GetCurrentCoreId() -> size_t
Definition cpu_io.h:26
constexpr auto kMinLevel
编译期最低日志级别
auto TryDrain() -> void
将队列中所有条目输出至串口
Here is the call graph for this function:

◆ PutStr()

__always_inline auto klog::detail::PutStr ( const char *  s) -> void

通过 etl_putchar 输出字符串(空指针安全)

Definition at line 82 of file kernel_log.hpp.

82 {
83 if (!s) {
84 s = "(null)";
85 }
86 while (*s) {
87 etl_putchar(static_cast<unsigned char>(*s));
88 ++s;
89 }
90}
auto etl_putchar(int c) -> void
早期控制台字符输出
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TryDrain()

auto klog::detail::TryDrain ( ) -> void
inline

将队列中所有条目输出至串口

使用 atomic_flag 实现非阻塞 try-lock,同一时刻仅一个核心执行排空, 其他核心直接返回,等待下次调用时再排空。

Definition at line 98 of file kernel_log.hpp.

98 {
99 // 非阻塞 try-lock:若其他核心正在排空则立即返回
100 if (drain_flag.test_and_set(std::memory_order_acquire)) {
101 return;
102 }
103
104 // 若有丢弃条目则上报
105 auto dropped = dropped_count.exchange(0, std::memory_order_relaxed);
106 if (dropped > 0) {
107 char drop_buf[64];
108 auto* end = etl::format_to_n(drop_buf, sizeof(drop_buf) - 1,
109 "\033[31m[LOG] dropped {} entries\033[0m\n",
110 static_cast<uint64_t>(dropped));
111 *end = '\0';
112 PutStr(drop_buf);
113 }
114
115 // 排空循环
116 auto printer_core = cpu_io::GetCurrentCoreId();
117 LogEntry entry{};
118 while (log_queue.pop(entry)) {
119 // 格式: [id][core_id1 core_id2 LEVEL] msg
120 char hdr_buf[48];
121 auto* hdr_end =
122 etl::format_to_n(hdr_buf, sizeof(hdr_buf) - 1, "[{}][{} {} {}]",
123 entry.seq, entry.core_id, printer_core,
124 kLevelLabel[static_cast<uint8_t>(entry.level)]);
125 *hdr_end = '\0';
126 PutStr(kLevelColor[static_cast<uint8_t>(entry.level)]);
127 PutStr(hdr_buf);
128 PutStr(entry.msg);
129 PutStr(kReset);
130 PutStr("\n");
131 }
132
133 drain_flag.clear(std::memory_order_release);
134}
std::atomic_flag drain_flag
单消费者排空保护(非阻塞 try-lock)
mpmc_queue::MPMCQueue< LogEntry, 256 > log_queue
全局日志队列(64 KB 静态内存,constexpr 构造)
__always_inline auto PutStr(const char *s) -> void
通过 etl_putchar 输出字符串(空指针安全)
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ drain_flag

std::atomic_flag klog::detail::drain_flag = ATOMIC_FLAG_INIT
inline

单消费者排空保护(非阻塞 try-lock)

Definition at line 76 of file kernel_log.hpp.

◆ dropped_count

std::atomic<uint64_t> klog::detail::dropped_count {0}
inline

队列满时丢弃的条目计数

Definition at line 79 of file kernel_log.hpp.

79{0};

◆ kCyan

constexpr auto klog::detail::kCyan = "\033[36m"
inlineconstexpr

Definition at line 25 of file kernel_log.hpp.

◆ kGreen

constexpr auto klog::detail::kGreen = "\033[32m"
inlineconstexpr

Definition at line 23 of file kernel_log.hpp.

◆ kLevelColor

constexpr const char* klog::detail::kLevelColor[]
inlineconstexpr
Initial value:
= {
}
constexpr auto kYellow
constexpr auto kGreen
constexpr auto kCyan
constexpr auto kRed

日志级别对应的颜色(按 Level 枚举索引)

Definition at line 49 of file kernel_log.hpp.

49 {
50 // 调试
51 kGreen,
52 // 信息
53 kCyan,
54 // 警告
55 kYellow,
56 // 错误
57 kRed,
58};

◆ kLevelLabel

constexpr const char* klog::detail::kLevelLabel[]
inlineconstexpr
Initial value:
= {
"DEBUG",
"INFO ",
"WARN ",
"ERROR",
}

日志级别标签(定宽,对齐输出)

Definition at line 41 of file kernel_log.hpp.

41 {
42 "DEBUG",
43 "INFO ",
44 "WARN ",
45 "ERROR",
46};

◆ kMinLevel

constexpr auto klog::detail::kMinLevel
inlineconstexpr
Initial value:
=
static_cast<Level>(SIMPLEKERNEL_MIN_LOG_LEVEL)
Level
日志级别

编译期最低日志级别

Definition at line 37 of file kernel_log.hpp.

◆ kRed

constexpr auto klog::detail::kRed = "\033[31m"
inlineconstexpr

Definition at line 22 of file kernel_log.hpp.

◆ kReset

constexpr auto klog::detail::kReset = "\033[0m"
inlineconstexpr

Definition at line 21 of file kernel_log.hpp.

◆ kYellow

constexpr auto klog::detail::kYellow = "\033[33m"
inlineconstexpr

Definition at line 24 of file kernel_log.hpp.

◆ log_queue

mpmc_queue::MPMCQueue<LogEntry, 256> klog::detail::log_queue
inline

全局日志队列(64 KB 静态内存,constexpr 构造)

Definition at line 70 of file kernel_log.hpp.

◆ log_seq

std::atomic<uint64_t> klog::detail::log_seq {0}
inline

用于跨核心排序的单调递增序列计数器

Definition at line 73 of file kernel_log.hpp.

73{0};