test_log.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <gtest/gtest.h>
  2. #include "log.h"
  3. #include "log_mgr.h"
  4. #include "log_factory.h"
  5. #include "log_single.h"
  6. #include "log_periodic.h"
  7. #include "log_udpclient.h"
  8. #include "log_udpdaemon.h"
  9. TEST(LogTest, LogPeriodicFactory)
  10. {
  11. logfactory_t* factory;
  12. logbase_t* log_inst = NULL;
  13. ASSERT_TRUE(periodicfilefactory_create(&factory) == 0);
  14. log_inst = (logbase_t*)factory->create_log(factory, "inst");
  15. ASSERT_TRUE(log_inst != NULL);
  16. ASSERT_TRUE(factory->init_log(factory, log_inst) == 0);
  17. ASSERT_TRUE(factory->set_log_param(factory, log_inst, "invalid", NULL) != 0);
  18. ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", NULL) != 0);
  19. ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", "test_inst.log") == 0);
  20. //TODO:
  21. factory->term_log(factory, log_inst);
  22. factory->destroy_log(factory, log_inst);
  23. factory->destroy(factory);
  24. }
  25. TEST(LogTest, LogMgrRoutine)
  26. {
  27. const auto args = ::testing::internal::GetArgvs();
  28. EXPECT_FALSE(args.empty());
  29. std::string test_dir("");
  30. if (args.size() == 1) {
  31. auto dir = args[0].find_last_of('/');
  32. if (dir == std::string::npos)
  33. dir = args[0].find_last_of('\\');
  34. if (dir != std::string::npos) {
  35. test_dir = args[0].substr(0, dir + 1);
  36. test_dir += "test_libtoolkit_area";
  37. }
  38. EXPECT_TRUE(!test_dir.empty());
  39. }
  40. else {
  41. test_dir = args[1];
  42. }
  43. std::cout << "test dir: " << test_dir << std::endl;
  44. logmgr_t* mgr;
  45. auto rc = logmgr_create(&mgr);
  46. ASSERT_TRUE(rc == 0);
  47. logfactory_t* t;
  48. ASSERT_TRUE(0 == periodicfilefactory_create(&t));
  49. ASSERT_TRUE(0 == logmgr_register_factory(mgr, t));
  50. ASSERT_FALSE(0 == logmgr_create_log(mgr, "no_exist", NULL, NULL));
  51. logbase_t *log_base;
  52. /* invoke 'logfactory_create_log' and log_base become 'periodicfile_t' type*/
  53. ASSERT_TRUE(0 == logmgr_create_log(mgr, "periodic", "log_instance", &log_base));
  54. ASSERT_FALSE(stringmap_find(mgr->log_table, "log_instance") == NULL);
  55. ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "level", "All"));
  56. ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "use_lock", "1"));
  57. std::string log_pattern = test_dir;
  58. log_pattern += "/{yyyy}{MM}{dd}.log";
  59. ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "file", log_pattern.c_str()));
  60. /*the c trap:
  61. you must know what it would be deal with before you put the param in and force to convert to void* type*/
  62. logfactory_init_log((void*)t, (void*)log_base);
  63. /*to log*/
  64. FILETIME ft;
  65. SYSTEMTIME st;
  66. #ifdef _WIN32
  67. GetSystemTime(&st);
  68. SystemTimeToFileTime(&st, &ft);
  69. #else
  70. ft.dwHighDateTime = ft.dwLowDateTime = 0;
  71. #endif // _WIN32
  72. ASSERT_TRUE(0 == logfactory_log_record(
  73. t, log_base, XLOG_LEVEL_INFO, ft.dwLowDateTime, ft.dwHighDateTime, "I am log test", strlen("I am log test")));
  74. /*let logmgr_destroy to do it!*/
  75. //logmgr_destroy_log(mgr, log_base);
  76. logmgr_destroy(mgr);
  77. }
  78. TEST(LogTest, LogInitTest)
  79. {
  80. auto rc = xlog_init(NULL);
  81. ASSERT_TRUE(rc == 0);
  82. char log_file_dir_format[260];
  83. #ifdef _WIN32
  84. strcpy(log_file_dir_format, "C:\\rvc\\dbg\\logTest\\{yyyy}{MM}{dd}.log");
  85. #else
  86. strcpy(log_file_dir_format, "/opt/rvc/dbg/logTest/{yyyy}{MM}{dd}.log");
  87. #endif // _WIN32
  88. ASSERT_TRUE(0 != xlog_add_logger(NULL, NULL));
  89. ASSERT_TRUE(0 != xlog_add_logger("", ""));
  90. rc = xlog_add_logger("logTest",
  91. "periodic",
  92. "level", "All",
  93. "use_lock", "1",
  94. "file", log_file_dir_format,
  95. NULL);
  96. ASSERT_TRUE(rc == 0);
  97. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_ERROR, "error test %s, %d", "hello world", 12345));
  98. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
  99. ASSERT_FALSE(0 == xlog_log_f("NoneTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
  100. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_WARN, "warn test %s", "hello world"));
  101. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_INFO, "info test %s", "hello world"));
  102. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_DEBUG, "debug test %s", "hello world"));
  103. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_TRACE, "trace test %s", "hello world"));
  104. ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_NONE, "none test %s", "hello world"));
  105. ASSERT_TRUE(0 == xlog_log("logTest", XLOG_LEVEL_TRACE, "log tace test."));
  106. ASSERT_FALSE(0 == xlog_log("NoneTeset", XLOG_LEVEL_TRACE, "log tace test."));
  107. rc = xlog_term();
  108. ASSERT_TRUE(rc == 0);
  109. }