123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- // test4log.cpp : 定义控制台应用程序的入口点。
- //
- #if defined(_MSC_VER)
- #include "stdafx.h"
- #else
- #define WINAPI
- #endif //_MSC_VER
- #include <assert.h>
- #include <time.h>
- #include <thread>
- #include "log4vendor.h"
- #include <iostream>
- 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 <process.h>
- #else
- #include <pthread.h>
- #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;
- }
|