os/log/level.rs
1//! 日志级别定义
2//!
3//! 该模块定义了内核日志系统使用的**八个日志级别**,
4//! 与 Linux 内核的 `printk` 级别相匹配。
5
6/// 日志级别枚举
7///
8/// 定义了从 Emergency (最高优先级) 到 Debug (最低优先级) 的八个优先级。
9/// 这些级别与 Linux 内核的 `KERN_*` 常量兼容。
10///
11/// # 级别语义
12///
13/// - **Emergency (紧急)**: 系统不可用
14/// - **Alert (警报)**: 必须立即采取行动
15/// - **Critical (关键)**: 关键状况
16/// - **Error (错误)**: 错误状况
17/// - **Warning (警告)**: 警告状况
18/// - **Notice (通知)**: 正常但重要的状况
19/// - **Info (信息)**: 信息性消息
20/// - **Debug (调试)**: 调试级别的消息
21#[repr(u8)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
23pub enum LogLevel {
24 /// 系统不可用
25 Emergency = 0,
26 /// 必须立即采取行动
27 Alert = 1,
28 /// 关键状况
29 Critical = 2,
30 /// 错误状况
31 Error = 3,
32 /// 警告状况
33 Warning = 4,
34 /// 正常但重要的状况
35 Notice = 5,
36 /// 信息性消息
37 Info = 6,
38 /// 调试级别的消息
39 Debug = 7,
40}
41
42impl LogLevel {
43 /// 返回日志级别的字符串表示形式
44 ///
45 /// 返回一个简短的标签,如 `[ERR]`、`[INFO]` 等。
46 pub(super) const fn as_str(&self) -> &'static str {
47 match self {
48 LogLevel::Emergency => "[EMERG]",
49 LogLevel::Alert => "[ALERT]",
50 LogLevel::Critical => "[CRIT]",
51 LogLevel::Error => "[ERR]",
52 LogLevel::Warning => "[WARNING]",
53 LogLevel::Notice => "[NOTICE]",
54 LogLevel::Info => "[INFO]",
55 LogLevel::Debug => "[DEBUG]",
56 }
57 }
58
59 /// 返回此日志级别对应的 ANSI 颜色代码
60 ///
61 /// # 颜色映射
62 ///
63 /// - Emergency/Alert/Critical: 亮红色
64 /// - Error: 红色
65 /// - Warning: 黄色
66 /// - Notice: 亮白色
67 /// - Info: 白色
68 /// - Debug: 灰色
69 pub(super) const fn color_code(&self) -> &'static str {
70 match self {
71 Self::Emergency | Self::Alert | Self::Critical => "\x1b[1;31m",
72 Self::Error => "\x1b[31m",
73 Self::Warning => "\x1b[33m",
74 Self::Notice => "\x1b[1;37m",
75 Self::Info => "\x1b[37m",
76 Self::Debug => "\x1b[90m",
77 }
78 }
79
80 /// 返回 ANSI 颜色重置代码
81 pub(super) const fn reset_color_code(&self) -> &'static str {
82 "\x1b[0m"
83 }
84
85 /// 将 u8 值转换为日志级别
86 ///
87 /// 如果该值无效,则返回默认日志级别。
88 pub fn from_u8(level: u8) -> Self {
89 match level {
90 0 => Self::Emergency,
91 1 => Self::Alert,
92 2 => Self::Critical,
93 3 => Self::Error,
94 4 => Self::Warning,
95 5 => Self::Notice,
96 6 => Self::Info,
97 7 => Self::Debug,
98
99 _ => super::config::DEFAULT_LOG_LEVEL,
100 }
101 }
102
103 /// 将日志级别转换为 u8 值
104 ///
105 /// # 返回值
106 /// 日志级别对应的数值 (0-7)
107 pub const fn to_u8(self) -> u8 {
108 self as u8
109 }
110}