printk的用法

内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6>Hello, world!\n");。内核中共提供了八种不同的日志级别,在 linux/kernel.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 */
#define KERN_INFO     "<6>"   /* informational */
#define KERN_DEBUG    "<7>"   /* debug-level messages */

所以 printk() 可以这样用:printk(KERN_INFO "Hello, world!\n");。

未指定日志级别的 printk() 采用的默认级别是 DEFAULT_MESSAGE_LOGLEVEL,这个宏在 kernel/printk.c 中被定义为整数 4,即对应KERN_WARNING。

在 /proc/sys/kernel/printk 会显示4个数值(可由 echo 修改),分别表示当前控制台日志级别、未明确指定日志级别的默认消息日志级别、最小(最高)允许设置的控制台日志级别、引导时默认的日志级别。当 printk() 中的消息日志级别小于当前控制台日志级别时,printk 的信息(要有\n符)就会在控制台上显示。但无论当前控制台日志级别是何值,通过 /proc/kmsg (或使用dmesg)总能查看。另外如果配置好并运行了 syslogd 或 klogd,没有在控制台上显示的 printk 的信息也会追加到 /var/log/messages.log 中。

char myname[] = "chinacodec\n";
printk(KERN_INFO "Hello, world %s!\n", myname);


printk 

4.2.1. printk函数

        We used the printk function in earlier chapters with the simplifying assumption that it works like printf. Now it's time to introduce some of the differences. 

        我们在前面章节中简单地把printk当作printf函数来使用。现在是时候来介绍它的一些不同之处了。 

        One of the differences is that printk lets you classify messages according to their severity by associating different loglevels, or priorities, with the messages. You usually indicate the loglevel with a macro. For example, KERN_INFO, which we saw prepended to some of the earlier print statements, is one of the possible loglevels of the message. The loglevel macro expands to a string, which is concatenated to the message text at compile time; that's why there is no comma between the priority and the format string in the following examples. Here are two examples of printk commands, a debug message and a critical message: 

        其中一个不同点是,printk允许你按照相关的记录级或优先级将消息严格分类。通常你需要一个宏来指定记录等级。例如,KERN_INFO,我们在早先的的例子中看到过这个宏,它就是消息记录等级的一种。记录等级宏的作用是扩展为一个字符串,这个字符串会在编译期间与相应的消息文本相连接;这就解释了下面例子中为什么在优先级和格式化字符串之间没有逗号了。下面是两个printk函数的例子,一个是调试消息,一个是临界消息: 

printk(KERN_DEBUG "Here I am: %s:%i\n", _ _FILE_ _, _ _LINE_ _);



printk(KERN_CRIT "I'm trashed; giving up on %p\n", ptr);



        There are eight possible loglevel strings, defined in the header ; we list them in order of decreasing severity: 
        
        在头文件<linux/kernel.h>中共定义了八个可用的记录级;我们下面按其严重性倒序列出: 
KERN_EMERG
Used for emergency messages, usually those that precede a crash. 
用于突发性事件的消息,通常在系统崩溃之前报告此类消息。 
KERN_ALERT
A situation requiring immediate action. 
在需要立即操作的情况下使用此消息。 
KERN_CRIT
Critical conditions, often related to serious hardware or software failures. 
用于临界条件下,通常遇到严重的硬软件错误时使用此消息。 
KERN_ERR
Used to report error conditions; device drivers often use KERN_ERR to report hardware difficulties. 
用于报告错误条件;设备驱动经常使用KERN_ERR报告硬件难题。 
KERN_WARNING
Warnings about problematic situations that do not, in themselves, create serious problems with the system. 
是关于问题状况的警告,一般这些状况不会引起系统的严重问题。 
KERN_NOTICE 
Situations that are normal, but still worthy of note. A number of security-related conditions are reported at this level. 
该级别较为普通,但仍然值得注意。许多与安全性相关的情况会在这个级别被报告。 
KERN_INFO
Informational messages. Many drivers print information about the hardware they find at startup time at this level. 
信息消息。许多驱动程序在启动时刻用它来输出获得的硬件信息。

KERN_DEBUG 

Used for debugging messages. 

