驱动日志Level精确打印

如何精确控制驱动程序的log输出?

一、设定Level等级

enum {
        DRV_MSG_ERR           = 0x0001,
        DRV_MSG_WARN          = 0x0002,
        DRV_MSG_INFO          = 0x0004,
        DRV_MSG_DEBUG         = 0x0008,
        DRV_MSG_XXX1          = 0x0010,
        DRV_MSG_XXX2          = 0x0020,
        DRV_MSG_XXX3          = 0x0040,
        DRV_MSG_XXX4          = 0x0080,
        DRV_MSG_XXX5          = 0x0100,
        DRV_MSG_XXX6          = 0x0200,
        DRV_MSG_XXX7          = 0x0400,
        DRV_MSG_XXX8          = 0x0800,
        DRV_MSG_XXX9          = 0x1000,
        DRV_MSG_XXX10         = 0x2000,
        DRV_MSG_XXX11         = 0x4000,
};

二、Level等级输出判断

#define drv_msg_err(p)        ((p)->msg_enable & DRV_MSG_ERR)
#define drv_msg_warn(p)       ((p)->msg_enable & DRV_MSG_WARN)
#define drv_msg_info(p)       ((p)->msg_enable & DRV_MSG_INFO)
#define drv_msg_debug(p)      ((p)->msg_enable & DRV_MSG_DEBUG)
#define drv_msg_xxx1(p)       ((p)->msg_enable & DRV_MSG_XXX1)
#define drv_msg_xxx2(p)       ((p)->msg_enable & DRV_MSG_XXX2)
#define drv_msg_xxx3(p)       ((p)->msg_enable & DRV_MSG_XXX3)
#define drv_msg_xxx4(p)       ((p)->msg_enable & DRV_MSG_XXX4)
#define drv_msg_xxx5(p)       ((p)->msg_enable & DRV_MSG_XXX5)
#define drv_msg_xxx6(p)       ((p)->msg_enable & DRV_MSG_XXX6)
#define drv_msg_xxx7(p)       ((p)->msg_enable & DRV_MSG_XXX7)
#define drv_msg_xxx8(p)       ((p)->msg_enable & DRV_MSG_XXX8)
#define drv_msg_xxx9(p)       ((p)->msg_enable & DRV_MSG_XXX9)
#define drv_msg_xxx10(p)      ((p)->msg_enable & DRV_MSG_XXX10)
#define drv_msg_xxx11(p)      ((p)->msg_enable & DRV_MSG_XXX11)

三、Level等级初始化

static inline u32 netif_msg_init(int debug_value, int default_msg_enable_bits)
{
        /* use default */
        if (debug_value < 0 || debug_value >= (sizeof(u32) * 8))
                return default_msg_enable_bits;
        if (debug_value == 0)   /* no output */
                return 0;
        /* set low N bits */
        return (1 << debug_value) - 1;
}

驱动程序demo:

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/slab.h>

static int msg_level = -1;
module_param(msg_level, int, 0);
MODULE_PARM_DESC(msg_level, "Override default message level");

struct priv_info {
        u32 msg_enable;
};

enum {
        DRV_MSG_ERR           = 0x0001,
        DRV_MSG_WARN          = 0x0002,
        DRV_MSG_INFO          = 0x0004,
        DRV_MSG_DEBUG         = 0x0008,
};

#define drv_msg_err(p)        ((p)->msg_enable & DRV_MSG_ERR)
#define drv_msg_warn(p)       ((p)->msg_enable & DRV_MSG_WARN)
#define drv_msg_info(p)       ((p)->msg_enable & DRV_MSG_INFO)
#define drv_msg_debug(p)      ((p)->msg_enable & DRV_MSG_DEBUG)

struct priv_info *pinfo;
static inline u32 drv_msg_init(int debug_value, int default_msg_enable_bits)
{
        /* use default */
        if (debug_value < 0 || debug_value >= (sizeof(u32) * 8))
                return default_msg_enable_bits;
        if (debug_value == 0)   /* no output */
                return 0;
        /* set low N bits */
        return (1 << debug_value) - 1;
}

static int __init drv_init(void)
{
        pinfo = kzalloc(sizeof(struct priv_info), GFP_KERNEL);
        if (!pinfo)
                return -ENOMEM;

        pinfo->msg_enable = drv_msg_init(msg_level, DRV_MSG_ERR |
                        DRV_MSG_WARN | DRV_MSG_INFO | DRV_MSG_DEBUG);

        if(drv_msg_err(pinfo))
                printk("[ERROR] - %s:%d - MSG ERROR\n", __func__, __LINE__);

        if (drv_msg_warn(pinfo))
                printk("[WARN] - %s:%d - MSG WARN\n", __func__, __LINE__);

        if (drv_msg_info(pinfo))
                printk("[INFO] - %s:%d - MSG INFO\n", __func__, __LINE__);

        if (drv_msg_debug(pinfo))
                printk("[DEBUG] - %s:%d - MSG DEBUG\n", __func__, __LINE__);

        return 0;
}

static void __exit drv_exit(void)
{
        kfree(pinfo);
        pr_info("drv_exit\n");
}

module_init(drv_init);
module_exit(drv_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("drv msg print level demo");
posted @ 2023-01-11 11:45  闹闹爸爸  阅读(111)  评论(0编辑  收藏  举报