diff --git a/include/ylt/easylog/record.hpp b/include/ylt/easylog/record.hpp index 2dcd224f7..db882454f 100644 --- a/include/ylt/easylog/record.hpp +++ b/include/ylt/easylog/record.hpp @@ -52,6 +52,32 @@ enum { ERROR = 0 }; namespace easylog { +namespace detail { +template +constexpr inline bool c_array_v = std::is_array_v> && + std::extent_v> > 0; + +template +struct has_data : std::false_type {}; + +template +struct has_data().data())>> + : std::true_type {}; + +template +constexpr inline bool has_data_v = has_data>::value; + +template +struct has_str : std::false_type {}; + +template +struct has_str().str())>> + : std::true_type {}; + +template +constexpr inline bool has_str_v = has_str>::value; +} // namespace detail + enum class Severity { NONE = 0, TRACE = 1, @@ -138,9 +164,24 @@ class record_t { auto [ptr, ec] = std::to_chars(buf + 2, buf + 32, (uintptr_t)data, 16); ss_.append(buf, std::distance(buf, ptr)); } - else { + else if constexpr (std::is_same_v || + std::is_same_v) { + ss_.append(data.data(), data.size()); + } + else if constexpr (detail::c_array_v) { ss_.append(data); } + else if constexpr (detail::has_data_v) { + ss_.append(data.data()); + } + else if constexpr (detail::has_str_v) { + ss_.append(data.str()); + } + else { + std::stringstream ss; + ss << data; + ss_.append(ss.str()); + } return *this; } diff --git a/src/easylog/tests/test_easylog.cpp b/src/easylog/tests/test_easylog.cpp index 8c14482a8..efc3002f2 100644 --- a/src/easylog/tests/test_easylog.cpp +++ b/src/easylog/tests/test_easylog.cpp @@ -42,6 +42,16 @@ TEST_CASE("test basic") { ELOG_INFO << ptr.get(); ELOG_INFO << 42 << " " << 4.5 << 'a' << Severity::DEBUG; ELOG_INFO << false << ", " << true; + char buf[5] = {"test"}; + std::string_view sv = "test"; + std::string str = "test"; + std::array arr{"test"}; + std::stringstream ss; + ss << "test"; + ELOG_INFO << arr << ", " << ss; + + auto id = std::this_thread::get_id(); + ELOG_INFO << buf << ", " << str << ", " << sv << ", " << id; ELOGV(INFO, "test"); ELOGV(INFO, "it is a long string test %d %s", 2, "ok");