diff --git a/include/fmtlog/fmtlog-inl.h b/include/fmtlog/fmtlog-inl.h index 16eed0d..c247f4c 100644 --- a/include/fmtlog/fmtlog-inl.h +++ b/include/fmtlog/fmtlog-inl.h @@ -103,10 +103,10 @@ class fmtlogDetailT setHeaderPattern("{HMSf} {s:<16} {l}[{t:<6}] "); logInfos.reserve(32); bgLogInfos.reserve(128); - bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::DBG, fmt::string_view()); - bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::INF, fmt::string_view()); - bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::WRN, fmt::string_view()); - bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::ERR, fmt::string_view()); + bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::DBG, std::string{}); + bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::INF, std::string{}); + bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::WRN, std::string{}); + bgLogInfos.emplace_back(nullptr, nullptr, fmtlog::ERR, std::string{}); threadBuffers.reserve(8); bgThreadBuffers.reserve(8); memset(membuf.data(), 0, membuf.capacity()); @@ -118,8 +118,8 @@ class fmtlogDetailT closeLogFile(); } - void setHeaderPattern(const char* pattern) { - if (shouldDeallocateHeader) delete[] headerPattern.data(); + void setHeaderPattern(std::string pattern) { + // if (shouldDeallocateHeader) delete[] headerPattern.data(); using namespace fmt::literals; for (int i = 0; i < parttenArgSize; i++) { reorderIdx[i] = parttenArgSize - 1; @@ -130,7 +130,7 @@ class fmtlogDetailT "l"_a = fmtlog::LogLevel(), "s"_a = "fmtlog.cc:123", "g"_a = "/home/raomeng/fmtlog/fmtlog.cc:123", "Ymd"_a = "", "HMS"_a = "", "HMSe"_a = "", "HMSf"_a = "", "HMSF"_a = "", "YmdHMS"_a = "", "YmdHMSe"_a = "", "YmdHMSf"_a = "", "YmdHMSF"_a = ""); - shouldDeallocateHeader = headerPattern.data() != pattern; + // shouldDeallocateHeader = headerPattern.data() != pattern; setArg<0>(fmt::string_view(weekdayName.s, 3)); setArg<1>(fmt::string_view(monthName.s, 3)); @@ -177,7 +177,7 @@ class fmtlogDetailT struct StaticLogInfo { // Constructor - constexpr StaticLogInfo(fmtlog::FormatToFn fn, const char* loc, fmtlog::LogLevel level, fmt::string_view fmtString) + StaticLogInfo(fmtlog::FormatToFn fn, const char* loc, fmtlog::LogLevel level, std::string fmtString) : formatToFn(fn) , formatString(fmtString) , location(loc) @@ -207,7 +207,7 @@ class fmtlogDetailT inline fmt::string_view getLocation() { return fmt::string_view(location, endPos); } fmtlog::FormatToFn formatToFn; - fmt::string_view formatString; + std::string formatString; const char* location; uint8_t basePos; uint8_t endPos; @@ -217,7 +217,7 @@ class fmtlogDetailT static thread_local ThreadBufferDestroyer sbc; int64_t midnightNs; - fmt::string_view headerPattern; + std::string headerPattern; bool shouldDeallocateHeader = false; FILE* outputFp = nullptr; bool manageFp = false; @@ -494,7 +494,7 @@ fmtlogDetailT<> fmtlogDetailWrapper<_>::impl; template void fmtlogT<_>::registerLogInfo(uint32_t& logId, FormatToFn fn, const char* location, - LogLevel level, fmt::string_view fmtString) noexcept { + LogLevel level, std::string fmtString) noexcept { auto& d = fmtlogDetailWrapper<>::impl; std::lock_guard lock(d.logInfoMutex); if (logId) return; diff --git a/include/fmtlog/fmtlog.h b/include/fmtlog/fmtlog.h index 19e7bf5..6247188 100644 --- a/include/fmtlog/fmtlog.h +++ b/include/fmtlog/fmtlog.h @@ -379,7 +379,7 @@ class fmtlogT int& argIdx, std::vector>& args); static void registerLogInfo(uint32_t& logId, FormatToFn fn, const char* location, LogLevel level, - fmt::string_view fmtString) noexcept; + std::string fmtString) noexcept; static void vformat_to(MemoryBuffer& out, fmt::string_view fmt, fmt::format_args args); @@ -610,11 +610,11 @@ class fmtlogT } template - static std::string unNameFormat(fmt::string_view in, uint32_t* reorderIdx, + static std::string unNameFormat(std::string in, uint32_t* reorderIdx, const Args&... args) { constexpr size_t num_named_args = fmt::detail::count()...>(); if constexpr (num_named_args == 0) { - return std::string{in.begin(),in.end()}; + return in; } const char* begin = in.data(); const char* p = begin; @@ -679,7 +679,8 @@ class fmtlogT fmt::format_string>::type...> format, Args&&... args) noexcept { if (!logId) { - auto unnamed_format = unNameFormat(fmt::string_view(format), nullptr, args...); + fmt::string_view format_str_v = format.get(); + auto unnamed_format = unNameFormat(std::string{format_str_v.begin(),format_str_v.end()}, nullptr, args...); registerLogInfo(logId, formatTo, location, level, unnamed_format); } constexpr size_t num_cstring = fmt::detail::count()...>(); diff --git a/test/src/enc_dec_test.cc b/test/src/enc_dec_test.cc index 79ed2ac..e7a752f 100644 --- a/test/src/enc_dec_test.cc +++ b/test/src/enc_dec_test.cc @@ -56,7 +56,7 @@ void test(const S& format, Args&&... args) { string ans = fmt::format(fmt::runtime(sv), std::forward(args)...); assert(ans.size() == formatted_size); - auto unnamed_format = fmtlog::unNameFormat(sv, nullptr, args...); + auto unnamed_format = fmtlog::unNameFormat(format, nullptr, args...); fmt::print("unnamed_format: {}\n", unnamed_format); size_t cstringSizes[1000]; char buf[1024];