日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

用于调试的printf函数和自定义log函数

發(fā)布時間:2023/12/13 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 用于调试的printf函数和自定义log函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 用宏定義調(diào)試用的DPRINT

#define DEBUG_ENABLE
#ifdef DEBUG_ENABLE
#define DPRINT(fmt, args...) fprintf(stderr, "[DPRINT...][%s %d] "fmt"
", __FILE__, __LINE__, ##args); 
#else
#define DPRINT(fmt, ...)  
#endif

發(fā)布時,將#define DEBUG_ENABLE去掉即可

2. 自定義的log函數(shù)模型:

char LogLastMsg[128]; // all info of the last log, all the info to log last time

int Log2Stderr = LOG_ERR; //control Loging to stderr

/**
 * @Synopsis  a log func demo 
 *      demo for how  user defined module log info
 *
 * @Param priority: level of log, LOG_ERR, LOG_DEBUG etc.
 * @Param errno:    errno
 * @Param fmt:  format of message to log
 * @Param ...:  args follow by fmt
 */
void mylog(int priority, int errno, char* fmt, ...)
{
    DPRINT("mylog Begin...");
    char priVc[][8] = {"Emerg", "Alert", "Crit", "Error", "Warning", "Notice", "Info", "Debug"};

    char* priPt = priority < 0 || priority >= sizeof(priVc)/sizeof(priVc[0]) ?
        "Unknow priority!" : priVc[priority];

    char *errMsg = errno <= 0 ? NULL : (const char*)strerror(errno);

    {
        va_list argPt;
        unsigned Ln;

        va_start(argPt, fmt);  //now argPt is point to mylog's param:...
        Ln = snprintf(LogLastMsg, sizeof(LogLastMsg), "[mylog...][%s]: ", priPt);
        Ln += vsnprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, fmt, argPt);
        if (NULL != errMsg)
        {
            Ln += snprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, "%d:%s", errno, errMsg);
        }
        va_end(argPt);
    }
    //choose the log which should be show on stderr
    if (priority < LOG_ERR || priority <= Log2Stderr)
    {
        fprintf(stderr, "%s
", LogLastMsg);
    }
    DPRINT("log to stderr");

    //always to syslog
    syslog(priority, "%s", LogLastMsg);

    if (priority <= LOG_ERR)
    {
        exit(-1);
    }
    return ;
}

總結(jié)

以上是生活随笔為你收集整理的用于调试的printf函数和自定义log函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。