新写的c++日志库:log4K

时间:2023-03-09 19:24:39
新写的c++日志库:log4K

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

测试代码:

#include "log4k.h"

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

static int g_Cnt = ;
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 = ; i < ;)
{
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 = ; i < ;)
{
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[];
ZeroMemory(hThread, sizeof(HANDLE) * ); hThread[] = CreateThread(NULL, , (LPTHREAD_START_ROUTINE)LogTestThread1, NULL, , NULL);
hThread[] = CreateThread(NULL, , (LPTHREAD_START_ROUTINE)LogTestThread2, NULL, , NULL); for (int i = ; i < ; i++)
{
LOGFMTD(L"[%d]======%s", i, __FUNCTIONW__);
} WaitForMultipleObjects(, 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 ;
}

新写的c++日志库:log4K

使用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:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\Log4K.obj /FdD:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\vc120.pdb /Gd /TP /analyze- /errorReport:prompt D:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\Log4K.cpp
Log4K.cpp
link /ERRORREPORT:PROMPT /OUT:D:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\Log4K.exe /INCREMENTAL /NOLOGO /LIBPATH:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib";"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib" 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:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\Log4K.pdp /LTCG /SUBSYSTEM:CONSOLE /DYNAMICBASE /NXCOMPAT /IMPLIB:D:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\Log4Z.lib /MACHINE:X86 D:\code\CPP_Code\KernalMainApp\Log4Z\Log4Z\out\log4k.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.0\VC\INCLUDE\xlocale(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。