From dbd0a07f57dd713bd78ce5b99f39f415a7c68a2f Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 30 Mar 2022 14:13:55 +0300 Subject: [PATCH] add boost index type in the logger --- bin/main.cpp | 12 ++++++++++-- meson.build | 3 +++ src/logger/logger.hpp | 34 ++++++++++++++++++++++------------ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bin/main.cpp b/bin/main.cpp index 0148e7f..6eb7c59 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -33,6 +33,11 @@ int minus(int a) return a--; } +struct ForTypeTrace +{ + int a; +}; + int main(int argc, char *argv[]) { {// ex: string::split_str @@ -48,16 +53,19 @@ int main(int argc, char *argv[]) {// ex: container::vector_multiset std::vector v; hack::container::vector_multiset(v, "asdf", "qwer", "zcv"); - for(const auto& c : v) std::cout << c << std::endl; + for(const auto& c : v) hack::log()(c); } {// ex: container::set_multiset std::set s; hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1); - for(const auto& c : s) std::cout << c << std::endl; + for(const auto& c : s) hack::log()(c); } {// ex: logger::log + int a = 10; + ForTypeTrace ftt { 1234 }; + hack::log::type_trace(a, a, ftt); hack::log()(1234, "run in main", 1234); hack::warn(" # ")(1234, "run in main", 1234); hack::error(" - ")(1234, "run in main", 1234); diff --git a/meson.build b/meson.build index 319cea4..81d5e7a 100644 --- a/meson.build +++ b/meson.build @@ -22,8 +22,11 @@ elif compiler.get_id() == 'clang' message('Compiler: LLVM/clang') endif +boost_dep = dependency('boost') + args = [] deps = [] +deps += boost_dep inc = [] subdir('src') diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index 75943bb..b6a5c1a 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -2,6 +2,8 @@ #include +#include "boost/type_index.hpp" + #include "view/color.hpp" #include "concepts/concepts.hpp" #include "iterators/sequence_ostream_iterator.hpp" @@ -10,6 +12,15 @@ namespace hack { + template + void prepare(T t, U u) + { + std::cout << t + << u.file_name() << ":" << view::color::reset + << view::color::italic << view::color::yellow << u.function_name() << "()" << view::color::reset + << view::color::bold << view::color::blue << "[" << u.line() << "]" << view::color::reset << ": "; + } + class log { public: @@ -21,14 +32,19 @@ namespace hack template void operator() (const Args&... args) { - std::cout << make_type_view - << location.file_name() << ":" << view::color::reset - << view::color::italic << view::color::yellow << location.function_name() << "()" << view::color::reset - << view::color::bold << view::color::blue << "[" << location.line() << "]" << view::color::reset << ": "; count = sizeof...(Args); + prepare(make_type_view, location); print(args...); } + template + static void type_trace(const Args&... args) + { + std::cout << make_type_view << ": " << view::color::reset; + count = sizeof...(Args); + print(boost::typeindex::type_id().pretty_name()...); + } + private: std::experimental::source_location location; static int count; @@ -127,10 +143,7 @@ namespace hack template void operator() (const Args&... args) { - std::cout << make_type_view - << location.file_name() << ":" << view::color::reset - << view::color::italic << view::color::yellow << location.function_name() << "()" << view::color::reset - << view::color::bold << view::color::blue << "[" << location.line() << "]" << view::color::reset << ": "; + prepare(make_type_view, location); count = sizeof...(Args); print(args...); } @@ -157,10 +170,7 @@ namespace hack template void operator() (const Args&... args) { - std::cout << make_type_view - << location.file_name() << ":" << view::color::reset - << view::color::italic << view::color::yellow << location.function_name() << "()" << view::color::reset - << view::color::bold << view::color::blue << "[" << location.line() << "]" << view::color::reset << ": "; + prepare(make_type_view, location); count = sizeof...(Args); print(args...); }