diff --git a/example.cpp b/example.cpp index b561fc6..c124bcf 100644 --- a/example.cpp +++ b/example.cpp @@ -1,36 +1,7 @@ -// -*- compile-command: "g++ -Iinclude -D_GLIBCXX_DEBUG -fsanitize=address,undefined -g3 -ggdb3 -std=c++17 example.cpp -o example && SOI_COLOR=1 SOI_EOFCHECK=1 ./example <<< ''" -*- +// -*- compile-command: "g++ -Iinclude -D_GLIBCXX_DEBUG -fsanitize=address,undefined -g3 -ggdb3 -std=c++17 example.cpp -o example && SOI_H_COLOR=1 SOI_H_EOFCHECK=1 ./example <<< ''" -*- #include -inline std::string methodName(const std::string& prettyFunction) { - size_t colons = prettyFunction.find("::"); - size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1; - size_t end = prettyFunction.rfind("(") - begin; - return prettyFunction.substr(begin,end) + "()"; -} - -struct A{ - void operator()() const { - dbg(methodName(__PRETTY_FUNCTION__)); - dbg("here"); - } - void bar(int a, int b, string c) const { - dbg("imbar"); - } - void foo(int a=3, int b=5, string c="hi") const { - dbg(methodName(__PRETTY_FUNCTION__)); - dbg("bar"); - } -}; - signed main() { - dbg(methodName(__PRETTY_FUNCTION__)); - A a; - a(); - a.foo(); - a.bar(3,4,"hi"); - - int l=3, r=5; - dbg(tuple{l, r}); dbg(); dbg("hi"); string s="world"; @@ -46,6 +17,6 @@ signed main() { dbg(vector>>{{3,{"hi"}},{4,{"hello", "world"}}}); dbg(set{3,1,4}); dbg(map{{3,"three"},{1,"one"}}); - dbg(tuple{unordered_map{{3,"three"},{1,"one"}}}); + dbg(unordered_map{{3,"three"},{1,"one"}}); dbg(unordered_set{3,1,4}); } diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index 390a1a6..d580601 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -74,7 +74,7 @@ std::string replace_all(std::string str, const std::string &from, return str; } -inline std::string sanitize_type(std::string s) { +std::string sanitize_type(std::string s) { auto f = [&](const char *a, const char *b) { s = replace_all(std::move(s), a, b); }; @@ -92,14 +92,6 @@ template std::string sanitized_type_name() { return sanitize_type(std::string(t.data, t.size)); } -inline std::string extract_method_name(std::string const& pretty_function) { - size_t colons = pretty_function.find("::"); - size_t begin = pretty_function.substr(0,colons).rfind(" ") + 1; - size_t end = pretty_function.rfind("(") - begin; - return pretty_function.substr(begin,end); -} - - // ---------------------------------------------------------------------------- // colorized output @@ -165,16 +157,15 @@ void dbg_init() { dbg_init(are_colors_enabled()); } // printer template -T &&dbg_print(T &&value, std::string const& type, char const *file, int line, - std::string const& function_name, char const *expression) { +T &&dbg_print(T &&value, std::string const &type, char const *file, int line, + char const *function_name, char const *expression) { const T &ref = value; std::stringstream - value_buffer; // avoid nesting of dbg macros within print functions + value_buffer; // avoid nesting of dbg macros within print functinos soi::print(value_buffer, ref); std::cerr << ANSI_DEBUG << "[" << file << ":" << line << " (" << function_name - << ")] " << ANSI_RESET << ANSI_EXPRESSION - << replace_all(c, "int64_t", "int") + << ")] " << ANSI_RESET << ANSI_EXPRESSION << expression << ANSI_RESET << " = " << ANSI_VALUE << value_buffer.rdbuf() << ANSI_RESET << " (" << ANSI_TYPE << type << ANSI_RESET << ")" << '\n'; @@ -184,7 +175,7 @@ T &&dbg_print(T &&value, std::string const& type, char const *file, int line, template auto dbg_print(const char (&msg)[N], std::string const &, char const *file, - int line, std::string const& function_name, char const *expression) + int line, char const *function_name, char const *expression) -> decltype(msg) { std::cerr << ANSI_DEBUG << "[" << file << ":" << line << " (" << function_name << ")] " << ANSI_RESET << ANSI_MESSAGE << msg << ANSI_RESET << '\n'; @@ -213,19 +204,29 @@ template T &&identity(T &&t) { return std::forward(t); } _15 #define SOI_IS_NONEMPTY(...) \ SOI_ARG16(1, ##__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) -#else - -#define SOI_IS_NONEMPTY(...) __VA_OPT__(1) -#define SOI_DBG_IMPL_ SOI_DBG_IMPL_0 -#endif #define SOI_DBG_IMPL_0() \ soi::detail::dbg_print_status(__FILE__, __LINE__, __func__) + #define SOI_DBG_IMPL_1(...) \ soi::detail::dbg_print( \ (__VA_ARGS__), \ soi::detail::sanitized_type_name(), __FILE__, \ - __LINE__, soi::detail::extract_method_name(__PRETTY_FUNCTION__), #__VA_ARGS__) + __LINE__, __func__, #__VA_ARGS__) + +#else + +#define SOI_IS_NONEMPTY(...) __VA_OPT__(HAS) + +#define SOI_DBG_IMPL_() \ + soi::detail::dbg_print_status(__FILE__, __LINE__, __func__) +#define SOI_DBG_IMPL_HAS(...) \ + soi::detail::dbg_print( \ + (__VA_ARGS__), \ + soi::detail::sanitized_type_name(), __FILE__, \ + __LINE__, __func__, #__VA_ARGS__) + +#endif #define SOI_CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__) #define SOI_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__