Skip to content

Instantly share code, notes, and snippets.

@tshm
Last active July 7, 2016 03:21
Show Gist options
  • Save tshm/b2132b061f944bac2372 to your computer and use it in GitHub Desktop.
Save tshm/b2132b061f944bac2372 to your computer and use it in GitHub Desktop.
logging adapter / stream I/F for VS2012 UT FW.
// 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"
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment