zoukankan      html  css  js  c++  java
  • 新写的一个日志接口

    主要是为了记录宏中使用可变参数。如果没有可变参数,也不会有逗号的影响。 主要是##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()); 
    }
  • 相关阅读:
    JAVA获取随机数
    web路径问题
    java.lang.IllegalStateException: Failed to load ApplicationContext,这个异常的解决方案
    使用表达式配置切入点
    Spring的aop操作
    AOP操作术语
    AOP原理
    AOP概念
    spring知识点
    Spring的ioc操作 与 IOC底层原理
  • 原文地址:https://www.cnblogs.com/Rong-/p/7922312.html
Copyright © 2011-2022 走看看