在C++ dll 中使用printf输出调试信息,可能会出现输出信息不及时的问题(尤其是在C#项目中),可以使用OutputDebugString 函数代替。这里为了方便,将OutputDebugString封装成一个函数,顺便支持不定长参数和时间打印,便于使用。
#include
#include
#include
void ShowDbgInfo(const char* data, ...)
{
char temp[2048];
if (!logFile)
{
logFile = fopen("debug.log", "w+");
}
SYSTEMTIME st;
GetLocalTime(&st);
sprintf(temp, "DLL日志输出: %d-%d-%d %02d:%02d:%02d:%03d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
OutputDebugStringA(temp);
fprintf(logFile, "%s", temp);
va_list ap;
va_start(ap, data);
vsprintf(temp, data, ap);
OutputDebugStringA(temp);
va_end(ap);
fprintf(logFile, "%s \n", temp);
fflush(logFile);
}