TRACEの実装
TRACEマクロは Afx.h の中で以下のように定義されている。#define TRACE ::AfxTraceつまり AfxTrace() が呼ばれている。
さらに AfxTrace() は Dumpout.cppの中で以下のように定義されている。
void AFX_CDECL AfxTrace(LPCTSTR lpszFormat, ...)
{
#ifdef _DEBUG // all AfxTrace output is controlled by afxTraceEnabled
if (!afxTraceEnabled)
return;
#endif
va_list args;
va_start(args, lpszFormat);
int nBuf;
TCHAR szBuffer[512];
nBuf = _vsntprintf(szBuffer, _countof(szBuffer), lpszFormat, args);
// was there an error? was the expanded string too long?
ASSERT(nBuf >= 0);
if ((afxTraceFlags & traceMultiApp) && (AfxGetApp() != NULL))
afxDump << AfxGetApp()->m_pszExeName << ": ";
afxDump << szBuffer;
va_end(args);
}
出力可能なのは半角512文字まで!
TRACEの定義で注目したいのがTCHAR szBuffer[512];の1文。512バイトしかバッファーを確保していないのでこれ以上の出力を行うこと ができない。実際に512バイト以上を表示しようとすると...
// was there an error? was the expanded string too long? ASSERT(nBuf >= 0);この部分でアサーション・エラーが生じる。
制限をなくすには
#ifdef _DEBUG afxDump << szBuffer; #endifを使えばいい。書式付にしたいなら以下のようにするだけ。
#ifdef _DEBUG
CString strTrace;
strTrace.Format("%d",10);
afxDump << strTrace;
#endif







