找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 1404|回复: 0

修改ASUSWRT系统日志输出

[复制链接]
本帖最后由 safehorse 于 2018-8-4 12:37 编辑

编译的华硕ASUSWRT固件后台系统日志输出太多, 想清爽点就找到一篇BLOG

Printk and Console log Level


printk is used in kernel programming to print messages into the kernel logs.


The syntax of printk is




printk ("log level" "message", <arguments>);





The log levels decide the importance of the message being printed, kernel defines 8 log levels in the file printk.h




#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/





We can see each log level corresponds to a number and the lower the number higher the importance of the message.
The levels are useful in deciding what should be displayed to the user on the console and what should not be.


Every console has log level called as the the console log level and any message with a log level number lesser than the console log level gets displayed on the console, and other messages which have a log level number higher or equal to the console log level are logged in the kernel log which can be looked into using the command "dmesg".


The console loglevel can be found by looking into the file /proc/sys/kernel/printk




$ cat /proc/sys/kernel/printk
4 4 1 7





The first number in the output is the console log level, the second is the default log level, third is the minimum log level and fourth is the maximum log level.


Log level 4 corresponds to KERN_WARNING. Thus all the messages with log levels 3,2,1 and 0 will get displayed on the screen as well as logged and the messages with log level 4,5,6,7 only get logged and can be viewed using "dmesg".


The console log level can be changed by writing into the proc entry




$ echo "6" > /proc/sys/kernel/printk
$ cat /proc/sys/kernel/printk
6 4 1 7





Now the console log level is set to 6, which is KERN_INFO.


We can test logging by using the following module


hello.c:




#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/init.h>


static int hello_init(void)
{
printk(KERN_WARNING "Hello, world \n ");
return 0;
}


static void hello_exit(void)
{
printk(KERN_INFO "Goodbye, world \n");
}


module_init(hello_init);
module_exit(hello_exit);





The printk called in the init function uses KERN_WARNING which is log level and lesser than 6 which is the console log level and hence should be seen on the screen.


The printk used in the exit function is KERN_INFO which is log level 6,same as the console log level, and hence should not be visible on the screen.


Note: We can test the operation of the code only by logging into a text mode as none of the messages are displayed on a terminal of GUI.


Makefile:




ifneq ($(KERNELRELEASE),)
   obj-m := hello.o
else


KERNELDIR ?= /lib/modules/$(shell uname -r)/build


PWD := $(shell pwd)


default:
make -C $(KERNELDIR) M=$(PWD) modules  
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
endif





Compile and insert




$ make
$ insmod hello.ko
[5377.966743] Hello world





We can see the hello world being printed on the screen.




$ rmmmod hello
$ dmesg| tail -2
[5424.190552] Good bye world





The good bye world message gets logged but is not printed on the screen but can be see in the logs.


Thus using printk and the console log levels we can control the kernel messages visible to the user.
我的恩山、我的无线 The best wifi forum is right here.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-28 07:05

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表