add: [log] a simple implementation to be optimized

This commit is contained in:
mutouyun 2022-05-23 00:19:07 +08:00
parent 0b504f7b48
commit e1de7f9638
6 changed files with 53 additions and 35 deletions

View File

@ -66,7 +66,7 @@ endif()
if (LIBIPC_BUILD_BENCHMARK) if (LIBIPC_BUILD_BENCHMARK)
set(BENCHMARK_ENABLE_TESTING OFF) set(BENCHMARK_ENABLE_TESTING OFF)
add_subdirectory(3rdparty/benchmark) add_subdirectory(3rdparty/benchmark)
# add_subdirectory(benchmark) add_subdirectory(benchmark)
endif() endif()
install( install(

View File

@ -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);

View File

@ -8,8 +8,10 @@
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <chrono>
#include "fmt/format.h" #include "fmt/format.h"
#include "fmt/chrono.h"
#include "libimp/def.h" #include "libimp/def.h"
#include "libimp/detect_plat.h" #include "libimp/detect_plat.h"
@ -88,22 +90,6 @@ public:
} }
}; };
class prefix {
std::string px_;
public:
template <typename... A>
prefix(A &&... args) {
LIBIMP_UNUSED auto unfold = {
0, ((px_ += ::fmt::format("[{}]", std::forward<A>(args))), 0)...
};
}
operator std::string() const noexcept {
return px_;
}
};
} // namespace detail_log } // namespace detail_log
class LIBIMP_EXPORT log_printer { class LIBIMP_EXPORT log_printer {
@ -139,6 +125,32 @@ std::string fmt(Fmt &&ft, A &&... args) {
return ::fmt::format(std::forward<Fmt>(ft), std::forward<A>(args)...); return ::fmt::format(std::forward<Fmt>(ft), std::forward<A>(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<std::chrono::milliseconds>(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 <typename Fmt, typename... A>
gripper &info(Fmt &&ft, A &&... args) {
return output(&log_printer::info, 'I', fmt(std::forward<Fmt>(ft), std::forward<A>(args)...));
}
template <typename Fmt, typename... A>
gripper &error(Fmt &&ft, A &&... args) {
return output(&log_printer::error, 'E', fmt(std::forward<Fmt>(ft), std::forward<A>(args)...));
}
};
} // namespace log
LIBIMP_NAMESPACE_END_ LIBIMP_NAMESPACE_END_

View File

@ -157,7 +157,6 @@ struct fmt::formatter<::LIBIMP_::result<T, D>> {
constexpr auto parse(format_parse_context& ctx) const { constexpr auto parse(format_parse_context& ctx) const {
return ctx.end(); return ctx.end();
} }
template <typename FormatContext> template <typename FormatContext>
auto format(::LIBIMP_::result<T, D> r, FormatContext &ctx) { auto format(::LIBIMP_::result<T, D> r, FormatContext &ctx) {
return format_to(::LIBIMP_::result<T, D>::default_traits_t::format(r, return format_to(::LIBIMP_::result<T, D>::default_traits_t::format(r,

View File

@ -1,6 +1,6 @@
#include <utility> #include <utility>
#include <iostream> #include <cstdio>
#include "libimp/log.h" #include "libimp/log.h"
@ -23,11 +23,11 @@ void log_printer::error(std::string && s) {
log_std_t log_std; log_std_t log_std;
void log_std_t::info(std::string && s) const { 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 { void log_std_t::error(std::string && s) const {
std::cerr << std::move(s); std::fprintf(stderr, "%s", s.c_str());
} }
LIBIMP_NAMESPACE_END_ LIBIMP_NAMESPACE_END_

View File

@ -74,17 +74,7 @@ TEST(log, log_printer) {
ps.info("hello world\n"); ps.info("hello world\n");
} }
#include <chrono> TEST(log, gripper) {
#include <fmt/chrono.h> imp::log::gripper log {imp::log_std, __func__};
log.info("hello");
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]");
} }