tshm
6/21/2013 - 4:42 AM

logging adapter / stream I/F for VS2012 UT FW.

logging adapter / stream I/F for VS2012 UT FW.

#ifdef _DEBUG
std::string _dtrace(CHAR *format, ...)
{
	CHAR buffer[1000];
	va_list argptr;
	va_start(argptr, format);
	wvsprintfA(buffer, format, argptr);
	va_end(argptr);
	std::string outbuf = std::string(buffer) + "\n";
	return outbuf;
}

class _tracer_ {
public:
	std::string fname;
	_tracer_(char* fname0, std::string msg) {
		fname = fname0 + std::string("() ");
		OutputDebugStringA((fname + msg).c_str());
	}
	~_tracer_() {
		_dtrace("%s end", fname.c_str());
	}
	void add_msg(std::string msg) {
		OutputDebugStringA(msg.c_str());
	}
};
#define trace(...) _tracer_ _tracer__(__FUNCTION__, _dtrace(__VA_ARGS__))
#define atrace(...) _tracer__.add_msg(_dtrace(__VA_ARGS__))

// utility function for observe looping period
void dumpTiming() {
	static std::chrono::milliseconds s0, s1;
	s1 = s0;
	s0 = std::chrono::duration_cast< std::chrono::milliseconds >(
		std::chrono::system_clock::now().time_since_epoch()
		);
	char buff[100];
	_snprintf_s(buff, sizeof(buff), "  %d\n", s1 - s0);
	OutputDebugStringA(buff);
}
#else
#define trace(...)
#define dumpTiming(...)
#endif

// adapter for the test framework Logger.
class DebugLogger {
  std::stringstream str;
public:
	DebugLogger(): str("") {}
	~DebugLogger() { Logger::WriteMessage( str.str().c_str() ); }
	template<typename T> DebugLogger& operator<<( const T& val ) {
		str << val;
		return *this;
	}
};
#define slog DebugLogger()
#define log(str__) DebugLogger() << str__ << "\n"