Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printk loglevel usage in module programming

In the book LDD3 by Rubini, under the printk section the author says that we can give log levels/priority to our messages. But I tried with a simple module program having different log-level of printks but it showing the same order in which I have written the printk message inside the program, why it is not printing according to the priority?

I have copied the code here

  #include<linux/module.h>
  #include<linux/kernel.h>
  static __init int log_init(void)
     {
         printk(KERN_INFO"inside init 4 \n");
         printk(KERN_ERR"inside init 3\n");
         printk(KERN_CRIT"inside init 2\n");

         return 0;
     }
  static __exit void log_exit(void)
    {
        printk("inside exit\n");
    }
  module_init(log_init);
  module_exit(log_exit);

  MODULE_LICENSE("GPL");

And I got a output as follows

[ 1508.721441] inside init 4 
[ 1508.721448] inside init 3
[ 1508.721454] inside init 2
root@jitesh-desktop:~/DD/debug/print#

so how I can print it according to priority like

init 2
init 3
init 4
like image 588
Jithesh Avatar asked Dec 21 '22 19:12

Jithesh


1 Answers

You are confusing the purpose of the printk priorities. They are not meant to change the order of execution as you are wishing here.

By assigning different priorities to different kernel messages, we can filter out the desired messages that appear on the console by specifying an appropriate value of loglevel through the kernel command line. For example, in the linux kernel. there are numerous messages with KERN_DEBUG priority. These are just ordinary debugging messages. So if you enable loglevel to the maximum 7, then you'll see a flood of messages on the console!! And your vital errors and warnings will be buried under this flurry of normal debugging messages.

So when you are debugging serious issues, you can specify the loglevel to a low value so that only critcal errors and warnings are displayed on the console.

Note: Irrespective of the loglevel, all printk messages are stored in the kernel buffer. The priority decides which one of them goes to the console.

like image 197
Pavan Manjunath Avatar answered Jan 08 '23 15:01

Pavan Manjunath