µð¹ö±× ·Î±×¸¦ ÆÄÀÏ·Î ÀúÀåÇØ º¸ÀÚ. µð¹ö±× ·Î±×¸¦ ÆÄÀÏ·Î ÀúÀåÇϱâ À§ÇØ ÇÊ¿äÇÑ ±â´ÉÀº ´ÙÀ½°ú °°´Ù.
ÇÊ¿äÇÑ Çì´õ ÆÄÀÏ #include <iostream>
#include <string> #include <cstdarg> //for va_start #include <windows.h> //for GetSystemTime, GetLocalTime #include <fstream> //for std::wofstream #include <sstream> //for sstream 1. °¡º¯ Àμö ¸ñ·ÏÀ» ¹Þ´Â ÇÔ¼ö ±¸Çö std::wstring wformat_list(const wchar_t *fmt, ...)
{ va_list va; va_start(va, fmt); //Returns the number of characters in the string referenced by a list of arguments. int len = _vscwprintf(fmt, va) + 1; // _vscwprintf doesn't count terminating '\0' std::wstring str; str.resize(len); vswprintf_s(const_cast<wchar_t*>(str.c_str()), len, fmt, va); va_end(va); return str; } //1. wformat_list test std::wcout << "1. wformat_list test" << std::endl; std::wstring testStr1 = wformat_list(L"MyTest %02d, %02d", 3, 8); std::wcout << testStr1 << std::endl << std::endl; 2. ½Ã½ºÅÛ Å¸ÀÓ(UTC) »ç¿ë // GetSystemTime UTC Ç¥Áؽð£
std::wstring GetSystemTime() { SYSTEMTIME st; GetSystemTime(&st); std::wstring str = wformat_list(L"%d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); return str; } //2. GetSystemTime test std::wcout << "2. GetSystemTime test" << std::endl; std::wstring testStr2 = GetSystemTime(); std::wcout << testStr2 << std::endl << std::endl; 3. ·ÎÄà ŸÀÓ »ç¿ë // GetLocalTime ÄÄÇ»ÅÍÀÇ ½Ã°£
std::wstring GetLocalTime() { SYSTEMTIME st; GetLocalTime(&st); /* std::wstring str = wformat_list(L"%d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); */ std::wstring str = wformat_list(L"%d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); return str; } //3. GetLocalTime test std::wcout << "3. GetLocalTime test" << std::endl; std::wstring testStr3 = GetLocalTime(); std::wcout << testStr3 << std::endl << std::endl; 4. ÆÄÀÏ·Î ·Î±× ÀúÀå void WriteTestLog()
{ std::wofstream outFile(L"debugTest.log"); if (outFile.is_open()) { outFile << L"Test" << L"\n"; outFile.close(); } } //4. Test Debug File std::wcout << "4. Save debugTest.log File" << std::endl; WriteTestLog(); 5. Variadic Macros Log test #define MY_LOG(...) wformat_list (__VA_ARGS__)
//5. Variadic Macros Log test std::wcout << std::endl << std::endl << "5. MY_LOG test" << std::endl; std::wstring testStr5 = MY_LOG(L"LOG %02d, %02d", 3, 8); std::wcout << testStr5 << std::endl << std::endl; 6. Debug Line LOG °¡º¯ ¸ÅÅ©·Î MY_DLOG¸¦ ÀÌ¿ëÇØ ·Î±×¸¦ ÆÄÀÏ·Î ÀúÀå ÇÑ´Ù. std::wofstream outFile(L"debug.log", std::ios::app)¿¡¼ std::ios::app Àμö°¡ ÀÖ¾î¾ß ÆÄÀÏÀ» Ãß°¡ÇÑ´Ù. #define MY_DLOG(fmt, ...) DebugLog (__FUNCTIONW__, __LINE__, fmt, ##__VA_ARGS__)
void DebugLog(const wchar_t* funcName, int nLine, const wchar_t *fmt, ...) { //variable argument string log va_list va; va_start(va, fmt); int len = _vscwprintf(fmt, va) + 1; // _vscwprintf doesn't count terminating '\0' std::wstring str; str.resize(len); vswprintf_s(const_cast<wchar_t*>(str.c_str()), len, fmt, va); va_end(va); //format: date:function_name(line)-log std::wstringstream stream; stream << GetLocalTime() << ":" << funcName << "(" << nLine << ")-" << str << L"\n"; //std::wcout << stream.str(); //save file std::wofstream outFile(L"debug.log", std::ios::app); if (outFile.is_open()) { outFile << stream.str(); outFile.close(); } } //6. Debug Line LOG std::wcout << "6. save debug.log File. " << std::endl; MY_DLOG(L"LOGFILE Test %02d, %02d", 3, 8); ´Ù¿î·Îµå: debug_log.cpp |