Linux lernel log level test
#define DEBUG | |
#define pr_fmt(fmt) "printk_device: " fmt | |
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/init.h> | |
#include <linux/miscdevice.h> | |
#include <linux/fs.h> | |
#include <linux/printk.h> | |
#define print(level) \ | |
printk(level "It is '" #level "' message generated from `printk`.") | |
static ssize_t printk_read(struct file *file, char __user *buf, | |
size_t count, loff_t *off) { | |
pr_devel("This message is output because of macro 'DEBUG' defination.\n"); | |
pr_debug("This message is dynamic output.\n"); | |
pr_info_once("This message should only appear once.\n"); | |
print(KERN_EMERG); | |
print(KERN_ALERT); | |
print(KERN_CRIT); | |
print(KERN_ERR); | |
print(KERN_WARNING); | |
print(KERN_NOTICE); | |
print(KERN_INFO); | |
print(KERN_DEBUG); | |
printk(KERN_CONT " It is the first 'KERN_CONT' message generated" \ | |
" from printk, shouldn't start at newline.\n"); | |
printk(KERN_CONT "It is the second 'KERN_CONT' message generated" \ | |
" from printk, it should be at newline and appear immediately" \ | |
" because of the end character '\\n'.\n"); | |
printk(KERN_CONT "It is the third 'KERN_CONT' message generated" \ | |
" from printk, it starts at newline because of the '\\n'." \ | |
" in the end of the last 'KERN_CONT' line, but it appears" \ | |
" in the next action because of no '\\n' suffix."); | |
return 0; | |
} | |
static ssize_t printk_write(struct file *file, const char __user *buf, | |
size_t count, loff_t *off) { | |
WARN(true, "This message is generated from 'WARN'.\n"); | |
WARN_ONCE(true, "This message is generated from 'WARN_ONCE', and should" | |
" appear once.\n"); | |
return count; | |
} | |
static struct file_operations printk_fops = { | |
.read = printk_read, | |
.write = printk_write, | |
}; | |
static struct miscdevice printk_device = { | |
.minor = MISC_DYNAMIC_MINOR, | |
.name = "printk", | |
.fops = &printk_fops, | |
}; | |
static int __init printk_init(void) { | |
int retval = 0; | |
retval = misc_register(&printk_device); | |
if (retval != 0) | |
pr_warn("Couldn't register device!\n"); | |
// BUILD_BUG_ON_MSG(true, "this line should be commented."); | |
return retval; | |
} | |
static void __exit printk_exit(void) { | |
misc_deregister(&printk_device); | |
} | |
module_init(printk_init); | |
module_exit(printk_exit); | |
MODULE_LICENSE("GPL"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment