C++ 文件操作与内存分配器详解
1. 日志记录器示例
1.1 目标
将 std::clog
流的输出重定向到日志文件和控制台。同时,若调试级别不足或调试被禁用,日志记录功能应被编译掉。
1.2 实现步骤
- 定义调试级别和调试开关 :
#ifdef DEBUG_LEVEL
constexpr auto g_debug_level = DEBUG_LEVEL;
#else
constexpr auto g_debug_level = 0;
#endif
#ifdef NDEBUG
constexpr auto g_ndebug = true;
#else
constexpr auto g_ndebug = false;
#endif
- 创建全局日志文件流 :
std::fstream g_log{"log.txt", std::ios::out | std::ios::app};
- 定义日志记录函数 :
template <std::size_t LEVEL>
constexpr voi