std cpp、std c模块通过函数指针调用qt的功能

本小妞迷上赌 提交于 2020-01-27 02:30:33

需求

win、vs环境下开发cpp程序,多个库,有std cpp、qt、std c…
因为qt的ts、qm、QCoreApplication::translate、lupdate…这一套东西方便了多语言的实现,所以希望在标准c、cpp库中也能用到qt这一套东西,但又不希望在基础库中引入qt库。

实现

在一个std cpp库中定义一个函数指针pFunc,
在qt cpp log库中定义此函数Func,并在初始化时赋值给pFunc。

类似这种需求都可以考虑采用函数指针来实现,相当于汇编级别的跳转指令。

std cpp库中定义一个函数指针pFunc:

namespace StdErrorLog{
enum EnumLogLevel{
    kELL_TRACE = 0,         /**< 0,跟踪 */
    kELL_DEBUG,             /**< 1,调试 */
    kELL_INFOR,             /**< 2,信息 */
    kELL_WARNG,             /**< 3,警告 */
    kELL_ERROR,             /**< 4,错误 */
    kELL_FATAL,             /**< 5,致命问题 */
    kELL_OFF,               /**< 6,关闭所有错误输出 */
    kELL_ALL = kELL_TRACE,  /**< 0,打开所有错误输出 */
};
}

typedef void(*StdWriteErrorFuncType)(const int iErrorCode, const char *pErrorCode,
    const char *pFile, const int iLine,
    const char *s1, const char *s2, const char *s3,
    const char *s4, const char *s5, const char *s6);
class TIME_LOG_EXPORT StdWriteErrorClass
{
private:
    StdWriteErrorClass(){}
    ~StdWriteErrorClass(){}
private:
    static StdWriteErrorFuncType StdWriteErrorFunc;
public:
    static inline void SetStdWriteErrorFunc(StdWriteErrorFuncType f){
        StdWriteErrorFunc = f;
    }
    static inline StdWriteErrorFuncType GetStdWriteErrorFunc(){
        return StdWriteErrorFunc;
    }
public:
    static inline void StdWriteError(const int iErrorCode, const char *pErrorCode,
        const char *pFile, const int iLine,
        const char *s1 = NULL, const char *s2 = NULL, const char *s3 = NULL,
        const char *s4 = NULL, const char *s5 = NULL, const char *s6 = NULL)
    {
        if (StdWriteErrorFunc == NULL)
        {
            return;
        }
        if (pErrorCode == NULL)
        {
            return;
        }
        StdWriteErrorFunc(iErrorCode, pErrorCode, pFile, iLine, s1, s2, s3, s4, s5, s6);//通过函数指针输出日志
    }
};

qt cpp log库中定义此函数Func:

void StdWriteError(const int iErrorCode, const char *pErrorCode, const char *pFile, const int iLine,
    const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6);


