新写的c++日志库:log4K

网是开源的c/c++日志库也不少,但用起来总觉得不方便,于是动手写了一个C++日志框架Log4K。

测试代码:

#include "log4k.h"

#pragma comment(lib, "log4k.lib")

static int g_Cnt = 0;
void LogTestThread1(LPVOID lpPara)
{
    DEBUG_FUNCTION();

    LOGFMTT(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTD(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTI(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTW(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTE(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);

    for (int i = 0; i < 10;)
    {
        LOGFMTT(L"[%d] %s", i++, __FUNCTIONW__);
        //Sleep(10);
    }
}

void LogTestThread2(LPVOID lpPara)
{
    DEBUG_FUNCTION();

    LOGFMTT_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTD(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTI_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTW(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTE_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);

    for (int i = 0; i < 10;)
    {
        LOGFMTI(L"[%d] %s", i++, __FUNCTIONW__);
        //Sleep(10);
    }
}

int main()
{
    DEBUG_FUNCTION();

    LOG4K_SET_OUT(Kevin55::OUT_TO_STDOUT_AND_LOG);
    LOGFMTT(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTD(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTI(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTW(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTE(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);

    LOGFMTT_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTD_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTI_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTW_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);
    LOGFMTE_A("----------Function:%s, Line:%d", __FUNCTION__, __LINE__);

    HANDLE hThread[2];
    ZeroMemory(hThread, sizeof(HANDLE) * 2);

    hThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LogTestThread1, NULL, 0, NULL);
    hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LogTestThread2, NULL, 0, NULL);


    for (int i = 0; i < 100; i++)
    {
        LOGFMTD(L"[%d]======%s", i, __FUNCTIONW__);
    }

    WaitForMultipleObjects(2, hThread, true, INFINITE);
    //LOG4K_SET_OUT(OUT_TO_STDOUT);

    LOGFMTT(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTD(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTI(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTW(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);
    LOGFMTE(L"----------Function:%s, Line:%d", __FUNCTIONW__, __LINE__);

    return 0;
}

使用nmake编译及链接完成后,还有error信息,下面是所有nmake输出:

    cl /c /Zi /W3 /WX- /sdl /O2 /Oi /Oy- /GL /D WIN32 /D NDEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope   /FoD:codeCPP_CodeKernalMainAppLog4ZLog4ZoutLog4K.obj /FdD:codeCPP_CodeKernalMainAppLog4ZLog4Zoutvc120.pdb /Gd /TP /analyze- /errorReport:prompt D:codeCPP_CodeKernalMainAppLog4ZLog4ZLog4K.cpp
Log4K.cpp
    link /ERRORREPORT:PROMPT /OUT:D:codeCPP_CodeKernalMainAppLog4ZLog4ZoutLog4K.exe /INCREMENTAL /NOLOGO /LIBPATH:"C:Program Files (x86)Microsoft SDKsWindowsv7.1ALib";"C:Program Files (x86)Microsoft Visual Studio 12.0VClib" kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /DEBUG /PDB:D:codeCPP_CodeKernalMainAppLog4ZLog4ZoutLog4K.pdp /LTCG /SUBSYSTEM:CONSOLE /DYNAMICBASE /NXCOMPAT /IMPLIB:D:codeCPP_CodeKernalMainAppLog4ZLog4ZoutLog4Z.lib /MACHINE:X86 D:codeCPP_CodeKernalMainAppLog4ZLog4Zoutlog4k.obj
LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
Generating code
Finished generating code
    cl  Log4K.cpp
Log4K.cpp
C:Program Files (x86)Microsoft Visual Studio 12.0VCINCLUDExlocale(337) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Log4K.cpp(427) : error C2664: 'BOOL SetConsoleTitleA(LPCSTR)' : cannot convert argument 1 from 'const wchar_t [14]' to 'LPCSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Log4K.cpp(513) : error C2664: 'DWORD GetModuleFileNameA(HMODULE,LPSTR,DWORD)' : cannot convert argument 2 from 'wchar_t [128]' to 'LPSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

网上好像有人提交过类似的问题:https://connect.microsoft.com/VisualStudio/feedback/details/808657/vs2013-compiler-fails-on-function-templates-with-c-11-alternate-declaration-syntax,这个问题可能是编译器的一个bug。
 

原文地址:https://www.cnblogs.com/kernel0815/p/4720690.html