Bjarne 老爷子教导我们:要忘记 stdio.h,改用 iostream。于是,有了我这个日志类。主要站在了 ostream 的肩膀上,所以代码并不多:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <atlbase.h> #include <sstream> #include <string> class DebugBuffer : public std::stringbuf { protected: virtual int sync(void) { std::string dbg = str(); if (!dbg.empty()) { ::OutputDebugStringA(dbg.c_str()); dbg.clear(); str(dbg); } return 0; } }; class Logger : public std::basic_ostream<char> { public: Logger(void) : std::basic_ostream<char>(&m_buf) { // Nothing } protected: DebugBuffer m_buf; }; |
示例:
1 2 | Logger logger; logger << "Hello, World!" << endl; |
考虑到 Logger 是对 basic_ostream<char> 的继承,而有时又需要输出宽字符串,所以可以增加一个 operator << 的实现:
1 2 3 4 5 6 7 8 | inline std::basic_ostream<char>& operator << (std::basic_ostream<char>& stm, const std::wstring& ws) { USES_CONVERSION; std::string str(W2CA(ws.c_str())); stm << str; return stm; } |
当然,大多数时候我们希望只在 Debug 配置时输出日志。那么可以这么修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #ifdef _DEBUG #define DBGLOG(x) x #define LogEndl (std::endl) #else class EmptyLogger { }; extern __declspec(selectany) EmptyLogger theEmptyLoger; template <typename T> inline EmptyLogger& operator<<(EmptyLogger& logger, const T& obj) { return logger; } #define DBGLOG(x) theEmptyLoger #define LogEndl 0 #endif |
示例:
DBGLOG(logger) << "Hello, World!" << LogEndl; |
什么?还是钟情于类 printf 的格式控制符?也没问题,再加一个类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Formatter { public: explicit Formatter(const char *format, ...) { va_list args; va_start(args, format); size_t len = _vscprintf(format, args); m_str.resize(len); vsprintf_s(const_cast<char *>(m_str.c_str()), len + 1, format, args); va_end(args); } private: friend std::basic_ostream<char>& operator << (std::basic_ostream<char>&, const Formatter&); std::string m_str; }; inline std::basic_ostream<char>& operator << (std::basic_ostream<char>& stm, const Formatter& fmt) { stm << fmt.m_str; return stm; } |
示例:
DBGLOG(logger) << Formatter("%x", 123) << LogEndl; |
东野圭吾的《分身》阅毕。不得不从心中感叹东野是越来越会讲故事了,这个故事的情感内容几乎达到了《白夜行》的水平。——但是,说好的悬疑推理呢?这完全是本科幻伦理小说啊少年!只够得上《科幻世界》的水平啊亲!