test4log.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. //Sleep(nInterval);
  166. } while (++count < times);
  167. }
  168. void MultiThreadTest()
  169. {
  170. srand((int)time(0));
  171. int count = 30;
  172. std::thread* handle[30];
  173. for (int i = 0; i < 30; ++i)
  174. handle[i] = new std::thread(thread1, i);
  175. for (int i = 0; i < 30; ++i) {
  176. if (handle[i]->joinable())
  177. handle[i]->join();
  178. delete handle[i];
  179. handle[i] = nullptr;
  180. }
  181. }
  182. void logInitize_SupportDiffLogLevel_OFF()
  183. {
  184. std::string err_msg;
  185. cmb::log_init_config config;
  186. config.dev_name =("PinPad");
  187. config.log_type = CMB_LOG_TYPE_FILE;
  188. config.log_level = CMB_LOG_LEVEL_OFF;
  189. #if defined(_MSC_VER)
  190. config.log_dir = ("C:\\rvc\\dbg\\");
  191. #else
  192. config.log_dir = ("/opt/rvc/dbg/");
  193. #endif //_MSC_VER
  194. cmb::log4vendor::init(config, err_msg);
  195. assert(err_msg.empty());
  196. LOG4VTM(TRACE,("The TRACE type message would not record!"));
  197. LOG4VTM(INFO,("The INFO type message would not record!"));
  198. LOG4VTM(WARN,("The WARN type message would not record!"));
  199. LOG4VTM(ERROR,("The ERROR type message would not record!"));
  200. LOG4VTM(FATAL,("The FATAL type message would not record!"));
  201. }
  202. #if defined(_MSC_VER)
  203. int _tmain(int argc, _TCHAR* argv[])
  204. #else
  205. int main(int argc, char* argv[])
  206. #endif //_MSC_VER
  207. {
  208. std::string err_msg;
  209. assert(cmb::log4vendor::instance() != NULL);
  210. {
  211. cmb::log_init_config config1;
  212. config1.dev_name =("");
  213. config1.log_type = CMB_LOG_TYPE_FILE;
  214. #if defined(_MSC_VER)
  215. config1.log_dir = ("C:\\rvc\\dbg\\");
  216. #else
  217. config1.log_dir = ("/opt/rvc/dbg/");
  218. #endif //_MSC_VER
  219. cmb::log4vendor::init(config1, err_msg);
  220. LOG4VTM(INFO,("This message would not be record with illegal dev name!"));
  221. assert(!err_msg.empty() &&(""));
  222. }
  223. {
  224. cmb::log_init_config config2;
  225. config2.dev_name =("PinPad");
  226. config2.log_type = ~(CMB_LOG_TYPE_CONSOLE | CMB_LOG_TYPE_FILE | CMB_LOG_TYPE_SOCKET);
  227. #if defined(_MSC_VER)
  228. config2.log_dir = ("C:\\rvc\\dbg\\");
  229. #else
  230. config2.log_dir = ("/opt/rvc/dbg/");
  231. #endif //_MSC_VER
  232. cmb::log4vendor::init(config2, err_msg);
  233. LOG4VTM(INFO,("This message would not be record with illegal log type!"));
  234. assert(!err_msg.empty() &&(""));
  235. }
  236. {
  237. cmb::log_init_config config;
  238. config.dev_name =("PinPad");
  239. config.log_type = CMB_LOG_TYPE_FILE;
  240. config.log_dir =("");
  241. cmb::log4vendor::init(config, err_msg);
  242. LOG4VTM(INFO,("This message would not be record with illegal dir path !"));
  243. assert(!err_msg.empty() &&(""));
  244. }
  245. /*test befor log initialize.*/
  246. testVoid();
  247. testInt();
  248. testDWORD();
  249. logInitize_FileRecord();
  250. //logInitize_Console();
  251. //logInitize_SupportDirPathWithoutSlash();
  252. //logInitize_SupportDiffLogLevel_ERROR();
  253. //logInitize_SupportDiffLogLevel_OFF();
  254. testVoid();
  255. testInt();
  256. testDWORD();
  257. MultiThreadTest();
  258. return 0;
  259. }