test4log.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // test4log.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <assert.h>
  5. #include <time.h>
  6. #include "log4vendor.h"
  7. #include <iostream>
  8. int testInt()
  9. {
  10. int result = 0;
  11. TRACE4VTM_FUNCTION(&result);
  12. /*
  13. ...
  14. ...
  15. */
  16. result = 95555;
  17. /*
  18. ...
  19. ...
  20. */
  21. return result;
  22. }
  23. int testDWORD()
  24. {
  25. DWORD result = 0;
  26. TRACE4VTM_FUNCTION(&result);
  27. /*
  28. ...
  29. ...
  30. */
  31. result = 4026531840; /* 0xF0000000 */
  32. /*
  33. ...
  34. ...
  35. */
  36. return result;
  37. }
  38. void testVoid()
  39. {
  40. LOG4VTM_FUNCTION();
  41. int count = 0;
  42. return;
  43. }
  44. void logInitize_FileRecord()
  45. {
  46. std::string err_msg;
  47. cmb::log_init_config config;
  48. config.dev_name =_T("PinPad");
  49. config.log_type = CMB_LOG_TYPE_FILE;
  50. config.log_level = CMB_LOG_LEVEL_ALL;
  51. config.log_dir =_T("C:\\rvc\\dbg\\");
  52. cmb::log4vendor::init(config, err_msg);
  53. assert(err_msg.empty());
  54. LOG4VTM(TRACE,_T("This is first TRACE type message."));
  55. LOG4VTM(INFO,_T("This is first INFO type message."));
  56. LOG4VTM(WARN,_T("This is first WARN type message."));
  57. LOG4VTM(ERROR,_T("This is first ERROR type message."));
  58. LOG4VTM(FATAL,_T("This is first FATAL type message."));
  59. }
  60. void logInitize_Console()
  61. {
  62. std::string err_msg;
  63. cmb::log_init_config config;
  64. config.dev_name =_T("PinPad");
  65. config.log_type = CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_CONSOLE;
  66. config.log_level = CMB_LOG_LEVEL_ALL;
  67. config.log_dir =_T("C:\\rvc\\dbg\\");
  68. cmb::log4vendor::init(config, err_msg);
  69. std::cout << err_msg << std::endl;
  70. assert(err_msg.empty());
  71. LOG4VTM(TRACE,_T("This is first TRACE type message."));
  72. LOG4VTM(INFO,_T("This is first INFO type message."));
  73. LOG4VTM(WARN,_T("This is first WARN type message."));
  74. LOG4VTM(ERROR,_T("This is first ERROR type message."));
  75. LOG4VTM(FATAL,_T("This is first FATAL type message."));
  76. }
  77. void logInitize_SupportDirPathWithoutSlash()
  78. {
  79. std::string err_msg;
  80. cmb::log_init_config config;
  81. config.dev_name =_T("PinPad");
  82. config.log_type = CMB_LOG_TYPE_FILE;
  83. config.log_level = CMB_LOG_LEVEL_ALL;
  84. config.log_dir =_T("C:\\rvc\\dbg");
  85. cmb::log4vendor::init(config, err_msg);
  86. assert(err_msg.empty());
  87. }
  88. void logInitize_SupportDiffLogLevel_ERROR()
  89. {
  90. std::string err_msg;
  91. cmb::log_init_config config;
  92. config.dev_name =_T("PinPad");
  93. config.log_type = CMB_LOG_TYPE_FILE;
  94. config.log_level = CMB_LOG_LEVEL_ERROR;
  95. config.log_dir =_T("C:\\rvc\\dbg\\");
  96. cmb::log4vendor::init(config, err_msg);
  97. assert(err_msg.empty());
  98. LOG4VTM(TRACE,_T("The TRACE type message would not record!"));
  99. LOG4VTM(INFO,_T("The INFO type message would not record!"));
  100. LOG4VTM(WARN,_T("The WARN type message would not record!"));
  101. LOG4VTM(ERROR,_T("This is first ERROR type message."));
  102. LOG4VTM(FATAL,_T("This is first FATAL type message."));
  103. }
  104. #include <process.h>
  105. #define random(x) (rand()%x)
  106. int get_random(int reft, int right)
  107. {
  108. int c = random(right);
  109. while(c < reft) {
  110. c = random(right);
  111. }
  112. return c;
  113. }
  114. UINT WINAPI thread1(LPVOID param)
  115. {
  116. int i = (int)param;
  117. TRACE4VTM_FUNCTION(&i);
  118. LOG4VTM(INFO,_T("current thread: ") << GetCurrentThreadId());
  119. const int times = 3000;
  120. int count = 0;
  121. do
  122. {
  123. const int nInterval = get_random(0, 3000);
  124. const int nLevel = nInterval % 6;
  125. if(nLevel == 0) {
  126. LOG4VTM(ERROR,_T("this is a ERROR log, times: ") << count);
  127. } else if(nLevel == 1) {
  128. LOG4VTM(FATAL, _T("this is a FATAL log, times: ") << count);
  129. } else if(nLevel == 2) {
  130. LOG4VTM(INFO,_T("this is a INFO log, times: ") << count);
  131. } else if(nLevel == 3) {
  132. LOG4VTM(DEBUG,_T("this is a DEBUG log, times: ") << count);
  133. } else if(nLevel == 4) {
  134. LOG4VTM(WARN,_T("this is a WRAN log, times: ") << count);
  135. } else if(nLevel == 5) {
  136. LOG4VTM(TRACE,_T("this is a TRACE log, times: ") << count);
  137. }
  138. } while (++count < times);
  139. return 0;
  140. }
  141. void MultiThreadTest()
  142. {
  143. srand((int)time(0));
  144. int count = 30;
  145. HANDLE handle[30];
  146. for(int i=0; i<30; ++i)
  147. handle[i] = (HANDLE)_beginthreadex(NULL, 0,thread1, (LPVOID)i, 0, NULL);
  148. WaitForMultipleObjects(30, handle, TRUE, INFINITE);
  149. }
  150. void logInitize_SupportDiffLogLevel_OFF()
  151. {
  152. std::string err_msg;
  153. cmb::log_init_config config;
  154. config.dev_name =_T("PinPad");
  155. config.log_type = CMB_LOG_TYPE_FILE;
  156. config.log_level = CMB_LOG_LEVEL_OFF;
  157. config.log_dir =_T("C:\\rvc\\dbg\\");
  158. cmb::log4vendor::init(config, err_msg);
  159. assert(err_msg.empty());
  160. LOG4VTM(TRACE,_T("The TRACE type message would not record!"));
  161. LOG4VTM(INFO,_T("The INFO type message would not record!"));
  162. LOG4VTM(WARN,_T("The WARN type message would not record!"));
  163. LOG4VTM(ERROR,_T("The ERROR type message would not record!"));
  164. LOG4VTM(FATAL,_T("The FATAL type message would not record!"));
  165. }
  166. int _tmain(int argc, _TCHAR* argv[])
  167. {
  168. std::string err_msg;
  169. assert(cmb::log4vendor::instance() != NULL);
  170. {
  171. cmb::log_init_config config1;
  172. config1.dev_name =_T("");
  173. config1.log_type = CMB_LOG_TYPE_FILE;
  174. config1.log_dir = _T("C:\\rvc\\dbg\\");
  175. cmb::log4vendor::init(config1, err_msg);
  176. LOG4VTM(INFO,_T("This message would not be record with illegal dev name!"));
  177. assert(!err_msg.empty() &&_T(""));
  178. }
  179. {
  180. cmb::log_init_config config2;
  181. config2.dev_name =_T("PinPad");
  182. config2.log_type = ~(CMB_LOG_TYPE_CONSOLE | CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_SOCKET);
  183. config2.log_dir =_T("C:\\rvc\\dbg\\");
  184. cmb::log4vendor::init(config2, err_msg);
  185. LOG4VTM(INFO,_T("This message would not be record with illegal log type!"));
  186. assert(!err_msg.empty() &&_T(""));
  187. }
  188. {
  189. cmb::log_init_config config;
  190. config.dev_name =_T("PinPad");
  191. config.log_type = CMB_LOG_TYPE_FILE;
  192. config.log_dir =_T("");
  193. cmb::log4vendor::init(config, err_msg);
  194. LOG4VTM(INFO,_T("This message would not be record with illegal dir path !"));
  195. assert(!err_msg.empty() &&_T(""));
  196. }
  197. /*test befor log initialize.*/
  198. testVoid();
  199. testInt();
  200. testDWORD();
  201. logInitize_FileRecord();
  202. //logInitize_Console();
  203. //logInitize_SupportDirPathWithoutSlash();
  204. //logInitize_SupportDiffLogLevel_ERROR();
  205. //logInitize_SupportDiffLogLevel_OFF();
  206. testVoid();
  207. testInt();
  208. testDWORD();
  209. MultiThreadTest();
  210. return 0;
  211. }