Log.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:5k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : Log.cpp
- // PURPOSE : General logging classes for all applications.
- // PROGRAM :
- // DATE : Sept. 19 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "Resource.h"
- #include "WarSoftware.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- ////////////////////////////////////////////////////////////////////
- // General Logging classes
- CLog *CLog::m_pLog;
- CLog::CLog()
- {
- m_LogQueue.Empty();
- m_UseHistory = TRUE;
- m_EventHandle = CDaemonEvent::DsRegister(EVT_LOG, (LPVOID)this, SendHistory);
- if (!m_pLog)
- m_pLog = this;
- m_Flags = 0xffffffff; // Turn all flags on..
- }
- CLog::~CLog()
- {
- if (m_pLog == this)
- m_pLog = NULL;
- if (m_EventHandle)
- CDaemonEvent::DsClose(m_EventHandle);
- m_LogHistory.KillAll();
- //ASSERT(m_LogQueue.IsEmpty());
- m_LogQueue.Empty();
- }
- void CLog::LogMsg(int LogType, LPCSTR Format, ...)
- {
- if (!ShouldLog(this, LogType))
- return;
- ASSERT(AfxIsValidString(Format, FALSE));
- va_list argList;
- va_start(argList, Format);
- LogMsgV(LogType, Format, argList);
- va_end(argList);
- }
- void CLog::LogMsgV(int LogType, LPCTSTR Format, va_list argList)
- {
- if (!ShouldLog(this, LogType))
- return;
- va_list argListSave = argList;
- if (Format)
- {
- CWarString cBuf, cCurrentLogLine;
- char tbuf[32];
- if (!(LogType &
- (LOGF_SYSTEM |
- (int)m_LogDebug |
- ((int)m_LogErrors << 1) |
- ((int)m_LogFileAccess << 2) |
- ((int)m_LogInOut << 3) |
- ((int)m_LogSecurity << 4) |
- ((int)m_LogWarnings << 5) |
- ((int)m_LogWinsock << 7))))
- {
- goto done;
- }
- cBuf.Format("[%s%s%s%s%s%s%s%s %s] ",
- LogType & LOGF_SYSTEM ? "S" : "",
- LogType & LOGF_DEBUG ? "D" : "",
- LogType & LOGF_ERROR ? "E" : "",
- LogType & LOGF_FILEACC ? "F" : "",
- LogType & LOGF_INOUT ? "L" : "",
- LogType & LOGF_SECURITY ? "C" : "",
- LogType & LOGF_WARNINGS ? "W" : "",
- LogType & LOGF_WINSOCK ? "N" : "",
- MyTime(tbuf));
- cCurrentLogLine = cBuf;
- cBuf.FormatVaList(Format, argList);
- cCurrentLogLine += cBuf;
- cCurrentLogLine += "rn";
- m_Lock.Lock();
- m_LogQueue += cCurrentLogLine;
- // Add to history list
- if (m_UseHistory)
- m_LogHistory.Add(cCurrentLogLine, m_LogLines);
- m_Lock.Unlock();
- if (m_EventHandle)
- CDaemonEvent::DsEvent(m_EventHandle, cCurrentLogLine);
- LoggedLine(LogType, cCurrentLogLine);
- }
- Flush(FALSE);
- done:
- va_end(argListSave);
- }
- void CLog::LoggedLine(int LogType, LPCSTR Line)
- {
- // Override to log to window...
- }
- LPCSTR CLog::MyTime(LPSTR buf)
- {
- SYSTEMTIME st;
- GetLocalTime(&st);
- return FormatTime(st, buf);
- }
- LPCSTR CLog::FormatTime(SYSTEMTIME& st, LPSTR buf)
- {
- sprintf(buf,"%04d %02d %02d %02d:%02d",
- st.wYear,
- st.wMonth,
- st.wDay,
- st.wHour,
- st.wMinute);
- return buf;
- }
- BOOL CLog::Flush(BOOL Force)
- {
- CFile File;
- BOOL Rval = TRUE;
- m_Lock.Lock();
- // Optimize flags.
- m_Flags = LOGF_SYSTEM;
- m_Flags |= m_LogDebug ? LOGF_DEBUG : 0;
- m_Flags |= m_LogErrors ? LOGF_ERROR : 0;
- m_Flags |= m_LogFileAccess ? LOGF_FILEACC : 0;
- m_Flags |= m_LogInOut ? LOGF_INOUT : 0;
- m_Flags |= m_LogSecurity ? LOGF_SECURITY : 0;
- m_Flags |= m_LogWarnings ? LOGF_WARNINGS : 0;
- m_Flags |= m_LogWinsock ? LOGF_WINSOCK : 0;
- if (m_LogFile.GetLength() && (Force || m_Timer.TimeOut(m_Delay * 1000)))
- {
- CFileException FileEx;
- CString cLogFile;
- ExpandStringMacros(m_LogFile, cLogFile);
- ExpandPath(cLogFile);
- if (File.Open(cLogFile,
- CFile::modeWrite
- | CFile::modeCreate
- | CFile::modeNoTruncate
- | CFile::shareExclusive,
- &FileEx))
- {
- m_Timer.Reset();
- try
- {
- File.SeekToEnd();
- File.Write(m_LogQueue,m_LogQueue.GetLength());
- File.Close();
- m_LogQueue.Empty();
- }
- catch(CFileException* theException)
- {
- File.Close();
- theException->Delete();
- LogMsg(LOGF_WARNINGS,"CLog::Flush(): File IO failed.");
- goto failed;
- }
- }
- else
- {
- failed:
- // Failed to open file. If the buffer is too large, we just empty it...
- if (m_LogQueue.GetLength() > (1024 * 16))
- {
- m_LogQueue.Empty();
- LogMsg(LOGF_WARNINGS,"CLog::Flush(): Failed to flush logfile. Log buffer trashed due to buffer overflow.");
- Rval = FALSE;
- }
- }
- }
- m_Lock.Unlock();
- return Rval;
- }
- void CLog::SendHistory(LPVOID pOrigin, CDaemonEvent *pCliEv)
- {
- CString *cs;
- CLog *pLog = (CLog *)pOrigin;
- ASSERT(AfxIsValidAddress(pLog, sizeof(CLog)));
- CLinkedListItem *Item = pLog->m_LogHistory.First();
- while(Item)
- {
- cs = (CString *)pLog->m_LogHistory.Ptr(Item);
- ASSERT(AfxIsValidString(*cs));
- pCliEv->DsEvent(pLog->m_EventHandle, *cs);
- Item = pLog->m_LogHistory.Next(Item);
- }
- }