现在的位置: 首页 > 综合 > 正文

在非控制台程序中使用控制台来调试程序。

2013年06月10日 ⁄ 综合 ⁄ 共 583字 ⁄ 字号 评论关闭

对于在某些情况下,在非console程序中需要控制台来打印日志方便的调试程序。可以用下面这个类。写的粗糙,

class CConsoleLog
{
public:
 CConsoleLog(void);
 ~CConsoleLog(void);
 void Log(char *format, ...);
};

#include <Windows.h>
#include <stdio.h>
#include <stdarg.h>

 

CConsoleLog::CConsoleLog(void)
{
 BOOL ret = AllocConsole();
 if(!ret)
 {
  throw "allocconsole failed";
 }
 freopen("CON", "w", stdout);
}

CConsoleLog::~CConsoleLog(void)
{
 BOOL ret = FreeConsole();
 if(!ret)
 {
  throw "free console failed";
 }
}

void CConsoleLog::Log(char *format, ...)
{
 va_list ap;
 char buf[255] = {0};
 va_start(ap, format);
 vsprintf(buf, format, ap);
 va_end(ap);
 fprintf(stdout, buf);
 return;
}

记下,方便以后查找

【上篇】
【下篇】

抱歉!评论已关闭.