C 语言添加打印信息

1、通过两个 printf 函数

#define PRINT_INFO (printf("[%s][%d]Info: ", __FILE__, __LINE__), printf)
#define PRINT_WARRING (printf("[%s][%d]Warring: ", __FILE__, __LINE__), printf)
#define PRINT_ERROR (printf("[%s][%d]Error: ", __FILE__, __LINE__), printf)
#define PRINT_DEBUG (printf("[%s][%d]Debug: ", __FILE__, __LINE__), printf)

2、打印并输出到 linux 的 syslog

/*
 * Function: LogMessage(const char *, ...)
 *
 * Purpose: Print a message to stdout or with logfacility.
 *
 * Arguments: format => the formatted error string to print out
 *            ... => format commands/fillers
 *
 * Returns: void function
 */
void LogMessage(const char *format,...)
{
    char buf[STD_BUF+1];
    va_list ap;

    va_start(ap, format);

    if(pv.daemon_flag)
    {
        vsnprintf(buf, STD_BUF, format, ap);
        syslog(LOG_DAEMON | LOG_NOTICE, "%s", buf);
    }
    else
    {
        vfprintf(stderr, format, ap);
    }
    
    va_end(ap);

    return;
}

3、通过可变参数实现

enum _PRINT_LEVEL_ {
        INFO,
        DEBUG,
        WARRING,
        ERROR
};



#define MY_PRINT(format, level, ...)\
do { \
        time_t t = time(NULL);\
        char *pTimeStr = ctime(&t);\
        const char *pLevelStr[] = {        \
	        "INFO",        \
	        "DEBUG",                \
	        "WARRING",        \
	        "ERROR"        \
        };        \
        printf("[%s][%s][%s][%d]%s: "format"", pTimeStr, __FILE__, __FUNCTION__, __LINE__, pLevelStr[level], ##__VA_ARGS__);\
} while(0);

#define PRINT_INFO(format, ...) MY_PRINT(format, INFO, ##__VA_ARGS__)
#define PRINT_DEBUG(format, ...) MY_PRINT(format, DEBUG, ##__VA_ARGS__)
#define PRINT_WARRING(format, ...) MY_PRINT(format, WARRING, ##__VA_ARGS__)
#define PRINT_ERROR(format, ...) MY_PRINT(format, ERROR, ##__VA_ARGS__)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半砖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值