首页 > 解决方案 > 如何使用此 Logger 记录特定类别的数据类型

问题描述

我是 C++ 新手,我正在做一个类项目(CMU 15-445 https://github.com/cmu-db/bustub)和调试。但我不知道如何使用这个记录器。

logger.h文件中,LOG_INFO是这样定义的:

#ifdef LOG_INFO_ENABLED
#undef LOG_INFO_ENABLED
#endif
#if LOG_LEVEL <= LOG_LEVEL_INFO
#define LOG_INFO_ENABLED
// #pragma message("LOG_INFO was enabled.")
#define LOG_INFO(...)                                                      \
  OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_INFO); \
  ::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__);                               \
  fprintf(LOG_OUTPUT_STREAM, "\n");                                        \
  ::fflush(stdout)
#else
#define LOG_INFO(...) ((void)0)
#endif

我知道 LOG_INFO("xxx") 可以打印出 xxx,但我想在generic_key.h中打印一个 <Generic_Key> 类型的值

  // NOTE: for test purpose only
  // interpret the first 8 bytes as int64_t from data vector
  inline int64_t ToString() const { return *reinterpret_cast<int64_t *>(const_cast<char *>(data_)); }

  // NOTE: for test purpose only
  // interpret the first 8 bytes as int64_t from data vector
  friend std::ostream &operator<<(std::ostream &os, const GenericKey &key) {
    os << key.ToString();
    return os;
  }

因此,如果我想打印 The key is inserted.并且密钥是 GenricKey 的类型。我尝试过类似 LOG_INFO("The key" << key << "is inserted"); 但这是错误的。有什么想法该怎么做?

标签: c++logging

解决方案


推荐阅读