• 新写的一个日志接口


    主要是为了记录宏中使用可变参数。如果没有可变参数,也不会有逗号的影响。 主要是##grgs的使用,至于具体gnu gcc版本对可变参数的支持程度,尚未进行充分测试。

    头文件

    #pragma once
    
    #include <string>
    
    typedef void (*LogCallBack)(unsigned short logType, const char* log);
    
    void set_log_callback(LogCallBack callback);
    
    void set_log_type_of_info(unsigned short logType);
    void set_log_type_of_trace(unsigned short logType);
    void set_log_type_of_debug(unsigned short logType);
    void set_log_type_of_error(unsigned short logType);
    
    void log_write(const char* file, const char* function, int line, unsigned short logType, const char* fmt, ...);
    void log_info(const char* file, const char* function, int line, const char* fmt, ...);
    void log_trace(const char* file, const char* function, int line, const char* fmt, ...);
    void log_debug(const char* file, const char* function, int line, const char* fmt, ...);
    void log_error(const char* file, const char* function, int line, const char* fmt, ...);
    
    #define log_WRITE(logType, format, args...) 
        log_write(__FILE__,__FUNCTION__, __LINE__, logType, format, ##args)
    
    #define log_INFO(format, args...) 
        log_info(__FILE__,__FUNCTION__, __LINE__, format, ##args)
    
    #define log_TRACE(format, args...) 
        log_trace(__FILE__,__FUNCTION__, __LINE__, format, ##args)
    
    #define log_DEBUG(format, args...) 
        log_debug(__FILE__,__FUNCTION__, __LINE__, format, ##args)
    
    #define log_ERROR(format, args...) 
        log_error(__FILE__,__FUNCTION__, __LINE__, format, ##args)

    源文件:

    #include "Log.h"
    
    #include <stdarg.h>
    #include <sstream>
    #include <stdio.h>
    #include "string.h"
    
    using namespace std;
    
    static LogCallBack callback_function = NULL;
    
    static unsigned short log_info     = 0;
    static unsigned short log_trace    = 0;
    static unsigned short log_debug    = 0;
    static unsigned short log_error    = 0;
    
    static void write_log(unsigned short logType, const char* file,
                    const char* function, int line, const char* fmt, va_list args);
    
    void log_write(const char* file, const char* function, int line, unsigned short logType, const char* fmt, ...)
    {    
        if (NULL == callback_function)
        {
            return;
        }
        
        va_list arguments;
        va_start(arguments, fmt);
        write_log(logType, file, function, line, fmt, arguments);    
        va_end(arguments);
    }
    
    void log_info(const char* file, const char* function, int line, const char* fmt, ...)
    {    
        if (NULL == callback_function)
        {
            return;
        }
        
        va_list arguments;
        va_start(arguments, fmt);
        write_log(log_info, file, function, line, fmt, arguments);    
        va_end(arguments);
    }
    
    void log_trace(const char* file, const char* function, int line, const char* fmt, ...)
    {    
        if (NULL == callback_function)
        {
            return;
        }
        
        va_list arguments;
        va_start(arguments, fmt);
        write_log(log_trace, file, function, line, fmt, arguments);    
        va_end(arguments);
    }
    
    void log_debug(const char* file, const char* function, int line, const char* fmt, ...)
    {    
        if (NULL == callback_function)
        {
            return;
        }
        
        va_list arguments;
        va_start(arguments, fmt);
        write_log(log_debug, file, function, line, fmt, arguments);    
        va_end(arguments);
    }
    
    void log_error(const char* file, const char* function, int line, const char* fmt, ...)
    {    
        if (NULL == callback_function)
        {
            return;
        }
        
        va_list arguments;
        va_start(arguments, fmt);
        write_log(log_error, file, function, line, fmt, arguments);    
        va_end(arguments);
    }
    
    void set_log_callback(LogCallBack callback)
    {
        callback_function = callback;
    }
    
    void set_log_type_of_info(unsigned short logType)
    {
        log_info = logType;
    }
    
    void set_log_type_of_debug(unsigned short logType)
    {
        log_debug = logType;
    }
    
    void set_log_type_of_trace(unsigned short logType)
    {
        log_trace = logType;
    }
    
    void set_log_type_of_error(unsigned short logType)
    {
        log_error = logType;
    }
    
    static void write_log(unsigned short logType, const char* file,
                    const char* function, int line, const char* fmt, va_list args)
    {
        char chArr[2048];
        memset(chArr, 0 , sizeof(chArr));
        
        vsnprintf(chArr, 2047, fmt, args); 
        
        stringstream oss;
        oss<<file<<"::"<<function<<"::"<<line<<"::"<<chArr<<"
    "<<endl;
            
        callback_function(logType, oss.str().c_str()); 
    }
  • 相关阅读:
    教你彻底弄懂JS中this的指向
    js-原型,原型链
    Firefox SyntaxError: invalid regexp group ChunkLoadError: Loading chunk task-show-task-show-module failed.
    什么是标签语义化?标签语义化有什么意义?
    什么是事件委托?jquery和js怎么去实现?
    express框架
    es6
    node搭建服务器
    node内容
    ajax面试题
  • 原文地址:https://www.cnblogs.com/Rong-/p/7922312.html
Copyright © 2020-2023  润新知