// test4log.cpp : 定义控制台应用程序的入口点。 // #if defined(_MSC_VER) #include "stdafx.h" #else #define WINAPI #endif //_MSC_VER #include #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 =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ALL; #if defined(_MSC_VER) config.log_dir = ("C:\\rvc\\dbg\\"); #else config.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,("This is first TRACE type message.")); LOG4VTM(INFO,("This is first INFO type message.")); LOG4VTM(WARN,("This is first WARN type message.")); LOG4VTM(ERROR,("This is first ERROR type message.")); LOG4VTM(FATAL,("This is first FATAL type message.")); } void logInitize_Console() { std::string err_msg; cmb::log_init_config config; config.dev_name =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_CONSOLE; config.log_level = CMB_LOG_LEVEL_ALL; #if defined(_MSC_VER) config.log_dir = ("C:\\rvc\\dbg\\"); #else config.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config, err_msg); std::cout << err_msg << std::endl; assert(err_msg.empty()); LOG4VTM(TRACE,("This is first TRACE type message.")); LOG4VTM(INFO,("This is first INFO type message.")); LOG4VTM(WARN,("This is first WARN type message.")); LOG4VTM(ERROR,("This is first ERROR type message.")); LOG4VTM(FATAL,("This is first FATAL type message.")); } void logInitize_SupportDirPathWithoutSlash() { std::string err_msg; cmb::log_init_config config; config.dev_name =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ALL; #if defined(_MSC_VER) config.log_dir = ("C:\\rvc\\dbg\\"); #else config.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER 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 =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_ERROR; #if defined(_MSC_VER) config.log_dir = ("C:\\rvc\\dbg\\"); #else config.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,("The TRACE type message would not record!")); LOG4VTM(INFO,("The INFO type message would not record!")); LOG4VTM(WARN,("The WARN type message would not record!")); LOG4VTM(ERROR,("This is first ERROR type message.")); LOG4VTM(FATAL,("This is first FATAL type message.")); } #if defined(_MSC_VER) #include #else #include #endif //_MSC_VER #define random(x) (rand()%x) int get_random(int reft, int right) { int c = random(right); while(c < reft) { c = random(right); } return c; } void thread1(int param) { #if defined(_MSC_VER) LOG4VTM(INFO, ("current thread: ") << GetCurrentThreadId()); #else LOG4VTM(INFO, ("current thread: ") << (int)pthread_self()); #endif //_MSC_VER 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,("this is a ERROR log, times: ") << count); } else if(nLevel == 1) { LOG4VTM(FATAL, ("this is a FATAL log, times: ") << count); } else if(nLevel == 2) { LOG4VTM(INFO,("this is a INFO log, times: ") << count); } else if(nLevel == 3) { LOG4VTM(DEBUG,("this is a DEBUG log, times: ") << count); } else if(nLevel == 4) { LOG4VTM(WARN,("this is a WRAN log, times: ") << count); } else if(nLevel == 5) { LOG4VTM(TRACE,("this is a TRACE log, times: ") << count); } } while (++count < times); } void MultiThreadTest() { srand((int)time(0)); int count = 30; std::thread* handle[30]; for (int i = 0; i < 30; ++i) handle[i] = new std::thread(thread1, i); for (int i = 0; i < 30; ++i) { if (handle[i]->joinable()) handle[i]->join(); delete handle[i]; handle[i] = nullptr; } } void logInitize_SupportDiffLogLevel_OFF() { std::string err_msg; cmb::log_init_config config; config.dev_name =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_level = CMB_LOG_LEVEL_OFF; #if defined(_MSC_VER) config.log_dir = ("C:\\rvc\\dbg\\"); #else config.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config, err_msg); assert(err_msg.empty()); LOG4VTM(TRACE,("The TRACE type message would not record!")); LOG4VTM(INFO,("The INFO type message would not record!")); LOG4VTM(WARN,("The WARN type message would not record!")); LOG4VTM(ERROR,("The ERROR type message would not record!")); LOG4VTM(FATAL,("The FATAL type message would not record!")); } #if defined(_MSC_VER) int _tmain(int argc, _TCHAR* argv[]) #else int main(int argc, char* argv[]) #endif //_MSC_VER { std::string err_msg; assert(cmb::log4vendor::instance() != NULL); { cmb::log_init_config config1; config1.dev_name =(""); config1.log_type = CMB_LOG_TYPE_FILE; #if defined(_MSC_VER) config1.log_dir = ("C:\\rvc\\dbg\\"); #else config1.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config1, err_msg); LOG4VTM(INFO,("This message would not be record with illegal dev name!")); assert(!err_msg.empty() &&("")); } { cmb::log_init_config config2; config2.dev_name =("PinPad"); config2.log_type = ~(CMB_LOG_TYPE_CONSOLE | CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_SOCKET); #if defined(_MSC_VER) config2.log_dir = ("C:\\rvc\\dbg\\"); #else config2.log_dir = ("/opt/rvc/dbg/"); #endif //_MSC_VER cmb::log4vendor::init(config2, err_msg); LOG4VTM(INFO,("This message would not be record with illegal log type!")); assert(!err_msg.empty() &&("")); } { cmb::log_init_config config; config.dev_name =("PinPad"); config.log_type = CMB_LOG_TYPE_FILE; config.log_dir =(""); cmb::log4vendor::init(config, err_msg); LOG4VTM(INFO,("This message would not be record with illegal dir path !")); assert(!err_msg.empty() &&("")); } /*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; }