Guozhong/check format (#10184)
* remove formatTimeMilli from time_utils.cpp * add traceCallStacks test case * add traceCallStacks test case in format_test.cpp * add param:"test" to function TraceCallStacks() * catch the exception of checkFormat * add space for try catch * rollback time_utils.cpp time_utils.hpp and log_utils_format_test.cpp * modify testcase for log.hpp * modify testcase from format_s to format_s_d_ld_u_lu2
This commit is contained in:
1 parent
7abd61f867
commit
14c1e98e8c
2 files changed
+229
-29
No files matched your search
@@ -241,10 +241,17 @@ inline void Log::doLog(bool on, bool isTraceCallStack, LogLevel level, const cha
|
||||
stream << '[' << tag << ']';
|
||||
}
|
||||
char buffer[255];
|
||||
checkFormat(fmt);
|
||||
std::string compatibleString = "%s" + std::string(fmt);
|
||||
std::snprintf(&buffer[0], sizeof(buffer), compatibleString.c_str(), "", args...);
|
||||
stream << ' ' << buffer << suffix << colorEnd(level);
|
||||
std::string compatibleString;
|
||||
|
||||
try {
|
||||
checkFormat(fmt);
|
||||
compatibleString = "%s" + std::string(fmt);
|
||||
std::snprintf(&buffer[0], sizeof(buffer), compatibleString.c_str(), "", args...);
|
||||
stream << ' ' << buffer << suffix << colorEnd(level);
|
||||
} catch (std::runtime_error& err) {
|
||||
stream << ' ' << err.what() << colorEnd(level);
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> autoLock(mutex);
|
||||
print(stream);
|
||||
}
|
||||
|
||||
@@ -21,113 +21,306 @@ public:
|
||||
};
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_s) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%s", "DEBUG"));
|
||||
LOG_DEBUG("%sabc", "DEBUG");
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
TEST_F(LogUtilsFormatTest, format_d) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%d", -1));
|
||||
LOG_DEBUG("%dabc", -1);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_ld) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%ld", -3));
|
||||
LOG_DEBUG("%ldabc", -3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_u) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%u", 1));
|
||||
LOG_DEBUG("%uabc", 1);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_lu) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%lu", 3));
|
||||
LOG_DEBUG("%luabc", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_s_d_ld_u_lu) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%s,%d,%ld,%u,%lu", "DEBUG", -1, -3, 1, 3));
|
||||
LOG_DEBUG("%s,%d,%ld,%u,%lu,abc", "DEBUG", -1, -3, 1, 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_s_d_ld_u_lu2) {
|
||||
std::string printResult = "";
|
||||
std::string pattern{"abc"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
ASSERT_NO_THROW(LOG_DEBUG("%s%d%ld%u%lu", "DEBUG", -1, -3, 1, 3));
|
||||
LOG_DEBUG("%s%d%ld%u%luabc", "DEBUG", -1, -3, 1, 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_p) {
|
||||
ASSERT_THROW(LOG_DEBUG("%p", MockLog::_mockLog), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%p", MockLog::_mockLog);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_x) {
|
||||
ASSERT_THROW(LOG_DEBUG("%x", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%x", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_X) {
|
||||
ASSERT_THROW(LOG_DEBUG("%X", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%X", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_o) {
|
||||
ASSERT_THROW(LOG_DEBUG("%o", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%o", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_e) {
|
||||
ASSERT_THROW(LOG_DEBUG("%e", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%e", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_E) {
|
||||
ASSERT_THROW(LOG_DEBUG("%E", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%E", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_f) {
|
||||
ASSERT_THROW(LOG_DEBUG("%f", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%f", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_F) {
|
||||
ASSERT_THROW(LOG_DEBUG("%F", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%F", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_g) {
|
||||
ASSERT_THROW(LOG_DEBUG("%g", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%g", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_G) {
|
||||
ASSERT_THROW(LOG_DEBUG("%G", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%G", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_a) {
|
||||
ASSERT_THROW(LOG_DEBUG("%a", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%a", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_A) {
|
||||
ASSERT_THROW(LOG_DEBUG("%A", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%A", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_c) {
|
||||
ASSERT_THROW(LOG_DEBUG("%c", 3), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%c", 3);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_n) {
|
||||
int num = 0;
|
||||
ASSERT_THROW(LOG_DEBUG("%n", &num), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%n", &num);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format__) {
|
||||
ASSERT_THROW(LOG_DEBUG("%%"), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%%");
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_s__) {
|
||||
ASSERT_THROW(LOG_DEBUG("%s%%", "DEBUG"), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%s%%", "DEBUG");
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_dn) {
|
||||
int num = 0;
|
||||
ASSERT_THROW(LOG_DEBUG("%d%n", num, &num), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("%d%n", num, &num);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, format_ccccdn) {
|
||||
int num = 0;
|
||||
ASSERT_THROW(LOG_DEBUG("cccc%d%n", num, &num), std::exception);
|
||||
std::string printResult = "";
|
||||
std::string pattern{"not valid"};
|
||||
std::regex regex(pattern);
|
||||
ON_CALL(*(HLogger), print(_)).WillByDefault([&](std::stringstream& stream) {
|
||||
printResult = stream.str();
|
||||
});
|
||||
EXPECT_CALL(*(HLogger), print(_)).Times(1);
|
||||
LOG_DEBUG("cccc%d%n", num, &num);
|
||||
EXPECT_TRUE(std::regex_search(printResult, regex));
|
||||
}
|
||||
|
||||
TEST_F(LogUtilsFormatTest, logPrintFormat_error) {
|
||||
|
||||
Reference in new issue
Block a user