如何使用pr_cont在同一行中继续以前的日志消息?
[问题] 最近,我在Linux内核上做了一些工作,并希望使用“打印”继续在同一行中继续以前的日志消息。因此,我尝试在< linux/kernel.h>中使用pr_cont;替换功能中的printk。
代码在此处:
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/printk.h>
typedef struct {
TEST_PRINTF printf;
TEST_UDELAY udelay;
TEST_MALLOC malloc;
TEST_FREE free;
} TEST_INIT_PARAM_T;
static int __init test_init(void) {
TEST_INIT_PARAM_T my_init_param = { 0 };
/* The problem is here */
my_init_param.printf = pr_cont;
my_init_param.udelya = my_udelay;
my_init_param.malloc = my_malloc;
my_init_param.free = my_free;
my_init(&my_init_param);
....
}
GCC显示一个错误:'pr_cont'未申报(在此功能中首次使用)。但是,如果使用
my_init_param.printf = printk;
,则可以。
我还尝试使用
void test_printk(some_variables) {
printk(KERN_CONT some_variables)
}
并制作my_init_param.printf = test_printk
。 但是我不知道“某个variable”应该是什么?
另外,我真的想知道为什么方程my_init_param.printf = pr_cont
是错误的吗?
[尝试] 我意识到方程式的真正原因 my_init_param.printf = pr_cont;
不保持。就像我无法定义a = int b
一样。因此,我添加Typedef如下所示
typedef int
(*TEST_PRINTK)(
const char *fmt,
...);
TEST_PRINTK test_pr_cont;
#define test_pr_cont(fmt, ...) \
printk(KERN_CONT fmt, ##__VA_ARGS__)
,等式my_init_param.printf = test_pr_cont;
可以通过GCC。
但是,其他称为“打印”的功能无法打印任何东西。
[Question]
Recently I did some work on the Linux kernel, and want to use 'print' to continue a previous log message in the same line. So I tried to use pr_cont in <linux/kernel.h> to replace printk in the function.
The code is here:
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/printk.h>
typedef struct {
TEST_PRINTF printf;
TEST_UDELAY udelay;
TEST_MALLOC malloc;
TEST_FREE free;
} TEST_INIT_PARAM_T;
static int __init test_init(void) {
TEST_INIT_PARAM_T my_init_param = { 0 };
/* The problem is here */
my_init_param.printf = pr_cont;
my_init_param.udelya = my_udelay;
my_init_param.malloc = my_malloc;
my_init_param.free = my_free;
my_init(&my_init_param);
....
}
and GCC shows an error: 'pr_cont' undeclared (first use in this function). However, if use
my_init_param.printf = printk;
and this will be ok.
I have also tried using
void test_printk(some_variables) {
printk(KERN_CONT some_variables)
}
And make my_init_param.printf = test_printk
.
But I don't know what the 'some_variable' should be?
Also, I really want to know why the equation my_init_param.printf = pr_cont
is wrong?
[Attempt]
I have realized that the true reason why the equationmy_init_param.printf = pr_cont;
doesn't hold. Just like I can't define a = int b
. So I add typedef as below
typedef int
(*TEST_PRINTK)(
const char *fmt,
...);
TEST_PRINTK test_pr_cont;
#define test_pr_cont(fmt, ...) \
printk(KERN_CONT fmt, ##__VA_ARGS__)
Then the equationmy_init_param.printf = test_pr_cont;
can pass the GCC.
However, other functions called this 'print' cannot print anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
pr_cont
是一个宏,所以它不是函数。唯一的标识符pr_cont
不存在,只有preprocessor的呼叫pr_cont(the call)
将扩展到printk
的呼叫。pr_cont
是一个类似功能的宏。写
pr_cont
在另一个pr _*
或printk
之后,上面没有新的线,而没有新线,内核中有无尽的例子,浏览它们。 https://elixir.bootlin.com/ linux/linect/cource/aCh/csky/kernel/traps.c#l124Because
pr_cont
is a macro, it's not a function. Sole identifierpr_cont
does not exist, only the callpr_cont( the call )
will be expanded to the call toprintk
by preprocessor.pr_cont
is a function-like macro.Write
pr_cont
with your message after anotherpr_*
orprintk
above without a newline on the end. There are endless examples in kernel, browse them. https://elixir.bootlin.com/linux/latest/source/arch/csky/kernel/traps.c#L124