diff --git a/CMakeLists.txt b/CMakeLists.txt index edfff1e..c33a54b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,7 +66,7 @@ endif() if (LIBIPC_BUILD_BENCHMARK) set(BENCHMARK_ENABLE_TESTING OFF) add_subdirectory(3rdparty/benchmark) - # add_subdirectory(benchmark) + add_subdirectory(benchmark) endif() install( diff --git a/benchmark/benchmark_log.cpp b/benchmark/benchmark_log.cpp new file mode 100644 index 0000000..696839b --- /dev/null +++ b/benchmark/benchmark_log.cpp @@ -0,0 +1,17 @@ + +#include "benchmark/benchmark.h" + +#include "libimp/log.h" + +namespace { + +void BM_imp_log_gripper(benchmark::State& state) { + imp::log::gripper log {{}, __func__}; + for (auto _ : state) { + log.info("hello log."); + } +} + +} // namespace + +BENCHMARK(BM_imp_log_gripper); \ No newline at end of file diff --git a/include/libimp/log.h b/include/libimp/log.h index 9c40821..1e2a9a1 100644 --- a/include/libimp/log.h +++ b/include/libimp/log.h @@ -8,8 +8,10 @@ #include #include +#include #include "fmt/format.h" +#include "fmt/chrono.h" #include "libimp/def.h" #include "libimp/detect_plat.h" @@ -88,22 +90,6 @@ public: } }; -class prefix { - std::string px_; - -public: - template - prefix(A &&... args) { - LIBIMP_UNUSED auto unfold = { - 0, ((px_ += ::fmt::format("[{}]", std::forward(args))), 0)... - }; - } - - operator std::string() const noexcept { - return px_; - } -}; - } // namespace detail_log class LIBIMP_EXPORT log_printer { @@ -139,6 +125,32 @@ std::string fmt(Fmt &&ft, A &&... args) { return ::fmt::format(std::forward(ft), std::forward(args)...); } -} // namespace log +class LIBIMP_EXPORT gripper { + log_printer printer_; + char const *func_; + gripper &output(void (log_printer::*out_fn)(std::string &&), char type, std::string &&log_str) { + auto tm = std::chrono::system_clock::now(); + auto ms = std::chrono::time_point_cast(tm).time_since_epoch().count() % 1000; + auto px = fmt("[{}][{:%Y-%m-%d %H:%M:%S}.{:03}][{}]\n", type, tm, ms, func_); + (printer_.*out_fn)(std::move(px += std::move(log_str))); + return *this; + } + +public: + gripper(log_printer printer, char const *func) noexcept + : printer_(printer) + , func_ (func) {} + + template + gripper &info(Fmt &&ft, A &&... args) { + return output(&log_printer::info, 'I', fmt(std::forward(ft), std::forward(args)...)); + } + template + gripper &error(Fmt &&ft, A &&... args) { + return output(&log_printer::error, 'E', fmt(std::forward(ft), std::forward(args)...)); + } +}; + +} // namespace log LIBIMP_NAMESPACE_END_ diff --git a/include/libimp/result.h b/include/libimp/result.h index 81f94c8..f27f0b3 100644 --- a/include/libimp/result.h +++ b/include/libimp/result.h @@ -157,7 +157,6 @@ struct fmt::formatter<::LIBIMP_::result> { constexpr auto parse(format_parse_context& ctx) const { return ctx.end(); } - template auto format(::LIBIMP_::result r, FormatContext &ctx) { return format_to(::LIBIMP_::result::default_traits_t::format(r, diff --git a/src/libimp/log.cpp b/src/libimp/log.cpp index dc37e33..c2ad1a8 100644 --- a/src/libimp/log.cpp +++ b/src/libimp/log.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include "libimp/log.h" @@ -23,11 +23,11 @@ void log_printer::error(std::string && s) { log_std_t log_std; void log_std_t::info(std::string && s) const { - std::cout << std::move(s); + std::fprintf(stdin, "%s", s.c_str()); } void log_std_t::error(std::string && s) const { - std::cerr << std::move(s); + std::fprintf(stderr, "%s", s.c_str()); } LIBIMP_NAMESPACE_END_ diff --git a/test/test_imp_log.cpp b/test/test_imp_log.cpp index cc171f1..db55eaa 100644 --- a/test/test_imp_log.cpp +++ b/test/test_imp_log.cpp @@ -74,17 +74,7 @@ TEST(log, log_printer) { ps.info("hello world\n"); } -#include -#include - -TEST(log, prefix) { - imp::detail_log::prefix prefix; - EXPECT_EQ(std::string{prefix}, ""); - - prefix = {"hello", "world"}; - EXPECT_EQ(std::string{prefix}, "[hello][world]"); - - using namespace std::literals::chrono_literals; - prefix = {"time", imp::log::fmt("{:%H:%M:%S}", 3h + 15min + 30s)}; - EXPECT_EQ(std::string{prefix}, "[time][03:15:30]"); +TEST(log, gripper) { + imp::log::gripper log {imp::log_std, __func__}; + log.info("hello"); } \ No newline at end of file