// test4log.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include "log4vendor.h" #include int testInt() { int result = 0; TRACE4VTM_FUNCTION(&result); /* ... ... */ result = 95555; /* ... ... */ return result; } int testDWORD() { DWORD result = 0; TRACE4VTM_FUNCTION(&result); /* ... ... */ result = 4026531840; /* 0xF0000000 */ /* ... ... */ return result; } void testVoid() { LOG4VTM_FUNCTION(); int count = 0; return; } void logInitize_FileRecord() { std::string err_msg; cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ALL; config.log_dir =_T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,_T("This is first TRACE type message.")); LOG4VTM(INFO,_T("This is first INFO type message.")); LOG4VTM(WARN,_T("This is first WARN type message.")); LOG4VTM(ERROR,_T("This is first ERROR type message.")); LOG4VTM(FATAL,_T("This is first FATAL type message.")); } void logInitize_Console() { std::string err_msg; cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_CONSOLE; config.log_level = CMB_LOG_LEVEL_ALL; config.log_dir =_T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config, err_msg); std::cout << err_msg << std::endl; assert(err_msg.empty()); LOG4VTM(TRACE,_T("This is first TRACE type message.")); LOG4VTM(INFO,_T("This is first INFO type message.")); LOG4VTM(WARN,_T("This is first WARN type message.")); LOG4VTM(ERROR,_T("This is first ERROR type message.")); LOG4VTM(FATAL,_T("This is first FATAL type message.")); } void logInitize_SupportDirPathWithoutSlash() { std::string err_msg; cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ALL; config.log_dir =_T("C:\\rvc\\dbg"); cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); } void logInitize_SupportDiffLogLevel_ERROR() { std::string err_msg; cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ERROR; config.log_dir =_T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,_T("The TRACE type message would not record!")); LOG4VTM(INFO,_T("The INFO type message would not record!")); LOG4VTM(WARN,_T("The WARN type message would not record!")); LOG4VTM(ERROR,_T("This is first ERROR type message.")); LOG4VTM(FATAL,_T("This is first FATAL type message.")); } #include #define random(x) (rand()%x) int get_random(int reft, int right) { int c = random(right); while(c < reft) { c = random(right); } return c; } UINT WINAPI thread1(LPVOID param) { int i = (int)param; TRACE4VTM_FUNCTION(&i); LOG4VTM(INFO,_T("current thread: ") << GetCurrentThreadId()); const int times = 3000; int count = 0; do { const int nInterval = get_random(0, 3000); const int nLevel = nInterval % 6; if(nLevel == 0) { LOG4VTM(ERROR,_T("this is a ERROR log, times: ") << count); } else if(nLevel == 1) { LOG4VTM(FATAL, _T("this is a FATAL log, times: ") << count); } else if(nLevel == 2) { LOG4VTM(INFO,_T("this is a INFO log, times: ") << count); } else if(nLevel == 3) { LOG4VTM(DEBUG,_T("this is a DEBUG log, times: ") << count); } else if(nLevel == 4) { LOG4VTM(WARN,_T("this is a WRAN log, times: ") << count); } else if(nLevel == 5) { LOG4VTM(TRACE,_T("this is a TRACE log, times: ") << count); } } while (++count < times); return 0; } void MultiThreadTest() { srand((int)time(0)); int count = 30; HANDLE handle[30]; for(int i=0; i<30; ++i) handle[i] = (HANDLE)_beginthreadex(NULL, 0,thread1, (LPVOID)i, 0, NULL); WaitForMultipleObjects(30, handle, TRUE, INFINITE); } void logInitize_SupportDiffLogLevel_OFF() { std::string err_msg; cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_OFF; config.log_dir =_T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,_T("The TRACE type message would not record!")); LOG4VTM(INFO,_T("The INFO type message would not record!")); LOG4VTM(WARN,_T("The WARN type message would not record!")); LOG4VTM(ERROR,_T("The ERROR type message would not record!")); LOG4VTM(FATAL,_T("The FATAL type message would not record!")); } int _tmain(int argc, _TCHAR* argv[]) { std::string err_msg; assert(cmb::log4vendor::instance() != NULL); { cmb::log_init_config config1; config1.dev_name =_T(""); config1.log_type = CMB_LOG_TYPE_FILE; config1.log_dir = _T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config1, err_msg); LOG4VTM(INFO,_T("This message would not be record with illegal dev name!")); assert(!err_msg.empty() &&_T("")); } { cmb::log_init_config config2; config2.dev_name =_T("PinPad"); config2.log_type = ~(CMB_LOG_TYPE_CONSOLE | CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_SOCKET); config2.log_dir =_T("C:\\rvc\\dbg\\"); cmb::log4vendor::init(config2, err_msg); LOG4VTM(INFO,_T("This message would not be record with illegal log type!")); assert(!err_msg.empty() &&_T("")); } { cmb::log_init_config config; config.dev_name =_T("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_dir =_T(""); cmb::log4vendor::init(config, err_msg); LOG4VTM(INFO,_T("This message would not be record with illegal dir path !")); assert(!err_msg.empty() &&_T("")); } /*test befor log initialize.*/ testVoid(); testInt(); testDWORD(); logInitize_FileRecord(); //logInitize_Console(); //logInitize_SupportDirPathWithoutSlash(); //logInitize_SupportDiffLogLevel_ERROR(); //logInitize_SupportDiffLogLevel_OFF(); testVoid(); testInt(); testDWORD(); MultiThreadTest(); return 0; }