如何使用pr_cont在同一行中继续以前的日志消息?

发布于 2025-02-03 19:12:47 字数 1635 浏览 4 评论 0原文

[问题] 最近,我在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 equation
my_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-02-10 19:12:47

为什么[expression] my_init_param.printf = pr_cont是错误的?

因为pr_cont是一个宏,所以它不是函数。唯一的标识符pr_cont不存在,只有preprocessor的呼叫pr_cont(the call)将扩展到printk的呼叫。 pr_cont是一个类似功能的宏。

如何使用pr_cont在同一行中继续以前的日志消息?

pr_cont在另一个pr _*printk之后,上面没有新的线,而没有新线,内核中有无尽的例子,浏览它们。 https://elixir.bootlin.com/ linux/linect/cource/aCh/csky/kernel/traps.c#l124

why the [expression] my_init_param.printf = pr_cont is wrong?

Because pr_cont is a macro, it's not a function. Sole identifier pr_cont does not exist, only the call pr_cont( the call ) will be expanded to the call to printk by preprocessor. pr_cont is a function-like macro.

How to use pr_cont to continue a previous log message in the same line?

Write pr_cont with your message after another pr_* or printk 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文