マクロ集 / out


マクロ集

OutputDebugString?にログを出力する

利点  ログ出力用のウィンドウが開かないのでデバッグが終わっても出力したままにしておける。出力内容を取得したい場合はこのツールを起動しておく。

サンプルコード

#include "out.txc"

main
{
  printf("test\n"); // OutputDebugStringで出力される
}

out.txc

#ifndef out_h
#define out_h

#include <windows.h>

#define printf outf

//## OutputDebugStringでログを出力
void outf(const mchar *format, ...)
{
	static mchar buf[32*1024];
	va_list args;
	va_start(args,format);
	wvsprintf(buf,format,args);
	va_end(args);
	OutputDebugString(buf);
}

//## Win32APIの成否をチェックし、失敗したらエラーメッセージを出力する。
#define Win32Check(cond) win32check(cond, __FILE__, __LINE__)

BOOL win32check(BOOL cond, mchar *file, int line)
{
	if(cond)
		return cond;

	DWORD code = GetLastError();
	static mchar buf[4096];
	FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, code,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // デフォルト言語
		buf, sizeof(buf), NULL);
	outf("\"%s\"(%d): Win32API failed: %s(%d)\n", file, line, buf, code);
	return cond;
}

//## テストコード
testout
{
	printf("hello,world:%d\n", GetTickCount());
}

#endif // out_h