www.pudn.com > loggerclient_src.zip > Logger.cpp
/*
* $Header: /logger/loggertest/Logger.cpp 1 11/11/03 2:44a Administrator $
*
* $History: Logger.cpp $
*
* ***************** Version 1 *****************
* User: Administrator Date: 11/11/03 Time: 2:44a
* Created in $/logger/loggertest
*/
#include "stdafx.h"
#include "Logger.h"
#include "Sleeper.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNAMIC(CLogger, CObject);
DWORD CLogger::m_dwSequence;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLogger::CLogger() : CObject()
{
TCHAR tszServerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD dwLength = sizeof(tszServerName);
m_dwSequence = 0;
m_dwLogLevel = 0;
GetComputerName(tszServerName, &dwLength);
m_csServerName = tszServerName;
m_iRetry = PIPE_RETRY_COUNT;
}
CLogger::~CLogger()
{
}
// dwLogLevel specifies a threshhold for message severity. Messages
// at or below dwLogLevel are written to the log pipe, other messages
// are thrown away.
void CLogger::Initialise(DWORD dwLogLevel, LPCTSTR szModuleName, LPCTSTR szLogFileName, LPCTSTR szServerName)
{
ASSERT(szServerName);
ASSERT(AfxIsValidString(szServerName));
ASSERT(szModuleName);
ASSERT(AfxIsValidString(szModuleName));
ASSERT(szLogFileName);
ASSERT(AfxIsValidString(szLogFileName));
m_csModuleName = szModuleName;
m_csPipeName.Format(_T("\\\\%s\\pipe\\%s.log"), szServerName, szLogFileName);
m_dwLogLevel = dwLogLevel;
m_csTargetServer = szServerName;
m_csLogFileName = szLogFileName;
m_pipe.Initialise(m_csPipeName);
CreateClientConnection();
}
void CLogger::CreateClientConnection()
{
// Create a temporary connection to the log service command pipe
CString csTemp;
CClientPipeInstance pTemp;
csTemp.Format(_T("\\\\%s\\pipe\\%s"), m_csTargetServer, COMMANDPIPENAME);
pTemp.Initialise(csTemp);
csTemp.Format(_T("createlog,%s"), m_csLogFileName);
pTemp.Write(csTemp);
// We need this (one time only) sleep to ensure the logger service
// has a chance to run and create the pipe.
CSleeper sleeper;
sleeper.Sleep(20);
}
void CLogger::Log(DWORD dwErrorCode, LPCTSTR szFormatString, ...)
{
if (dwErrorCode / 1000 <= m_dwLogLevel)
{
ASSERT(szFormatString);
ASSERT(AfxIsValidString(szFormatString));
va_list args;
va_start(args, szFormatString);
Log(dwErrorCode, szFormatString, args);
va_end(args);
}
}
void CLogger::Log(DWORD dwErrorCode, ...)
{
if (dwErrorCode / 1000 <= m_dwLogLevel)
{
CString csFormat;
va_list args;
csFormat.LoadString(dwErrorCode);
va_start(args, dwErrorCode);
Log(dwErrorCode, csFormat, args);
va_end(args);
}
}
void CLogger::Log(DWORD dwErrorCode, LPCTSTR szFormatString, va_list args)
{
ASSERT(szFormatString);
ASSERT(AfxIsValidString(szFormatString));
DWORD dwLen;
TCHAR szBuffer[BUFFERSIZE],
szFormat[BUFFERSIZE];
CString csTimestamp;
SYSTEMTIME st;
GetLocalTime(&st);
csTimestamp.Format(_T("%02d/%02d/%04d %02d:%02d:%02d.%03d"), st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
_sntprintf(szFormat, BUFFERSIZE, _T("%s,%d,%d,%d,%s,%s,%s\r\n"), csTimestamp, m_dwSequence++, dwErrorCode, GetCurrentThreadId(), m_csModuleName, m_csServerName, szFormatString);
szFormat[BUFFERSIZE - 1] = '\0';
dwLen = _vsntprintf(szBuffer, BUFFERSIZE, szFormat, args);
// m_iRetry is initialised to PIPE_RETRY_COUNT in the constructor.
// If it's 0 when we get here it's because we've already tried
// PIPE_RETRY_COUNT times to write to the logpipe and failed each
// time. Basically we've given up!
while (m_iRetry)
{
if (m_pipe.Write(szBuffer) == FALSE)
{
// We couldn't write, so let's assume it was a broken pipe
// and try again.
m_iRetry--;
CreateClientConnection();
}
else
{
// We succeeded in writing so reset the retry counter
m_iRetry = PIPE_RETRY_COUNT;
break;
}
}
}