void StdWriteError(const int iErrorCode, const char *pErrorCode,
    const char *pFile, const int iLine,
    const char *s1, const char *s2, const char *s3,
    const char *s4, const char *s5, const char *s6)
{
    if (pErrorCode == NULL)
    {
        return;
    }
    int iErrorStrCode = gXpe.GetErrorCode(pErrorCode);
    if (iErrorStrCode == kEEC_ERROR)
    {
        //未找到错误码的字符串直接输出
        switch (iErrorCode)
        {
        case kELL_TRACE:
        {
            TRACE_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        case kELL_DEBUG:
        {
            DEBUG_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        case kELL_INFOR:
        {
            INFOR_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        case kELL_WARNG:
        {
            WARNG_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        case kELL_ERROR:
        {
            ERROR_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        case kELL_FATAL:
        {
            FATAL_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        default:
        {
            ERROR_LOGS_FL(pErrorCode, pFile, iLine);
            break;
        }
        }
    }
    else
    {
        switch (iErrorCode)
        {
        case kELL_TRACE:
        {
            if (s1 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                TRACE_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        case kELL_DEBUG:
        {
            if (s1 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                DEBUG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        case kELL_INFOR:
        {
            if (s1 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                INFOR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        case kELL_WARNG:
        {
            if (s1 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                WARNG_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        case kELL_ERROR:
        {
            if (s1 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        case kELL_FATAL:
        {
            if (s1 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                FATAL_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        default:
        {
            if (s1 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode), pFile, iLine);
            }
            else if (s2 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1), pFile, iLine);
            }
            else if (s3 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2), pFile, iLine);
            }
            else if (s4 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3), pFile, iLine);
            }
            else if (s5 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4), pFile, iLine);
            }
            else if (s6 == NULL)
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5), pFile, iLine);
            }
            else
            {
                ERROR_LOG_FL(iErrorStrCode, xLogTr(pErrorCode).arg(s1).arg(s2).arg(s3).arg(s4).arg(s5).arg(s6), pFile, iLine);
            }
            break;
        }
        }
    }
}

函数指针赋值:

    StdWriteErrorClass::SetStdWriteErrorFunc(StdWriteError);

日志宏定义:


//第一组日志的s只是不支持翻译的字符串,可以由输出方提前拼好。实现就是s在错误编码map中找不到就直接输出。
#define STD_TRACE_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,s,__FILE__,__LINE__)
#define STD_DEBUG_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,s,__FILE__,__LINE__)
#define STD_INFOR_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,s,__FILE__,__LINE__)
#define STD_WARNG_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,s,__FILE__,__LINE__)
#define STD_ERROR_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,s,__FILE__,__LINE__)
#define STD_FATAL_LOGS(s) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,s,__FILE__,__LINE__)

//无参数的错误码,输出时查表获得多语言描述。例如:kEEC_NO_MEMORY
#define STD_TRACE_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)
#define STD_DEBUG_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)
#define STD_INFOR_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)
#define STD_WARNG_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)
#define STD_ERROR_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)
#define STD_FATAL_LOG0(e) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__)

//有一个参数的错误码,输出时查表获得多语言描述,插入参数输出。例如:kEEC_FILE_NOT_EXIST_V1
#define STD_TRACE_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)
#define STD_DEBUG_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)
#define STD_INFOR_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)
#define STD_WARNG_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)
#define STD_ERROR_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)
#define STD_FATAL_LOG1(e,s1) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1)

//有两个参数的错误码,输出时查表获得多语言描述,插入参数输出。例如:kEEC_FILE_COPY_V2
#define STD_TRACE_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)
#define STD_DEBUG_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)
#define STD_INFOR_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)
#define STD_WARNG_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)
#define STD_ERROR_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)
#define STD_FATAL_LOG2(e,s1,s2) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2)

//有3个参数的错误码
#define STD_TRACE_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)
#define STD_DEBUG_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)
#define STD_INFOR_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)
#define STD_WARNG_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)
#define STD_ERROR_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)
#define STD_FATAL_LOG3(e,s1,s2,s3) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3)

//有4个参数的错误码
#define STD_TRACE_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)
#define STD_DEBUG_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)
#define STD_INFOR_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)
#define STD_WARNG_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)
#define STD_ERROR_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)
#define STD_FATAL_LOG4(e,s1,s2,s3,s4) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4)

//有5个参数的错误码
#define STD_TRACE_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)
#define STD_DEBUG_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)
#define STD_INFOR_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)
#define STD_WARNG_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)
#define STD_ERROR_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)
#define STD_FATAL_LOG5(e,s1,s2,s3,s4,s5) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5)

//有6个参数的错误码
#define STD_TRACE_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_TRACE,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)
#define STD_DEBUG_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_DEBUG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)
#define STD_INFOR_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_INFOR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)
#define STD_WARNG_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_WARNG,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)
#define STD_ERROR_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_ERROR,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)
#define STD_FATAL_LOG6(e,s1,s2,s3,s4,s5,s6) StdWriteErrorClass::StdWriteError(StdErrorLog::kELL_FATAL,reinterpret_cast<const char*>(#e),__FILE__,__LINE__,s1,s2,s3,s4,s5,s6)

日志使用示例:

STD_ERROR_LOG1(kEEC_PUGI_FILE_NOT_FOUND_V1, CodePageInfor::LocalToUtf8(file_temp).c_str());

其它说明:
gXpe是全局变量,存储一些配置信息。
GetErrorCode函数负责将字符串翻译为对应的错误编码。
xLogTr函数负责在gXpe中查询字符串对应的描述,再用QCoreApplication::translate函数将描述翻译为当前语言。

牵涉到log4cplus的宏修改:
只是将__FILE__,__LINE__改为参数传入

#define LOG4CPLUS_ERROR_FL(logger, logEvent,f,l)                               \
    LOG4CPLUS_MACRO_BODY_FL (logger, logEvent, ERROR_LOG_LEVEL,f,l)
#define LOG4CPLUS_MACRO_BODY_FL(logger, logEvent, logLevel,ff,ll)       \
    LOG4CPLUS_SUPPRESS_DOWHILE_WARNING()                                \
    do {                                                                \
        log4cplus::Logger const & _l                                    \
            = log4cplus::detail::macros_get_logger (logger);            \
        if (LOG4CPLUS_MACRO_LOGLEVEL_PRED (                             \
                _l.isEnabledFor (log4cplus::logLevel), logLevel)) {     \
            LOG4CPLUS_MACRO_INSTANTIATE_OSTRINGSTREAM (_log4cplus_buf); \
            _log4cplus_buf << logEvent;                                 \
            log4cplus::detail::macro_forced_log (_l,                    \
                log4cplus::logLevel, _log4cplus_buf.str(),              \
                ff, ll, LOG4CPLUS_MACRO_FUNCTION ());                   \
        }                                                               \
    } while (0)                                                         \
    LOG4CPLUS_RESTORE_DOWHILE_WARNING()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!