test4log.cpp 6.7 KB

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