| ... | ... | @@ -1,711 +0,0 @@ |
| 1 | | /***************************************************************************** |
| 2 | | |
| 3 | | dbg(...) macro |
| 4 | | |
| 5 | | License (MIT): |
| 6 | | |
| 7 | | Copyright (c) 2019 David Peter <mail@david-peter.de> |
| 8 | | |
| 9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | | of this software and associated documentation files (the "Software"), to |
| 11 | | deal in the Software without restriction, including without limitation the |
| 12 | | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | | sell copies of the Software, and to permit persons to whom the Software is |
| 14 | | furnished to do so, subject to the following conditions: |
| 15 | | |
| 16 | | The above copyright notice and this permission notice shall be included in |
| 17 | | all copies or substantial portions of the Software. |
| 18 | | |
| 19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | | SOFTWARE. |
| 26 | | |
| 27 | | *****************************************************************************/ |
| 28 | | |
| 29 | | #ifndef DBG_MACRO_DBG_H |
| 30 | | #define DBG_MACRO_DBG_H |
| 31 | | |
| 32 | | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 33 | | #define DBG_MACRO_UNIX |
| 34 | | #elif defined(_MSC_VER) |
| 35 | | #define DBG_MACRO_WINDOWS |
| 36 | | #endif |
| 37 | | |
| 38 | | #ifndef DBG_MACRO_NO_WARNING |
| 39 | | #pragma message("WARNING: the 'dbg.h' header is included in your code base") |
| 40 | | #endif // DBG_MACRO_NO_WARNING |
| 41 | | |
| 42 | | #include <algorithm> |
| 43 | | #include <chrono> |
| 44 | | #include <ctime> |
| 45 | | #include <iomanip> |
| 46 | | #include <ios> |
| 47 | | #include <iostream> |
| 48 | | #include <memory> |
| 49 | | #include <sstream> |
| 50 | | #include <string> |
| 51 | | #include <tuple> |
| 52 | | #include <type_traits> |
| 53 | | #include <vector> |
| 54 | | |
| 55 | | #ifdef DBG_MACRO_UNIX |
| 56 | | #include <unistd.h> |
| 57 | | #endif |
| 58 | | |
| 59 | | #if __cplusplus >= 201703L || defined(_MSC_VER) |
| 60 | | #define DBG_MACRO_CXX_STANDARD 17 |
| 61 | | #elif __cplusplus >= 201402L |
| 62 | | #define DBG_MACRO_CXX_STANDARD 14 |
| 63 | | #else |
| 64 | | #define DBG_MACRO_CXX_STANDARD 11 |
| 65 | | #endif |
| 66 | | |
| 67 | | #if DBG_MACRO_CXX_STANDARD >= 17 |
| 68 | | #include <optional> |
| 69 | | #include <variant> |
| 70 | | #endif |
| 71 | | |
| 72 | | namespace dbg { |
| 73 | | |
| 74 | | #ifdef DBG_MACRO_UNIX |
| 75 | | inline bool isColorizedOutputEnabled() { |
| 76 | | return isatty(fileno(stderr)); |
| 77 | | } |
| 78 | | #else |
| 79 | | inline bool isColorizedOutputEnabled() { |
| 80 | | return true; |
| 81 | | } |
| 82 | | #endif |
| 83 | | |
| 84 | | struct time {}; |
| 85 | | |
| 86 | | namespace pretty_function { |
| 87 | | |
| 88 | | // Compiler-agnostic version of __PRETTY_FUNCTION__ and constants to |
| 89 | | // extract the template argument in `type_name_impl` |
| 90 | | |
| 91 | | #if defined(__clang__) |
| 92 | | #define DBG_MACRO_PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 93 | | static constexpr size_t PREFIX_LENGTH = |
| 94 | | sizeof("const char *dbg::type_name_impl() [T = ") - 1; |
| 95 | | static constexpr size_t SUFFIX_LENGTH = sizeof("]") - 1; |
| 96 | | #elif defined(__GNUC__) && !defined(__clang__) |
| 97 | | #define DBG_MACRO_PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 98 | | static constexpr size_t PREFIX_LENGTH = |
| 99 | | sizeof("const char* dbg::type_name_impl() [with T = ") - 1; |
| 100 | | static constexpr size_t SUFFIX_LENGTH = sizeof("]") - 1; |
| 101 | | #elif defined(_MSC_VER) |
| 102 | | #define DBG_MACRO_PRETTY_FUNCTION __FUNCSIG__ |
| 103 | | static constexpr size_t PREFIX_LENGTH = |
| 104 | | sizeof("const char *__cdecl dbg::type_name_impl<") - 1; |
| 105 | | static constexpr size_t SUFFIX_LENGTH = sizeof(">(void)") - 1; |
| 106 | | #else |
| 107 | | #error "This compiler is currently not supported by dbg_macro." |
| 108 | | #endif |
| 109 | | |
| 110 | | } // namespace pretty_function |
| 111 | | |
| 112 | | // Formatting helpers |
| 113 | | |
| 114 | | template <typename T> |
| 115 | | struct print_formatted { |
| 116 | | static_assert(std::is_integral<T>::value, |
| 117 | | "Only integral types are supported."); |
| 118 | | |
| 119 | | print_formatted(T value, int numeric_base) |
| 120 | | : inner(value), base(numeric_base) {} |
| 121 | | |
| 122 | | operator T() const { return inner; } |
| 123 | | |
| 124 | | const char* prefix() const { |
| 125 | | switch (base) { |
| 126 | | case 8: |
| 127 | | return "0o"; |
| 128 | | case 16: |
| 129 | | return "0x"; |
| 130 | | case 2: |
| 131 | | return "0b"; |
| 132 | | default: |
| 133 | | return ""; |
| 134 | | } |
| 135 | | } |
| 136 | | |
| 137 | | T inner; |
| 138 | | int base; |
| 139 | | }; |
| 140 | | |
| 141 | | template <typename T> |
| 142 | | print_formatted<T> hex(T value) { |
| 143 | | return print_formatted<T>{value, 16}; |
| 144 | | } |
| 145 | | |
| 146 | | template <typename T> |
| 147 | | print_formatted<T> oct(T value) { |
| 148 | | return print_formatted<T>{value, 8}; |
| 149 | | } |
| 150 | | |
| 151 | | template <typename T> |
| 152 | | print_formatted<T> bin(T value) { |
| 153 | | return print_formatted<T>{value, 2}; |
| 154 | | } |
| 155 | | |
| 156 | | // Implementation of 'type_name<T>()' |
| 157 | | |
| 158 | | template <typename T> |
| 159 | | const char* type_name_impl() { |
| 160 | | return DBG_MACRO_PRETTY_FUNCTION; |
| 161 | | } |
| 162 | | |
| 163 | | template <typename T> |
| 164 | | struct type_tag {}; |
| 165 | | |
| 166 | | template <int&... ExplicitArgumentBarrier, typename T> |
| 167 | | std::string get_type_name(type_tag<T>) { |
| 168 | | namespace pf = pretty_function; |
| 169 | | |
| 170 | | std::string type = type_name_impl<T>(); |
| 171 | | return type.substr(pf::PREFIX_LENGTH, |
| 172 | | type.size() - pf::PREFIX_LENGTH - pf::SUFFIX_LENGTH); |
| 173 | | } |
| 174 | | |
| 175 | | template <typename T> |
| 176 | | std::string type_name() { |
| 177 | | if (std::is_volatile<T>::value) { |
| 178 | | if (std::is_pointer<T>::value) { |
| 179 | | return type_name<typename std::remove_volatile<T>::type>() + " volatile"; |
| 180 | | } else { |
| 181 | | return "volatile " + type_name<typename std::remove_volatile<T>::type>(); |
| 182 | | } |
| 183 | | } |
| 184 | | if (std::is_const<T>::value) { |
| 185 | | if (std::is_pointer<T>::value) { |
| 186 | | return type_name<typename std::remove_const<T>::type>() + " const"; |
| 187 | | } else { |
| 188 | | return "const " + type_name<typename std::remove_const<T>::type>(); |
| 189 | | } |
| 190 | | } |
| 191 | | if (std::is_pointer<T>::value) { |
| 192 | | return type_name<typename std::remove_pointer<T>::type>() + "*"; |
| 193 | | } |
| 194 | | if (std::is_lvalue_reference<T>::value) { |
| 195 | | return type_name<typename std::remove_reference<T>::type>() + "&"; |
| 196 | | } |
| 197 | | if (std::is_rvalue_reference<T>::value) { |
| 198 | | return type_name<typename std::remove_reference<T>::type>() + "&&"; |
| 199 | | } |
| 200 | | return get_type_name(type_tag<T>{}); |
| 201 | | } |
| 202 | | |
| 203 | | inline std::string get_type_name(type_tag<short>) { |
| 204 | | return "short"; |
| 205 | | } |
| 206 | | |
| 207 | | inline std::string get_type_name(type_tag<unsigned short>) { |
| 208 | | return "unsigned short"; |
| 209 | | } |
| 210 | | |
| 211 | | inline std::string get_type_name(type_tag<long>) { |
| 212 | | return "long"; |
| 213 | | } |
| 214 | | |
| 215 | | inline std::string get_type_name(type_tag<unsigned long>) { |
| 216 | | return "unsigned long"; |
| 217 | | } |
| 218 | | |
| 219 | | inline std::string get_type_name(type_tag<std::string>) { |
| 220 | | return "std::string"; |
| 221 | | } |
| 222 | | |
| 223 | | template <typename T> |
| 224 | | std::string get_type_name(type_tag<std::vector<T, std::allocator<T>>>) { |
| 225 | | return "std::vector<" + type_name<T>() + ">"; |
| 226 | | } |
| 227 | | |
| 228 | | template <typename T1, typename T2> |
| 229 | | std::string get_type_name(type_tag<std::pair<T1, T2>>) { |
| 230 | | return "std::pair<" + type_name<T1>() + ", " + type_name<T2>() + ">"; |
| 231 | | } |
| 232 | | |
| 233 | | template <typename... T> |
| 234 | | std::string type_list_to_string() { |
| 235 | | std::string result; |
| 236 | | auto unused = {(result += type_name<T>() + ", ", 0)..., 0}; |
| 237 | | static_cast<void>(unused); |
| 238 | | |
| 239 | | if (sizeof...(T) > 0) { |
| 240 | | result.pop_back(); |
| 241 | | result.pop_back(); |
| 242 | | } |
| 243 | | return result; |
| 244 | | } |
| 245 | | |
| 246 | | template <typename... T> |
| 247 | | std::string get_type_name(type_tag<std::tuple<T...>>) { |
| 248 | | return "std::tuple<" + type_list_to_string<T...>() + ">"; |
| 249 | | } |
| 250 | | |
| 251 | | template <typename T> |
| 252 | | inline std::string get_type_name(type_tag<print_formatted<T>>) { |
| 253 | | return type_name<T>(); |
| 254 | | } |
| 255 | | |
| 256 | | // Implementation of 'is_detected' to specialize for container-like types |
| 257 | | |
| 258 | | namespace detail_detector { |
| 259 | | |
| 260 | | struct nonesuch { |
| 261 | | nonesuch() = delete; |
| 262 | | ~nonesuch() = delete; |
| 263 | | nonesuch(nonesuch const&) = delete; |
| 264 | | void operator=(nonesuch const&) = delete; |
| 265 | | }; |
| 266 | | |
| 267 | | template <typename...> |
| 268 | | using void_t = void; |
| 269 | | |
| 270 | | template <class Default, |
| 271 | | class AlwaysVoid, |
| 272 | | template <class...> |
| 273 | | class Op, |
| 274 | | class... Args> |
| 275 | | struct detector { |
| 276 | | using value_t = std::false_type; |
| 277 | | using type = Default; |
| 278 | | }; |
| 279 | | |
| 280 | | template <class Default, template <class...> class Op, class... Args> |
| 281 | | struct detector<Default, void_t<Op<Args...>>, Op, Args...> { |
| 282 | | using value_t = std::true_type; |
| 283 | | using type = Op<Args...>; |
| 284 | | }; |
| 285 | | |
| 286 | | } // namespace detail_detector |
| 287 | | |
| 288 | | template <template <class...> class Op, class... Args> |
| 289 | | using is_detected = typename detail_detector:: |
| 290 | | detector<detail_detector::nonesuch, void, Op, Args...>::value_t; |
| 291 | | |
| 292 | | namespace detail { |
| 293 | | |
| 294 | | namespace { |
| 295 | | using std::begin; |
| 296 | | using std::end; |
| 297 | | #if DBG_MACRO_CXX_STANDARD < 17 |
| 298 | | template <typename T> |
| 299 | | constexpr auto size(const T& c) -> decltype(c.size()) { |
| 300 | | return c.size(); |
| 301 | | } |
| 302 | | template <typename T, std::size_t N> |
| 303 | | constexpr std::size_t size(const T (&)[N]) { |
| 304 | | return N; |
| 305 | | } |
| 306 | | #else |
| 307 | | using std::size; |
| 308 | | #endif |
| 309 | | } // namespace |
| 310 | | |
| 311 | | template <typename T> |
| 312 | | using detect_begin_t = decltype(detail::begin(std::declval<T>())); |
| 313 | | |
| 314 | | template <typename T> |
| 315 | | using detect_end_t = decltype(detail::end(std::declval<T>())); |
| 316 | | |
| 317 | | template <typename T> |
| 318 | | using detect_size_t = decltype(detail::size(std::declval<T>())); |
| 319 | | |
| 320 | | template <typename T> |
| 321 | | struct is_container { |
| 322 | | static constexpr bool value = |
| 323 | | is_detected<detect_begin_t, T>::value && |
| 324 | | is_detected<detect_end_t, T>::value && |
| 325 | | is_detected<detect_size_t, T>::value && |
| 326 | | !std::is_same<std::string, |
| 327 | | typename std::remove_cv< |
| 328 | | typename std::remove_reference<T>::type>::type>::value; |
| 329 | | }; |
| 330 | | |
| 331 | | template <typename T> |
| 332 | | using ostream_operator_t = |
| 333 | | decltype(std::declval<std::ostream&>() << std::declval<T>()); |
| 334 | | |
| 335 | | template <typename T> |
| 336 | | struct has_ostream_operator : is_detected<ostream_operator_t, T> {}; |
| 337 | | |
| 338 | | } // namespace detail |
| 339 | | |
| 340 | | // Helper to dbg(…)-print types |
| 341 | | template <typename T> |
| 342 | | struct print_type {}; |
| 343 | | |
| 344 | | template <typename T> |
| 345 | | print_type<T> type() { |
| 346 | | return print_type<T>{}; |
| 347 | | } |
| 348 | | |
| 349 | | // Specializations of "pretty_print" |
| 350 | | |
| 351 | | template <typename T> |
| 352 | | inline void pretty_print(std::ostream& stream, const T& value, std::true_type) { |
| 353 | | stream << value; |
| 354 | | } |
| 355 | | |
| 356 | | template <typename T> |
| 357 | | inline void pretty_print(std::ostream&, const T&, std::false_type) { |
| 358 | | static_assert(detail::has_ostream_operator<const T&>::value, |
| 359 | | "Type does not support the << ostream operator"); |
| 360 | | } |
| 361 | | |
| 362 | | template <typename T> |
| 363 | | inline typename std::enable_if<!detail::is_container<const T&>::value && |
| 364 | | !std::is_enum<T>::value, |
| 365 | | bool>::type |
| 366 | | pretty_print(std::ostream& stream, const T& value) { |
| 367 | | pretty_print(stream, value, |
| 368 | | typename detail::has_ostream_operator<const T&>::type{}); |
| 369 | | return true; |
| 370 | | } |
| 371 | | |
| 372 | | inline bool pretty_print(std::ostream& stream, const bool& value) { |
| 373 | | stream << std::boolalpha << value; |
| 374 | | return true; |
| 375 | | } |
| 376 | | |
| 377 | | inline bool pretty_print(std::ostream& stream, const char& value) { |
| 378 | | const bool printable = value >= 0x20 && value <= 0x7E; |
| 379 | | |
| 380 | | if (printable) { |
| 381 | | stream << "'" << value << "'"; |
| 382 | | } else { |
| 383 | | stream << "'\\x" << std::setw(2) << std::setfill('0') << std::hex |
| 384 | | << std::uppercase << (0xFF & value) << "'"; |
| 385 | | } |
| 386 | | return true; |
| 387 | | } |
| 388 | | |
| 389 | | template <typename P> |
| 390 | | inline bool pretty_print(std::ostream& stream, P* const& value) { |
| 391 | | if (value == nullptr) { |
| 392 | | stream << "nullptr"; |
| 393 | | } else { |
| 394 | | stream << value; |
| 395 | | } |
| 396 | | return true; |
| 397 | | } |
| 398 | | |
| 399 | | template <typename T, typename Deleter> |
| 400 | | inline bool pretty_print(std::ostream& stream, |
| 401 | | std::unique_ptr<T, Deleter>& value) { |
| 402 | | pretty_print(stream, value.get()); |
| 403 | | return true; |
| 404 | | } |
| 405 | | |
| 406 | | template <typename T> |
| 407 | | inline bool pretty_print(std::ostream& stream, std::shared_ptr<T>& value) { |
| 408 | | pretty_print(stream, value.get()); |
| 409 | | stream << " (use_count = " << value.use_count() << ")"; |
| 410 | | |
| 411 | | return true; |
| 412 | | } |
| 413 | | |
| 414 | | template <size_t N> |
| 415 | | inline bool pretty_print(std::ostream& stream, const char (&value)[N]) { |
| 416 | | stream << value; |
| 417 | | return false; |
| 418 | | } |
| 419 | | |
| 420 | | template <> |
| 421 | | inline bool pretty_print(std::ostream& stream, const char* const& value) { |
| 422 | | stream << '"' << value << '"'; |
| 423 | | return true; |
| 424 | | } |
| 425 | | |
| 426 | | template <size_t Idx> |
| 427 | | struct pretty_print_tuple { |
| 428 | | template <typename... Ts> |
| 429 | | static void print(std::ostream& stream, const std::tuple<Ts...>& tuple) { |
| 430 | | pretty_print_tuple<Idx - 1>::print(stream, tuple); |
| 431 | | stream << ", "; |
| 432 | | pretty_print(stream, std::get<Idx>(tuple)); |
| 433 | | } |
| 434 | | }; |
| 435 | | |
| 436 | | template <> |
| 437 | | struct pretty_print_tuple<0> { |
| 438 | | template <typename... Ts> |
| 439 | | static void print(std::ostream& stream, const std::tuple<Ts...>& tuple) { |
| 440 | | pretty_print(stream, std::get<0>(tuple)); |
| 441 | | } |
| 442 | | }; |
| 443 | | |
| 444 | | template <typename... Ts> |
| 445 | | inline bool pretty_print(std::ostream& stream, const std::tuple<Ts...>& value) { |
| 446 | | stream << "{"; |
| 447 | | pretty_print_tuple<sizeof...(Ts) - 1>::print(stream, value); |
| 448 | | stream << "}"; |
| 449 | | |
| 450 | | return true; |
| 451 | | } |
| 452 | | |
| 453 | | template <> |
| 454 | | inline bool pretty_print(std::ostream& stream, const std::tuple<>&) { |
| 455 | | stream << "{}"; |
| 456 | | |
| 457 | | return true; |
| 458 | | } |
| 459 | | |
| 460 | | template <> |
| 461 | | inline bool pretty_print(std::ostream& stream, const time&) { |
| 462 | | using namespace std::chrono; |
| 463 | | |
| 464 | | const auto now = system_clock::now(); |
| 465 | | const auto us = |
| 466 | | duration_cast<microseconds>(now.time_since_epoch()).count() % 1000000; |
| 467 | | const auto hms = system_clock::to_time_t(now); |
| 468 | | const std::tm* tm = std::localtime(&hms); |
| 469 | | stream << "current time = " << std::put_time(tm, "%H:%M:%S") << '.' |
| 470 | | << std::setw(6) << std::setfill('0') << us; |
| 471 | | |
| 472 | | return false; |
| 473 | | } |
| 474 | | |
| 475 | | // Converts decimal integer to binary string |
| 476 | | template <typename T> |
| 477 | | std::string decimalToBinary(T n) { |
| 478 | | const size_t length = 8 * sizeof(T); |
| 479 | | std::string toRet; |
| 480 | | toRet.resize(length); |
| 481 | | |
| 482 | | for (size_t i = 0; i < length; ++i) { |
| 483 | | const auto bit_at_index_i = (n >> i) & 1; |
| 484 | | toRet[length - 1 - i] = bit_at_index_i + '0'; |
| 485 | | } |
| 486 | | |
| 487 | | return toRet; |
| 488 | | } |
| 489 | | |
| 490 | | template <typename T> |
| 491 | | inline bool pretty_print(std::ostream& stream, |
| 492 | | const print_formatted<T>& value) { |
| 493 | | if (value.inner < 0) { |
| 494 | | stream << "-"; |
| 495 | | } |
| 496 | | stream << value.prefix(); |
| 497 | | |
| 498 | | // Print using setbase |
| 499 | | if (value.base != 2) { |
| 500 | | stream << std::setw(sizeof(T)) << std::setfill('0') |
| 501 | | << std::setbase(value.base) << std::uppercase; |
| 502 | | |
| 503 | | if (value.inner >= 0) { |
| 504 | | // The '+' sign makes sure that a uint_8 is printed as a number |
| 505 | | stream << +value.inner; |
| 506 | | } else { |
| 507 | | using unsigned_type = typename std::make_unsigned<T>::type; |
| 508 | | stream << +(static_cast<unsigned_type>(-(value.inner + 1)) + 1); |
| 509 | | } |
| 510 | | } else { |
| 511 | | // Print for binary |
| 512 | | if (value.inner >= 0) { |
| 513 | | stream << decimalToBinary(value.inner); |
| 514 | | } else { |
| 515 | | using unsigned_type = typename std::make_unsigned<T>::type; |
| 516 | | stream << decimalToBinary<unsigned_type>( |
| 517 | | static_cast<unsigned_type>(-(value.inner + 1)) + 1); |
| 518 | | } |
| 519 | | } |
| 520 | | |
| 521 | | return true; |
| 522 | | } |
| 523 | | |
| 524 | | template <typename T> |
| 525 | | inline bool pretty_print(std::ostream& stream, const print_type<T>&) { |
| 526 | | stream << type_name<T>(); |
| 527 | | |
| 528 | | stream << " [sizeof: " << sizeof(T) << " byte, "; |
| 529 | | |
| 530 | | stream << "trivial: "; |
| 531 | | if (std::is_trivial<T>::value) { |
| 532 | | stream << "yes"; |
| 533 | | } else { |
| 534 | | stream << "no"; |
| 535 | | } |
| 536 | | |
| 537 | | stream << ", standard layout: "; |
| 538 | | if (std::is_standard_layout<T>::value) { |
| 539 | | stream << "yes"; |
| 540 | | } else { |
| 541 | | stream << "no"; |
| 542 | | } |
| 543 | | stream << "]"; |
| 544 | | |
| 545 | | return false; |
| 546 | | } |
| 547 | | |
| 548 | | template <typename Container> |
| 549 | | inline typename std::enable_if<detail::is_container<const Container&>::value, |
| 550 | | bool>::type |
| 551 | | pretty_print(std::ostream& stream, const Container& value) { |
| 552 | | stream << "{"; |
| 553 | | const size_t size = detail::size(value); |
| 554 | | const size_t n = std::min(size_t{10}, size); |
| 555 | | size_t i = 0; |
| 556 | | using std::begin; |
| 557 | | using std::end; |
| 558 | | for (auto it = begin(value); it != end(value) && i < n; ++it, ++i) { |
| 559 | | pretty_print(stream, *it); |
| 560 | | if (i != n - 1) { |
| 561 | | stream << ", "; |
| 562 | | } |
| 563 | | } |
| 564 | | |
| 565 | | if (size > n) { |
| 566 | | stream << ", ..."; |
| 567 | | stream << " size:" << size; |
| 568 | | } |
| 569 | | |
| 570 | | stream << "}"; |
| 571 | | return true; |
| 572 | | } |
| 573 | | |
| 574 | | template <typename Enum> |
| 575 | | inline typename std::enable_if<std::is_enum<Enum>::value, bool>::type |
| 576 | | pretty_print(std::ostream& stream, Enum const& value) { |
| 577 | | using UnderlyingType = typename std::underlying_type<Enum>::type; |
| 578 | | stream << static_cast<UnderlyingType>(value); |
| 579 | | |
| 580 | | return true; |
| 581 | | } |
| 582 | | |
| 583 | | inline bool pretty_print(std::ostream& stream, const std::string& value) { |
| 584 | | stream << '"' << value << '"'; |
| 585 | | return true; |
| 586 | | } |
| 587 | | |
| 588 | | template <typename T1, typename T2> |
| 589 | | inline bool pretty_print(std::ostream& stream, const std::pair<T1, T2>& value) { |
| 590 | | stream << "{"; |
| 591 | | pretty_print(stream, value.first); |
| 592 | | stream << ", "; |
| 593 | | pretty_print(stream, value.second); |
| 594 | | stream << "}"; |
| 595 | | return true; |
| 596 | | } |
| 597 | | |
| 598 | | #if DBG_MACRO_CXX_STANDARD >= 17 |
| 599 | | |
| 600 | | template <typename T> |
| 601 | | inline bool pretty_print(std::ostream& stream, const std::optional<T>& value) { |
| 602 | | if (value) { |
| 603 | | stream << '{'; |
| 604 | | pretty_print(stream, *value); |
| 605 | | stream << '}'; |
| 606 | | } else { |
| 607 | | stream << "nullopt"; |
| 608 | | } |
| 609 | | |
| 610 | | return true; |
| 611 | | } |
| 612 | | |
| 613 | | template <typename... Ts> |
| 614 | | inline bool pretty_print(std::ostream& stream, |
| 615 | | const std::variant<Ts...>& value) { |
| 616 | | stream << "{"; |
| 617 | | std::visit([&stream](auto&& arg) { pretty_print(stream, arg); }, value); |
| 618 | | stream << "}"; |
| 619 | | |
| 620 | | return true; |
| 621 | | } |
| 622 | | |
| 623 | | #endif |
| 624 | | |
| 625 | | class DebugOutput { |
| 626 | | public: |
| 627 | | DebugOutput(const char* filepath, |
| 628 | | int line, |
| 629 | | const char* function_name, |
| 630 | | const char* expression) |
| 631 | | : m_use_colorized_output(isColorizedOutputEnabled()), |
| 632 | | m_filepath(filepath), |
| 633 | | m_line(line), |
| 634 | | m_function_name(function_name), |
| 635 | | m_expression(expression) { |
| 636 | | const std::size_t path_length = m_filepath.length(); |
| 637 | | if (path_length > MAX_PATH_LENGTH) { |
| 638 | | m_filepath = ".." + m_filepath.substr(path_length - MAX_PATH_LENGTH, |
| 639 | | MAX_PATH_LENGTH); |
| 640 | | } |
| 641 | | } |
| 642 | | |
| 643 | | template <typename T> |
| 644 | | T&& print(const std::string& type, T&& value) const { |
| 645 | | const T& ref = value; |
| 646 | | std::stringstream stream_value; |
| 647 | | const bool print_expr_and_type = pretty_print(stream_value, ref); |
| 648 | | |
| 649 | | std::stringstream output; |
| 650 | | output << ansi(ANSI_DEBUG) << "[" << m_filepath << ":" << m_line << " (" |
| 651 | | << m_function_name << ")] " << ansi(ANSI_RESET); |
| 652 | | if (print_expr_and_type) { |
| 653 | | output << ansi(ANSI_EXPRESSION) << m_expression << ansi(ANSI_RESET) |
| 654 | | << " = "; |
| 655 | | } |
| 656 | | output << ansi(ANSI_VALUE) << stream_value.str() << ansi(ANSI_RESET); |
| 657 | | if (print_expr_and_type) { |
| 658 | | output << " (" << ansi(ANSI_TYPE) << type << ansi(ANSI_RESET) << ")"; |
| 659 | | } |
| 660 | | output << std::endl; |
| 661 | | std::cerr << output.str(); |
| 662 | | |
| 663 | | return std::forward<T>(value); |
| 664 | | } |
| 665 | | |
| 666 | | private: |
| 667 | | const char* ansi(const char* code) const { |
| 668 | | if (m_use_colorized_output) { |
| 669 | | return code; |
| 670 | | } else { |
| 671 | | return ANSI_EMPTY; |
| 672 | | } |
| 673 | | } |
| 674 | | |
| 675 | | const bool m_use_colorized_output; |
| 676 | | |
| 677 | | std::string m_filepath; |
| 678 | | const int m_line; |
| 679 | | const std::string m_function_name; |
| 680 | | const std::string m_expression; |
| 681 | | |
| 682 | | static constexpr std::size_t MAX_PATH_LENGTH = 20; |
| 683 | | |
| 684 | | static constexpr const char* const ANSI_EMPTY = ""; |
| 685 | | static constexpr const char* const ANSI_DEBUG = "\x1b[02m"; |
| 686 | | static constexpr const char* const ANSI_EXPRESSION = "\x1b[36m"; |
| 687 | | static constexpr const char* const ANSI_VALUE = "\x1b[01m"; |
| 688 | | static constexpr const char* const ANSI_TYPE = "\x1b[32m"; |
| 689 | | static constexpr const char* const ANSI_RESET = "\x1b[0m"; |
| 690 | | }; |
| 691 | | |
| 692 | | // Identity function to suppress "-Wunused-value" warnings in DBG_MACRO_DISABLE |
| 693 | | // mode |
| 694 | | template <typename T> |
| 695 | | T&& identity(T&& t) { |
| 696 | | return std::forward<T>(t); |
| 697 | | } |
| 698 | | |
| 699 | | } // namespace dbg |
| 700 | | |
| 701 | | #ifndef DBG_MACRO_DISABLE |
| 702 | | // We use a variadic macro to support commas inside expressions (e.g. |
| 703 | | // initializer lists): |
| 704 | | #define dbg(...) \ |
| 705 | | dbg::DebugOutput(__FILE__, __LINE__, __func__, #__VA_ARGS__) \ |
| 706 | | .print(dbg::type_name<decltype(__VA_ARGS__)>(), (__VA_ARGS__)) |
| 707 | | #else |
| 708 | | #define dbg(...) dbg::identity(__VA_ARGS__) |
| 709 | | #endif // DBG_MACRO_DISABLE |
| 710 | | |
| 711 | | #endif // DBG_MACRO_DBG_H |