我使用宏研究日志记录机制:
#define LOGFATAL 1 // very serious errors
#define TOSTRING(s) dynamic_cast< std::ostringstream & >((std::ostringstream() << s ) ).str()
#define LOG_TRANSFER(t, s) "[" << t << "] " << s
void inline print_log(const char *level, const std::string& value)
{
std::cout << "[" << timestamp() << "]["<< level << "]" << value << std::endl;
}
#ifdef DEBUGGING
#define DEBUG_OUT(l, s) print_log(l, TOSTRING(s));
#else
#define DEBUG_OUT(s, l)
#endif
#if DEBUGGING >= LOGFATAL
#define LOG_FATAL(s) DEBUG_OUT("FATAL", s)
#else
#define LOG_FATAL(s)
#endif
我按照他的方式使用它: LOG_TRACE(LOG_TRANSFER(ptr, "MemoryManager > allocate ")) LOG_TRACE("MemoryManager > size : " << active_list.size() )
gcc 的作用是:
print_log("TRACE", dynamic_cast< std::ostringstream & >((std::ostringstream() << "[" << ptr << "] " << "MemoryManager > allocate " ) ).str());
print_log("TRACE", dynamic_cast< std::ostringstream & >((std::ostringstream() << "MemoryManager > size : " << active_list.size() ) ).str());
这对我来说看起来不错,但我得到以下输出:
[0 s][TRACE]0x80940d40x9988ea8] MemoryManager > allocate
[0 s][TRACE]0x80941e01
错误在哪里?