흔히 볼 수 있는 가변인자의 활용에는 printf가 있다.
파라메터의 갯수에 상관없이 알아서 출력해주는 기능이 편하다.
프로그램을 하다보면 이러한 기능을 써야할 때가 종종 있다. 예를들면 printf로는 로그를 남기기 부족할 때이다.
아래 방법은 MFC에서 쓰는 방법이므로 C에서 쓰려면 손봐야 한다.
szFuncName = __FUNCTION__
nLine = __LINE__
// 넘어오는 param은 위의 매크로를 사용했다.
int debuglvl = LOG_INFO;
void DBGOutPut(TCHAR* szFuncName, int nLine, LOG_LEVEL debuglvl, TCHAR* pszErr, ...)
{
CString strComplete;
// 로그 레벨 지정
int lvl = LOG_INFO;
lvl = GetPrivateProfileInt(_T("Log"), _T("DebugLevel"), 2, path);
if (debuglvl <= lvl){
static CString strErr;
va_list ap;
va_start(ap, pszErr);
strErr.FormatV(pszErr, ap);
va_end(ap);
strComplete.Format(_T("[RegService] [FUNCTION : %s][LINE : %d][LastErr : %d] = "), szFuncName, nLine, GetLastError());
strComplete += strErr;
OutputDebugString(strComplete);
OutputDebugString(_T("\n"));
}
}
이 함수를 사용하면 DebugView에서 로그 찍히는 것을 확인해볼 수 있다.
No comments:
Post a Comment