debug_utility.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef SOPHIAR2_DEBUG_MACRO_H
  2. #define SOPHIAR2_DEBUG_MACRO_H
  3. #include "core/timestamp_helper.hpp"
  4. #include "third_party/scope_guard.hpp"
  5. #include "utility/versatile_buffer2.hpp"
  6. #include <boost/asio/awaitable.hpp>
  7. #include <boost/asio/high_resolution_timer.hpp>
  8. #include <boost/asio/this_coro.hpp>
  9. #include <boost/asio/use_awaitable.hpp>
  10. #include <boost/core/demangle.hpp>
  11. #include <spdlog/spdlog.h>
  12. #include <iostream>
  13. #define DEBUG_PRINT(msg) \
  14. std::cout << fmt::format("F:{} L:{} T:{} {}", \
  15. __FILE__, __LINE__, current_timestamp(), msg) \
  16. << std::endl
  17. #define FILE_LINE_TRACE DEBUG_PRINT("");
  18. #define FILE_LINE_TRACE_WITH_THIS \
  19. { \
  20. auto __msg = fmt::format("TH:{}", (void *) this); \
  21. DEBUG_PRINT(__msg); \
  22. }
  23. #define DEBUG_PRINT_ADDRESS(obj) \
  24. { \
  25. auto __msg = fmt::format("PTR:{}", (void *) std::addressof(obj)); \
  26. DEBUG_PRINT(__msg); \
  27. }
  28. #define DEBUG_PRINT_TYPE(type) \
  29. { \
  30. auto __msg = fmt::format("TYPE:{}", boost::core::demangle(typeid(type).name())); \
  31. DEBUG_PRINT(__msg); \
  32. }
  33. #define ENSURE(func) \
  34. { \
  35. bool ok = (func); \
  36. if (!ok) return false; \
  37. }
  38. #define ENSURE_CO(func) \
  39. { \
  40. bool ok = (func); \
  41. if (!ok) co_return false; \
  42. }
  43. #define CO_ENSURE(func) \
  44. { \
  45. bool ok = co_await (func); \
  46. if (!ok) { \
  47. co_return false; \
  48. } \
  49. }
  50. #define RUN_ONCE \
  51. static bool __is_called = false; \
  52. if (__is_called) [[likely]] return; \
  53. __is_called = true;
  54. namespace sophiar {
  55. using boost::asio::awaitable;
  56. using boost::asio::use_awaitable;
  57. template<typename DurationType>
  58. inline awaitable<void> coro_sleep(DurationType t) {
  59. boost::asio::high_resolution_timer timer(co_await boost::asio::this_coro::executor);
  60. timer.expires_from_now(t);
  61. co_await timer.async_wait(use_awaitable);
  62. co_return;
  63. }
  64. template<typename BasicObjType>
  65. void print_variable(const char *var_name, const BasicObjType &var) {
  66. auto ss = string_writer{};
  67. var->write_to(ss);
  68. std::cout << fmt::format("{} = {}", var_name, ss.get_string_and_reset()) << std::endl;
  69. }
  70. #define PRINT_VARIABLE(var) print_variable(#var, var)
  71. }
  72. #endif //SOPHIAR2_DEBUG_MACRO_H