实现CLogClass以进行体面的日志记录后我也定义了宏,但它只适用于一个参数......
class CLogClass { public: static void DoLog(LPCTSTR sMessage, ...); }; #define DebugLog(sMessage, x) ClogClass::DoLog(__FILE__, __LINE__, sMessage, x)
好吧,当用超过2个参数调用它时会失败:( ...它是否可以避免它?它能以某种方式转换为模板吗?
编辑:Variadic宏在VS 2005中引入(但我目前在VS 2003 ...).有什么建议吗?
你可以有一个MYLOG宏返回一个自定义的functor对象,它接受可变数量的参数.
#include#include struct CLogObject { void operator()( const char* pFormat, ... ) const { printf( "[%s:%d] ", filename.c_str(), linenumber ); va_list args; va_start( args, pFormat ); vfprintf( stderr, pFormat, args ); va_end( args ); } CLogObject( std::string filename, const int linenumber ) : filename( filename ), linenumber( linenumber ) {} std::string filename; int linenumber; }; #define MYLOG CLogObject( __FILE__, __LINE__ ) int _tmain(int argc, _TCHAR* argv[]) { MYLOG( "%s, %d", "string", 5 ); return 0; }
请注意,这个答案触及的类型安全变体并不是很难:由于链接效应,您不需要任何可变参数operator<<
.
struct CTSLogObject { template< typename T > std::ostream& operator<<( const T& t ) const { return std::cout << "[" << filename << ":" << linenumber << "] "; } CTSLogObject( std::string filename, const int linenumber ) : filename( filename ), linenumber( linenumber ) {} std::string filename; int linenumber; }; #define typesafelog CTSLogObject( __FILE__, __LINE__ ) int _tmain(int argc, _TCHAR* argv[]) { typesafelog << "typesafe" << ", " << 5 << std::endl; return 0; }