用于输出调试信息 

        Each string (in the macro expansion) represents an integer in angle brackets. Integers range from 0 to 7, with smaller values representing higher priorities. 

        每一个字符串(由宏扩展而成)表示了尖括号内的一个整数。数值范围从0到7,数值越小,优先级越高。 

        A printk statement with no specified priority defaults to DEFAULT_MESSAGE_LOGLEVEL, specified in kernel/printk.c as an integer. In the 2.6.10 kernel, DEFAULT_MESSAGE_LOGLEVEL is KERN_WARNING, but that has been known to change in the past. 

        一个printk的缺省优先级是DEFAULT_MESSAGE_LOGLEVEL,它是一个在kernel/printk.c文件中指定的整数。在 2.6.10内核中,DEFAULT_MESSAGE_LOGLEVEL相当于KERN_WARNING,但据说在早期版本中这是两个不同的优先级。 

        Based on the loglevel, the kernel may print the message to the current console, be it a text-mode terminal, a serial port, or a parallel printer. If the priority is less than the integer variable console_loglevel, the message is delivered to the console one line at a time (nothing is sent unless a trailing newline is provided). If both klogd and syslogd are running on the system, kernel messages are appended to /var/log/messages (or otherwise treated depending on your syslogd configuration), independent of console_loglevel. If klogd is not running, the message won't reach user space unless you read /proc/kmsg (which is often most easily done with the dmesg command). When using klogd, you should remember that it doesn't save consecutive identical lines; it only saves the first such line and, at a later time, the number of repetitions it received. 

        基于这些记录级,内核可以把消息输出到当前的控制台,也可以是一个文本模式的终端,一个串口,或是一个并口打印机。如果优先级小于整形变量 console_loglevel,那么一次将会只发送一行消息到控制台中(除非遇到一个换行符,否则将什么都不会发送)。如果系统中运行了klogd和syslogd进程,那么内核消息就会被完整地添加到/var/log/messages文件中(或者根据你的syslogd进程的配置状况进行发送)而忽略console_loglevel,如果klogd没有运行,那么消息将不会到达用户空间,除非你对/proc/kmsg文件读取(实际上这项工作已被较早的dmesg命令完成)。当使用klogd时,你应该记住它不会保留重复的消息行;对于它接收到的重复消息,它只会保留第一条。 

        The variable console_loglevel is initialized to DEFAULT_CONSOLE_LOGLEVEL and can be modified through the sys_syslog system call. One way to change it is by specifying the -c switch when invoking klogd, as specified in the klogd manpage. Note that to change the current value, you must first kill klogd and then restart it with the -c option. Alternatively, you can write a program to change the console loglevel. You'll find a version of such a program in misc-progs/setlevel.c in the source files provided on O'Reilly's FTP site. The new level is specified as an integer value between 1 and 8, inclusive. If it is set to 1, only messages of level 0 (KERN_EMERG) reach the console; if it is set to 8, all messages, including debugging ones, are displayed. 

        被初始化为DEFAULT_CONSOLE_LOGLEVEL的console_loglevel变量可以通过sys_syslog系统调用修改。改变它内容的一个方法就是在调用klogd时使用-c选项,具体参考klogd的man帮助。请注意,为了改变当前的数值,你必须首先结束klogd进程,并且用-c选项重新启动它。另一种方法是,你可以写一个应用程序来改变控制台的记录等级。你可以在O'Reilly的FTP站点提供的源代码文件中找到/misc-progs/setlevel.c文件,其中就有一个这样的程序。新记录级为一个1到8的整数。如果设置为1,那么只有优先级为0(KERN_EMERG)的消息才可以到达控制台;如果等级设置为8,那么包括调试信息在内的所有消息都会被显示。 

        It is also possible to read and modify the console loglevel using the text file /proc/sys/kernel/printk. The file hosts four integer values: the current loglevel, the default level for messages that lack an explicit loglevel, the minimum allowed loglevel, and the boot-time default loglevel. Writing a single value to this file changes the current loglevel to that value; thus, for example, you can cause all kernel messages to appear at the console by simply entering: 

        也可以通过文本文件/proc/sys/kernel/printk来获取和更改控制台的记录等级。这个文件中存储着四个整型数值:当前记录级,缺省记录级,最低记录级和启动时刻的缺省记录级。可以向该文件写入一个单一数值来改变当前记录级;例如,如果你可以想所有的内核消息都可以在控制台中显示,可以使用以下命令: 

# echo 8 > /proc/sys/kernel/printk 

        It should now be apparent why the hello.c sample had the KERN_ALERT; markers; they are there to make sure that the messages appear on the console. 

        现在你应该明白在hello.c示例代码中为什么会有KERN_ALERT;标识了吧;这样做可以保证消息顺利地输出到控制台中 。

