zoukankan      html  css  js  c++  java
  • 用于调试的printf函数和自定义log函数

    1. 用宏定义调试用的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
    
    发布时,将#define DEBUG_ENABLE去掉即可


    2. 自定义的log函数模型:

    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 ;
    }
    


  • 相关阅读:
    switch 语句注意事项
    line-height 和 font-size的关系
    HTTP 缓存
    hashchange事件的认识
    面向对象的写法,见到就添,持续更新。。。
    chrome浏览器开发者工具之同步修改至本地
    history对象的一些知识点
    你不知道的函数节流,提高你的JS性能!
    玩媒体查询,就是这么简单粗暴!
    css中clip-path属性的运用
  • 原文地址:https://www.cnblogs.com/Windeal/p/4284611.html
Copyright © 2011-2022 走看看