简介
早在2001-2002年期间,当我在工作需要某些Windows服务要建一个项目,我需要的信息相同的服务如何在Win 2K/XP,掌上电脑,运行Win CE的设备,以及与数据操纵。我写了一些功能,将记录到一个文本文件所需的信息。该功能可以采取任何被记录到日志文件的参数。
本文介绍如何登录到一个预定义的文本文件的日期和时间戳记所需要的信息。使用代码
有如下三个功能,功能注释块中提到的有关各功能的详细信息。
使用代码,在此代码替换CKishoreResearch与您的项目类名称,或者你可以在这三种功能,在一些common.h中文件中,作为全球功能在您的项目中使用它们。
在H文件,声明如下:// File pointer that will be used
// in the log functions to write to a valid file
FILE *m_fp;
// Logs the Pointer to a null-terminated
// string of 16-bit Unicode characters
void log(LPWSTR str, ...);
// Logs the Pointer to a null-terminated string
// of 8-bit Windows (ANSI) characters.
void log(LPSTR str, ...);
m_fp = NULL;
m_fp = _wfopen(L"C:\\KishoreLog.txt", L"a");
在CPP文件中,在类的构造,使用下面的代码:/*
Function : void log(LPWSTR str, ...)
Return Type : void
parameters : LPWSTR,Ellipsis
Use : Logs the information onto a text file
with any number of input parameters
: Pointer to a constant null-terminated
string of 16-bit Unicode characters.
Author : Kishore Gaddam
Date : 03 December 2002
Version : 1.0
*/
void CKishoreResearch::log(LPWSTR str, ...)
{
if (m_fp)
// m_fp Contains the pointer
// to the log file to log the information
{
va_list arg_ptr;
va_start(arg_ptr, str);
SYSTEMTIME st;
GetLocalTime(&st);
fwprintf(m_fp, L"[%d/%d/%d - %d:%d:%d:%d]",
st.wMonth, st.wDay, st.wYear, st.wHour,
st.wMinute, st.wSecond, st.wMilliseconds);
vfwprintf(m_fp, str, arg_ptr);
fwprintf(m_fp, L"\n");
fflush(m_fp);
}
}
/*
Function : void log(LPSTR str, ...)
Return Type : void
parameters : LPSTR,Ellipsis
Use : Logs the information onto a text file
with any number of input parameters
: Pointer to a null-terminated string
of 8-bit Windows (ANSI) characters.
Author : Kishore Gaddam
Date : 03 December 2002
Version : 1.0
*/
void CKishoreResearch::log(LPSTR str, ...)
{
if (m_fp)
// m_fp Contains the pointer
// to the log file to log the information
{
va_list arg_ptr;
va_start(arg_ptr, str);
SYSTEMTIME st;
GetLocalTime(&st);
fprintf(m_fp, "[%d/%d/%d - %d:%d:%d:%d]", st.wMonth,
st.wDay, st.wYear, st.wHour, st.wMinute,
st.wSecond, st.wMilliseconds);
vfprintf(m_fp, str, arg_ptr);
fprintf(m_fp, "\n");
fflush(m_fp);
}
}
/*
Function : ComErrorMessageLog(_com_error &e)
Return Type : void
parameters : _com_error
Use : Logs the COM error information to a text file
Author : Kishore Gaddam
Date : 03 December 2004
Version : 1.0
*/
void CKishoreResearch::ComErrorMessageLog(_com_error &e)
{
bstr_t bstrSource(e.Source());
bstr_t bstrDescription(e.Description());
log( _T("Code = 0x%08lx\n"), e.Error());
log( _T("Code meaning = %s\n"), e.ErrorMessage());
log( _T("IErrorInfo.Source = %s\n"), (LPTSTR)bstrSource );
log( _T("IErrorInfo.Description = %s"),
(LPTSTR)bstrDescription );
}
以下是实现代码,实际上记录功能:
复制到您的代码中的上述三个功能。同样,使用的代码,在上面的代码替换您的项目中的类的名称类CKishoreResearch。一旦这三个功能是复制,您可以拨打电话到日志(..)函数并传递所需的信息,需要登录到文本文件。记录常用的数据类型
下面列出了一些常用的数据类型的例子。//bsSelection is the value that needs
//to be logged into the log file.
_bstr_t bsSelection;
//Make a call to Log function and pass
//(LPTSTR)bsSelection as the parameter.
log((LPTSTR)bsSelection);
例如,如果你有一个_bstr_t数据类型的变量,你要记录使用日志功能相同的价值:
要记录一个TCHAR数据类型的值:TCHAR szSql[256];
log(szSql);
日志随着一些用户自定义的字符串int数据类型的值:int nData;
log("Item Select = %d", nData);
UINT cbID1,cbID2;
log(L" ID1 is %d and ID2 is %d", cbID1, cbID2);
要登录多个uint数据类型的值,随着一些用户定义的字符串:TCHAR *szFolderName
log("Folder: [%s]", szFolderName);
日志随着一些用户自定义的字符串一个TCHAR数据类型的值:兴趣点
除了从记录所需的信息,你也可以在异常处理中使用这些功能来记录异常原因。catch(_com_error &e)
{
::MessageBox(NULL, (char*)e.Description(),
"Application Error", MB_OK|MB_ICONERROR);
ComErrorMessageLog(e);
CoUninitialize();
}
以下是将一个COM异常的情况下,在catch块中的代码:catch(CException* e)
/ Handle all other types of exceptions here.
{
TCHAR szErr[1024];
e->GetErrorMessage(szErr, 1024);
::MessageBox(NULL, (char*)szErr, "Application Error",
MB_OK|MB_ICONERROR);
log(szErr);
}
以下是将在catch块中,在一个正常的异常情况下,在MFC中的代码:
好了,使用日志功能列表的东西会和。所以,我会停在这里,让用户使用上面的代码来记录他们的信息。 :)