-
-
Notifications
You must be signed in to change notification settings - Fork 158
实现内核日志系统 #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
实现内核日志系统 #489
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3e162cf
实现写日志和读取日志,并且能够在用户态下执行dmesg命令查看日志
84e6b96
解决冲突
8f9a7f7
通过klogctl实现dmesg
009f642
改用ConstGenericRingBuffer作内核缓冲区
8906fc5
更改缓冲区容量
dc4a394
将能够输出到控制台的日志级别改为日志级别枚举类,使用SpinLock控制KMSG,使用枚举类定义SYSLOG_ACTION,将do_sys…
54dbf07
Merge branch 'master' into log
3f1b5ae
fix warning
006d437
完善do_syslog注释
79e124f
Merge branch 'master' into log
a88ba31
将KMSG接入kinfo、kdebug等
3c95b95
Merge branch 'master' into log
a8c56b6
fix warning
a64d899
修复显示的秒数不正确,·以及无法通过CI的问题
fslongjin 43983b8
1
fslongjin 1b5c95b
Merge branch 'master' into xuzihao-log
fslongjin d87ea37
github ci
fslongjin b957df6
1
fslongjin 5d67955
1
fslongjin 079c2cf
1
fslongjin 5e068e1
1
fslongjin d6e19b0
1
fslongjin 82951e2
1
fslongjin a574e77
1
fslongjin f8d4013
1
fslongjin 7ffa10b
1
fslongjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,6 @@ pub extern crate thingbuf; | |
|
||
pub extern crate memoffset; | ||
|
||
pub extern crate ringbuffer; | ||
|
||
pub extern crate crc; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use super::log::{LogLevel, LogMessage}; | ||
|
||
use crate::libs::spinlock::SpinLock; | ||
|
||
use alloc::{borrow::ToOwned, string::ToString, vec::Vec}; | ||
|
||
use kdepends::ringbuffer::{AllocRingBuffer, RingBuffer}; | ||
|
||
use system_error::SystemError; | ||
|
||
/// 缓冲区容量 | ||
const KMSG_BUFFER_CAPACITY: usize = 1024; | ||
|
||
/// 全局环形缓冲区 | ||
pub static mut KMSG: Option<SpinLock<Kmsg>> = None; | ||
|
||
/// 初始化KMSG | ||
pub fn kmsg_init() { | ||
let kmsg = SpinLock::new(Kmsg::new()); | ||
unsafe { KMSG = Some(kmsg) }; | ||
} | ||
|
||
/// 日志 | ||
pub struct Kmsg { | ||
/// 环形缓冲区 | ||
buffer: AllocRingBuffer<LogMessage>, | ||
/// 缓冲区字节数组 | ||
data: Vec<u8>, | ||
/// 能够输出到控制台的日志级别,当console_loglevel为DEFAULT时,表示可以打印所有级别的日志消息到控制台 | ||
console_loglevel: LogLevel, | ||
/// 判断buffer在上一次转成字节数组之后是否发生变动 | ||
is_changed: bool, | ||
} | ||
|
||
impl Kmsg { | ||
pub fn new() -> Self { | ||
Kmsg { | ||
buffer: AllocRingBuffer::new(KMSG_BUFFER_CAPACITY), | ||
data: Vec::new(), | ||
console_loglevel: LogLevel::DEFAULT, | ||
is_changed: false, | ||
} | ||
} | ||
|
||
/// 添加日志消息 | ||
pub fn push(&mut self, msg: LogMessage) { | ||
self.buffer.push(msg); | ||
self.is_changed = true; | ||
} | ||
|
||
/// 读取缓冲区 | ||
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> { | ||
self.tobytes(); | ||
|
||
match self.console_loglevel { | ||
LogLevel::DEFAULT => self.read_all(buf), | ||
_ => self.read_level(buf), | ||
} | ||
} | ||
|
||
/// 读取缓冲区所有日志消息 | ||
fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> { | ||
let len = self.data.len().min(buf.len()); | ||
|
||
// 拷贝数据 | ||
let src = &self.data[0..len]; | ||
buf[0..src.len()].copy_from_slice(src); | ||
|
||
return Ok(src.len()); | ||
} | ||
|
||
/// 读取缓冲区特定level的日志消息 | ||
fn read_level(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> { | ||
let mut data_level: Vec<u8> = Vec::new(); | ||
|
||
for msg in self.buffer.iter() { | ||
if msg.level() == self.console_loglevel { | ||
data_level.append(&mut msg.to_string().as_bytes().to_owned()); | ||
} | ||
} | ||
|
||
let len = data_level.len().min(buf.len()); | ||
|
||
// 拷贝数据 | ||
let src = &data_level[0..len]; | ||
buf[0..src.len()].copy_from_slice(src); | ||
|
||
// 将控制台输出日志level改回默认,否则之后都是打印特定level的日志消息 | ||
self.console_loglevel = LogLevel::DEFAULT; | ||
|
||
return Ok(data_level.len()); | ||
} | ||
|
||
/// 读取并清空缓冲区 | ||
pub fn read_clear(&mut self, buf: &mut [u8]) -> Result<usize, SystemError> { | ||
let r = self.read_all(buf); | ||
self.clear()?; | ||
|
||
return r; | ||
} | ||
|
||
/// 清空缓冲区 | ||
pub fn clear(&mut self) -> Result<usize, SystemError> { | ||
self.buffer.clear(); | ||
self.data.clear(); | ||
|
||
return Ok(0); | ||
} | ||
|
||
/// 设置输出到控制台的日志级别 | ||
pub fn set_level(&mut self, log_level: usize) -> Result<usize, SystemError> { | ||
let log_level = log_level - 1; | ||
|
||
self.console_loglevel = match log_level { | ||
0 => LogLevel::EMERG, | ||
1 => LogLevel::ALERT, | ||
2 => LogLevel::CRIT, | ||
3 => LogLevel::ERR, | ||
4 => LogLevel::WARN, | ||
5 => LogLevel::NOTICE, | ||
6 => LogLevel::INFO, | ||
7 => LogLevel::DEBUG, | ||
8 => LogLevel::DEFAULT, | ||
_ => return Err(SystemError::EINVAL), | ||
}; | ||
|
||
return Ok(0); | ||
} | ||
|
||
/// 将环形缓冲区的日志消息转成字节数组以拷入用户buf | ||
fn tobytes(&mut self) -> usize { | ||
if self.is_changed { | ||
self.data.clear(); | ||
|
||
if self.console_loglevel == LogLevel::DEFAULT { | ||
for msg in self.buffer.iter() { | ||
self.data.append(&mut msg.to_string().as_bytes().to_owned()); | ||
} | ||
} | ||
|
||
self.is_changed = false; | ||
} | ||
|
||
return self.data.len(); | ||
} | ||
|
||
// 返回内核缓冲区所占字节数 | ||
pub fn data_size(&mut self) -> Result<usize, SystemError> { | ||
return Ok(self.tobytes()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我感觉,level应该是个“大于”的关系。就比如我们使用日志库的时候,设置为debug级别,就是打印优先级大于等于debug的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我测试了下,在Linux中,(dmesg -l notice) 和 (dmesg -l debug) 都不会打印 info 级别的日志消息(优先级:debug < info < notice)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦哦哦确实噢!