printk的用法的更多相关文章

  1. printk和printf的区别

    内核使用printk()打印! 应用层使用printf()打印! &&& 大部分常用的C库函数在Linux内核中都已经得到了实现.在所有没有实现的函数中,最著名的就数print ...

  2. 使用 /proc 文件系统来访问 linux操作系统 内核的内容 && 虚拟文件系统vfs及proc详解

    http://blog.163.com/he_junwei/blog/static/19793764620152743325659/ http://www.01yun.com/other/201304 ...

  3. (转)使用 /proc 文件系统来访问 Linux 内核的内容

    转载网址:http://www.ibm.com/developerworks/cn/linux/l-proc.html 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口/proc 文件系统是 ...

  4. linux设备驱动程序第四部分:从如何定位oops对代码的调试方法,驱动线

    在一个我们谈到了如何编写一个简单的字符设备驱动程序,我们不是神,编写肯定会失败的代码,在这个过程中,我们需要继续写代码调试.在普通c应用.我们经常使用printf输出信息.或者使用gdb要调试程序,然 ...

  5. (笔记)Linux下的ioctl()函数详解

    我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...

  6. linux ioctl()函数

    我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...

  7. Android的logger机制分析

    分析安卓的Logger机制 一.概述 Logger机制是在Android系统中提供的一个轻量级的日志系统,这个日志系统是以驱动程序的形式在内核空间实现的,在用户空间分别提供了Java接口和C/C++接 ...

  8. Linux下的ioctl()函数详解

    我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...

  9. Linux驱动开发——pr_fmt的用法

    作者:彭东林 邮箱:pengdonglin137@163.com 在阅读kernel代码的时候,总是看到有很多驱动都在第一行定义pr_fmt,闲来没事,分析了一下, 发现,确实挺方便的.下面记录分享一 ...

随机推荐

  1. Python【第四课】 模块 and 包

    本篇内容 模块基础和导入(import,from...import...) 包常识和导入(import,from...import...) 函数式第一类对象 1.模块基础和导入 1.1 模块认识 什么 ...

  2. Linux 基本概念和操作

    我们在使用Linux时,不是直接和系统打交道,而是通过shell的中间程序.在图形界面下为了实现窗口的输入和输出,linux系统为我们提供了终端模拟器Terminal,常见的终端模拟器有 gnome- ...

  3. Android自定义模糊匹配搜索控件(二)

    在项目中遇到一个需要通过某个字的值筛选匹配带出其他信息的需求,在这里将实现思路整理出来. 源码地址:https://github.com/whieenz/SearchSelect 先看效果图 上图中的 ...

  4. Java并发编程之并发工具类

    CountDownLatch CountDownLatch可以用于一个或多个线程等待其他线程完成操作. 示例代码 private static CountDownLatch c = new Count ...

  5. Mobx使用详解

    Mobx是一个功能强大,上手非常容易的状态管理工具.就连redux的作者也曾经向大家推荐过它,在不少情况下你的确可以使用Mobx来替代掉redux. 本教程旨在介绍其用法及概念,并重点介绍其与Reac ...

  6. hive升级遇到的几个小问题

    1.hiveserver2正常启动,但是beeline连不上服务. 我的服务是开在本机的,但是用主机名和127好像都不能访问服务了 jdbc:hive2://172.19.1.11:10000> ...

  7. ubuntu 英文系统下安装中文输入法

    环境:ubuntu15.10 64位 英文版 软件:fcitx输入法框架,及多种拼音输入法 linux的英文系统会比中文少很多麻烦,特别是在命令行输入路径的时候,如果路径是中文将是一件很头疼的问题.但 ...

  8. Oracle中rownum原理介绍

    rownum原理如下:1.执行查询操作2.将第一行的row num置为13.将得到的行的row num与条件相比较,如果不匹配,则抛弃行,如果匹配,则返回行4.oracle获取下一行,然后将rownu ...

  9. JAVA不可变类(immutable)机制与String的不可变性

    一.不可变类简介 不可变类:所谓的不可变类是指这个类的实例一旦创建完成后,就不能改变其成员变量值.如JDK内部自带的很多不可变类:Interger.Long和String等. 可变类:相对于不可变类, ...

  10. 剑指架构师系列-持续集成之Maven实现项目的编译、发布和部署

    Maven组织项目进行编译.部署 Maven项目基本的结构说明如下: mazhi  // 控制所有荐的编译.部署.发布 mazhi-app-parent  // 项目的父项目,有一些公共的设置可以被子 ...