123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include <gtest/gtest.h>
- #include "log.h"
- #include "log_mgr.h"
- #include "log_factory.h"
- #include "log_single.h"
- #include "log_periodic.h"
- #include "log_udpclient.h"
- #include "log_udpdaemon.h"
- TEST(LogTest, LogPeriodicFactory)
- {
- logfactory_t* factory;
- logbase_t* log_inst = NULL;
- ASSERT_TRUE(periodicfilefactory_create(&factory) == 0);
- log_inst = (logbase_t*)factory->create_log(factory, "inst");
- ASSERT_TRUE(log_inst != NULL);
- ASSERT_TRUE(factory->init_log(factory, log_inst) == 0);
- ASSERT_TRUE(factory->set_log_param(factory, log_inst, "invalid", NULL) != 0);
- ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", NULL) != 0);
- ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", "test_inst.log") == 0);
- //TODO:
- factory->term_log(factory, log_inst);
- factory->destroy_log(factory, log_inst);
- factory->destroy(factory);
- }
- TEST(LogTest, LogMgrRoutine)
- {
- const auto args = ::testing::internal::GetArgvs();
- EXPECT_FALSE(args.empty());
- std::string test_dir("");
- if (args.size() == 1) {
- auto dir = args[0].find_last_of('/');
- if (dir == std::string::npos)
- dir = args[0].find_last_of('\\');
- if (dir != std::string::npos) {
- test_dir = args[0].substr(0, dir + 1);
- test_dir += "test_libtoolkit_area";
- }
- EXPECT_TRUE(!test_dir.empty());
- }
- else {
- test_dir = args[1];
- }
- std::cout << "test dir: " << test_dir << std::endl;
- logmgr_t* mgr;
- auto rc = logmgr_create(&mgr);
- ASSERT_TRUE(rc == 0);
- logfactory_t* t;
- ASSERT_TRUE(0 == periodicfilefactory_create(&t));
- ASSERT_TRUE(0 == logmgr_register_factory(mgr, t));
- ASSERT_FALSE(0 == logmgr_create_log(mgr, "no_exist", NULL, NULL));
- logbase_t *log_base;
- /* invoke 'logfactory_create_log' and log_base become 'periodicfile_t' type*/
- ASSERT_TRUE(0 == logmgr_create_log(mgr, "periodic", "log_instance", &log_base));
- ASSERT_FALSE(stringmap_find(mgr->log_table, "log_instance") == NULL);
- ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "level", "All"));
- ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "use_lock", "1"));
- std::string log_pattern = test_dir;
- log_pattern += "/{yyyy}{MM}{dd}.log";
- ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "file", log_pattern.c_str()));
- /*the c trap:
- you must know what it would be deal with before you put the param in and force to convert to void* type*/
- logfactory_init_log((void*)t, (void*)log_base);
- /*to log*/
- FILETIME ft;
- SYSTEMTIME st;
- #ifdef _WIN32
- GetSystemTime(&st);
- SystemTimeToFileTime(&st, &ft);
- #else
- ft.dwHighDateTime = ft.dwLowDateTime = 0;
- #endif // _WIN32
- ASSERT_TRUE(0 == logfactory_log_record(
- t, log_base, XLOG_LEVEL_INFO, ft.dwLowDateTime, ft.dwHighDateTime, "I am log test", strlen("I am log test")));
- /*let logmgr_destroy to do it!*/
- //logmgr_destroy_log(mgr, log_base);
- logmgr_destroy(mgr);
- }
- TEST(LogTest, LogInitTest)
- {
- auto rc = xlog_init(NULL);
- ASSERT_TRUE(rc == 0);
-
- char log_file_dir_format[260];
- #ifdef _WIN32
- strcpy(log_file_dir_format, "C:\\rvc\\dbg\\logTest\\{yyyy}{MM}{dd}.log");
- #else
- strcpy(log_file_dir_format, "/opt/rvc/dbg/logTest/{yyyy}{MM}{dd}.log");
- #endif // _WIN32
- ASSERT_TRUE(0 != xlog_add_logger(NULL, NULL));
- ASSERT_TRUE(0 != xlog_add_logger("", ""));
- rc = xlog_add_logger("logTest",
- "periodic",
- "level", "All",
- "use_lock", "1",
- "file", log_file_dir_format,
- NULL);
- ASSERT_TRUE(rc == 0);
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_ERROR, "error test %s, %d", "hello world", 12345));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
- ASSERT_FALSE(0 == xlog_log_f("NoneTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_WARN, "warn test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_INFO, "info test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_DEBUG, "debug test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_TRACE, "trace test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_NONE, "none test %s", "hello world"));
- ASSERT_TRUE(0 == xlog_log("logTest", XLOG_LEVEL_TRACE, "log tace test."));
- ASSERT_FALSE(0 == xlog_log("NoneTeset", XLOG_LEVEL_TRACE, "log tace test."));
- rc = xlog_term();
- ASSERT_TRUE(rc == 0);
- }
|