How to print a message in one single line in Linux kernel

杀马特。学长 韩版系。学妹 提交于 2019-12-18 21:54:43

问题


I am making a simple enque/deque program in kernel. I want to print message in kernel, and this is what I got:

[18594.595747] Enqueue 3 
[18594.595748]  queue :  
[18594.595751] 2 
[18594.595751] 1 
[18594.595752] 3 

But I want to print this without newline:

[8594.595747] Enqueue 3 
[18594.595748]  queue : 2 1 3

This is a part of my code:

   printk(KERN_ALERT "Enqueue %d \n queue :  ", a);
   rear++;
   for(i = front; i<rear; i++)
      printk(KERN_ALERT "%d ", queue_test[i]);  

In short, I want to print in kernel a message in one line. But if I use printk, it changes line automatically. How do I print a message in one line?


回答1:


To prevent a new line from being started, use KERN_CONT:

printk(KERN_ALERT "self destruction commences in ");
printk(KERN_CONT "%d", time_remaining);
printk(KERN_CONT " minutes\n");



回答2:


Debugging by printing

printk(KERN_ERR "Doing something was ");
/* <100 lines of whatever>*/
if (success)
   printk(KERN_CONT "successful\n");
else
   printk(KERN_CONT "NOT successful\n");

Logging prints should be safe: SFP vs single CPU.



来源:https://stackoverflow.com/questions/29337967/how-to-print-a-message-in-one-single-line-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!