CLogFile Class Reference

#include <svdhash.h>

Collaboration diagram for CLogFile:

Collaboration graph
[legend]

Public Member Functions

 CLogFile (void)
 ~CLogFile (void)
void WriteBuffer (size_t nString, const char *pString)
void WriteString (const char *pString)
void WriteInteger (int iNumber)
void DCL_CDECL tinyprintf (char *pFormatSpec,...)
void Flush (void)
void SetPrefix (const char *pPrefix)
void SetBasename (const char *pBasename)
void StartLogging (void)
void StopLogging (void)

Private Member Functions

void CreateLogFile (void)
void AppendLogFile (void)
void CloseLogFile (void)

Private Attributes

CLinearTimeAbsolute m_ltaStarted
HANDLE m_hFile
size_t m_nSize
size_t m_nBuffer
char m_aBuffer [SIZEOF_LOG_BUFFER]
bool bEnabled
bool bUseStderr
char * m_pBasename
char m_szPrefix [32]
char m_szFilename [SIZEOF_PATHNAME]

Detailed Description

Definition at line 290 of file svdhash.h.


Constructor & Destructor Documentation

CLogFile::CLogFile ( void   ) 

Definition at line 2790 of file svdhash.cpp.

References bEnabled, bUseStderr, CLinearTimeAbsolute::GetLocal(), INVALID_HANDLE_VALUE, m_hFile, m_ltaStarted, m_nBuffer, m_nSize, m_pBasename, m_szFilename, and m_szPrefix.

02791 {
02792 #ifdef WIN32
02793     InitializeCriticalSection(&csLog);
02794 #endif // WIN32
02795 
02796     m_ltaStarted.GetLocal();
02797     m_hFile = INVALID_HANDLE_VALUE;
02798     m_nSize = 0;
02799     m_nBuffer = 0;
02800     bEnabled = false;
02801     bUseStderr = true;
02802     m_pBasename = NULL;
02803     m_szPrefix[0] = '\0';
02804     m_szFilename[0] = '\0';
02805 }

CLogFile::~CLogFile ( void   ) 

Definition at line 2831 of file svdhash.cpp.

References StopLogging().

02832 {
02833     StopLogging();
02834 #ifdef WIN32
02835     DeleteCriticalSection(&csLog);
02836 #endif // WIN32
02837 }


Member Function Documentation

void CLogFile::AppendLogFile ( void   )  [private]

Definition at line 2667 of file svdhash.cpp.

References CloseLogFile(), INVALID_HANDLE_VALUE, m_hFile, m_szFilename, and O_BINARY.

Referenced by SetPrefix().

02668 {
02669     CloseLogFile();
02670 
02671 #ifdef WIN32
02672     m_hFile = CreateFile(m_szFilename, GENERIC_READ | GENERIC_WRITE,
02673         FILE_SHARE_READ, 0, OPEN_ALWAYS,
02674         FILE_ATTRIBUTE_NORMAL + FILE_FLAG_SEQUENTIAL_SCAN, NULL);
02675 #else // WIN32
02676     m_hFile = open(m_szFilename, O_RDWR|O_BINARY, 0600);
02677 #endif // WIN32
02678 
02679     if (m_hFile != INVALID_HANDLE_VALUE)
02680     {
02681 #ifdef WIN32
02682         SetFilePointer(m_hFile, 0, 0, FILE_END);
02683 #else // WIN32
02684         lseek(m_hFile, 0, SEEK_END);
02685 #endif // WIN32
02686     }
02687 }

void CLogFile::CloseLogFile ( void   )  [private]

Definition at line 2689 of file svdhash.cpp.

References INVALID_HANDLE_VALUE, and m_hFile.

Referenced by AppendLogFile(), CreateLogFile(), Flush(), SetPrefix(), and StopLogging().

02690 {
02691     if (m_hFile != INVALID_HANDLE_VALUE)
02692     {
02693 #ifdef WIN32
02694         CloseHandle(m_hFile);
02695 #else // WIN32
02696         close(m_hFile);
02697 #endif // WIN32
02698         m_hFile = INVALID_HANDLE_VALUE;
02699     }
02700 }

void CLogFile::CreateLogFile ( void   )  [private]

Definition at line 2653 of file svdhash.cpp.

References CloseLogFile(), m_hFile, m_nSize, m_szFilename, and O_BINARY.

Referenced by Flush(), and StartLogging().

02654 {
02655     CloseLogFile();
02656 
02657     m_nSize = 0;
02658 #ifdef WIN32
02659     m_hFile = CreateFile(m_szFilename, GENERIC_READ | GENERIC_WRITE,
02660         FILE_SHARE_READ, 0, CREATE_ALWAYS,
02661         FILE_ATTRIBUTE_NORMAL + FILE_FLAG_SEQUENTIAL_SCAN, NULL);
02662 #else // WIN32
02663     m_hFile = open(m_szFilename, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, 0600);
02664 #endif // WIN32
02665 }

void CLogFile::Flush ( void   ) 

Definition at line 2704 of file svdhash.cpp.

References bEnabled, bUseStderr, CloseLogFile(), CreateLogFile(), FILE_SIZE_TRIGGER, CLinearTimeAbsolute::GetLocal(), m_aBuffer, m_hFile, m_ltaStarted, m_nBuffer, m_nSize, m_pBasename, m_szFilename, m_szPrefix, and MakeLogName().

Referenced by AssertionFailed(), db_read(), db_write(), end_log(), log_perror(), OutOfMemory(), sighandler(), StopLogging(), unparse_boolexp1(), and WriteBuffer().

02705 {
02706     if (  m_nBuffer <= 0
02707        || !bEnabled)
02708     {
02709         return;
02710     }
02711     if (bUseStderr)
02712     {
02713         fwrite(m_aBuffer, m_nBuffer, 1, stderr);
02714     }
02715     else
02716     {
02717         m_nSize += m_nBuffer;
02718 #ifdef WIN32
02719         unsigned long nWritten;
02720         WriteFile(m_hFile, m_aBuffer, (DWORD)m_nBuffer, &nWritten, NULL);
02721 #else // WIN32
02722         write(m_hFile, m_aBuffer, m_nBuffer);
02723 #endif // WIN32
02724 
02725         if (m_nSize > FILE_SIZE_TRIGGER)
02726         {
02727             CloseLogFile();
02728 
02729             m_ltaStarted.GetLocal();
02730             MakeLogName(m_pBasename, m_szPrefix, m_ltaStarted, m_szFilename);
02731 
02732             CreateLogFile();
02733         }
02734     }
02735     m_nBuffer = 0;
02736 }

void CLogFile::SetBasename ( const char *  pBasename  ) 

Definition at line 2764 of file svdhash.cpp.

References bUseStderr, m_pBasename, MEMFREE, and StringClone().

Referenced by dbconvert(), main(), and StopLogging().

02765 {
02766     if (m_pBasename)
02767     {
02768         MEMFREE(m_pBasename);
02769         m_pBasename = NULL;
02770     }
02771     if (  pBasename
02772        && strcmp(pBasename, "-") == 0)
02773     {
02774         bUseStderr = true;
02775     }
02776     else
02777     {
02778         bUseStderr = false;
02779         if (pBasename)
02780         {
02781             m_pBasename = StringClone(pBasename);
02782         }
02783         else
02784         {
02785             m_pBasename = StringClone("");
02786         }
02787     }
02788 }

void CLogFile::SetPrefix ( const char *  pPrefix  ) 

Definition at line 2738 of file svdhash.cpp.

References AppendLogFile(), bEnabled, bUseStderr, CloseLogFile(), m_ltaStarted, m_pBasename, m_szFilename, m_szPrefix, MakeLogName(), ReplaceFile(), and SIZEOF_PATHNAME.

Referenced by CF_HAND().

02739 {
02740     if (  !bUseStderr
02741        && strcmp(szPrefix, m_szPrefix) != 0)
02742     {
02743         if (bEnabled)
02744         {
02745             CloseLogFile();
02746         }
02747 
02748         char szNewName[SIZEOF_PATHNAME];
02749         MakeLogName(m_pBasename, szPrefix, m_ltaStarted, szNewName);
02750         if (bEnabled)
02751         {
02752             ReplaceFile(m_szFilename, szNewName);
02753         }
02754         strcpy(m_szPrefix, szPrefix);
02755         strcpy(m_szFilename, szNewName);
02756 
02757         if (bEnabled)
02758         {
02759             AppendLogFile();
02760         }
02761     }
02762 }

void CLogFile::StartLogging ( void   ) 

Definition at line 2807 of file svdhash.cpp.

References bEnabled, bUseStderr, CreateLogFile(), CLinearTimeAbsolute::GetLocal(), m_ltaStarted, m_pBasename, m_szFilename, m_szPrefix, and MakeLogName().

Referenced by dbconvert(), and main().

02808 {
02809     if (!bUseStderr)
02810     {
02811         m_ltaStarted.GetLocal();
02812         MakeLogName(m_pBasename, m_szPrefix, m_ltaStarted, m_szFilename);
02813         CreateLogFile();
02814     }
02815     bEnabled = true;
02816 }

void CLogFile::StopLogging ( void   ) 

Definition at line 2818 of file svdhash.cpp.

References bEnabled, bUseStderr, CloseLogFile(), Flush(), m_szFilename, m_szPrefix, and SetBasename().

Referenced by do_restart(), and ~CLogFile().

02819 {
02820     Flush();
02821     bEnabled = false;
02822     if (!bUseStderr)
02823     {
02824         CloseLogFile();
02825         m_szPrefix[0] = '\0';
02826         m_szFilename[0] = '\0';
02827         SetBasename(NULL);
02828     }
02829 }

void DCL_CDECL CLogFile::tinyprintf ( char *  pFormatSpec,
  ... 
)

Definition at line 2617 of file svdhash.cpp.

References mux_vsnprintf(), SIZEOF_LOG_BUFFER, and WriteBuffer().

Referenced by add_comsys(), AssertionFailed(), cache_del(), cache_put(), CF_HAND(), cf_log_notfound(), db_read(), dbconvert(), del_comsys(), CHashFile::FindFirstKey(), get_list(), HelpIndex_Read(), info(), init_dbfile(), CHashFile::Insert(), load_channels(), load_comsys(), load_comsystem(), load_game(), log_name(), LogStatBuf(), main(), make_socket(), OutOfMemory(), pool_alloc(), pool_alloc_lbuf(), pool_err(), pool_free(), pool_free_lbuf(), putbool_subexp(), CHashPage::ReadPage(), ReplaceFile(), report_timecheck(), save_comsys(), sighandler(), CHashPage::Split(), start_log(), write_pidfile(), and CHashPage::WritePage().

02618 {
02619     va_list ap;
02620     va_start(ap, fmt);
02621     char aTempBuffer[SIZEOF_LOG_BUFFER];
02622     int nString = mux_vsnprintf(aTempBuffer, SIZEOF_LOG_BUFFER, fmt, ap);
02623     va_end(ap);
02624     WriteBuffer(nString, aTempBuffer);
02625 }

void CLogFile::WriteBuffer ( size_t  nString,
const char *  pString 
)

Definition at line 2571 of file svdhash.cpp.

References bEnabled, Flush(), m_aBuffer, m_nBuffer, and SIZEOF_LOG_BUFFER.

Referenced by log_printf(), tinyprintf(), WriteInteger(), and WriteString().

02572 {
02573     if (!bEnabled)
02574     {
02575         return;
02576     }
02577 
02578 #ifdef WIN32
02579     EnterCriticalSection(&csLog);
02580 #endif // WIN32
02581 
02582     while (nString > 0)
02583     {
02584         size_t nAvailable = SIZEOF_LOG_BUFFER - m_nBuffer;
02585         if (nAvailable == 0)
02586         {
02587             Flush();
02588             continue;
02589         }
02590 
02591         size_t nToMove = nAvailable;
02592         if (nString < nToMove)
02593         {
02594             nToMove = nString;
02595         }
02596 
02597         // Move nToMove bytes from pString to aBuffer+nBuffer
02598         //
02599         memcpy(m_aBuffer+m_nBuffer, pString, nToMove);
02600         pString += nToMove;
02601         nString -= nToMove;
02602         m_nBuffer += nToMove;
02603     }
02604     Flush();
02605 
02606 #ifdef WIN32
02607     LeaveCriticalSection(&csLog);
02608 #endif // WIN32
02609 }

void CLogFile::WriteInteger ( int  iNumber  ) 

Definition at line 2564 of file svdhash.cpp.

References mux_ltoa(), and WriteBuffer().

Referenced by log_number().

02565 {
02566     char aTempBuffer[20];
02567     size_t nTempBuffer = mux_ltoa(iNumber, aTempBuffer);
02568     WriteBuffer(nTempBuffer, aTempBuffer);
02569 }

void CLogFile::WriteString ( const char *  pString  ) 

Definition at line 2611 of file svdhash.cpp.

References WriteBuffer().

Referenced by db_read(), db_write(), dbconvert(), dup_bool(), end_log(), eval_boolexp(), CHashTable::FindFirstKey(), CHashFile::FindFirstKey(), info(), CHashTable::Insert(), CHashFile::Insert(), CHashPage::Insert(), load_malias(), log_name(), log_perror(), log_text(), main(), CHashFile::ReadCache(), CHashFile::RebuildDirectory(), CHashPage::Split(), start_log(), CHashFile::Sync(), and unparse_boolexp1().

02612 {
02613     size_t nString = strlen(pString);
02614     WriteBuffer(nString, pString);
02615 }


Field Documentation

bool CLogFile::bEnabled [private]

Definition at line 301 of file svdhash.h.

Referenced by CLogFile(), Flush(), SetPrefix(), StartLogging(), StopLogging(), and WriteBuffer().

bool CLogFile::bUseStderr [private]

Definition at line 302 of file svdhash.h.

Referenced by CLogFile(), Flush(), SetBasename(), SetPrefix(), StartLogging(), and StopLogging().

char CLogFile::m_aBuffer[SIZEOF_LOG_BUFFER] [private]

Definition at line 300 of file svdhash.h.

Referenced by Flush(), and WriteBuffer().

HANDLE CLogFile::m_hFile [private]

Definition at line 297 of file svdhash.h.

Referenced by AppendLogFile(), CLogFile(), CloseLogFile(), CreateLogFile(), and Flush().

CLinearTimeAbsolute CLogFile::m_ltaStarted [private]

Definition at line 293 of file svdhash.h.

Referenced by CLogFile(), Flush(), SetPrefix(), and StartLogging().

size_t CLogFile::m_nBuffer [private]

Definition at line 299 of file svdhash.h.

Referenced by CLogFile(), Flush(), and WriteBuffer().

size_t CLogFile::m_nSize [private]

Definition at line 298 of file svdhash.h.

Referenced by CLogFile(), CreateLogFile(), and Flush().

char* CLogFile::m_pBasename [private]

Definition at line 303 of file svdhash.h.

Referenced by CLogFile(), Flush(), SetBasename(), SetPrefix(), and StartLogging().

char CLogFile::m_szFilename[SIZEOF_PATHNAME] [private]

Definition at line 305 of file svdhash.h.

Referenced by AppendLogFile(), CLogFile(), CreateLogFile(), Flush(), SetPrefix(), StartLogging(), and StopLogging().

char CLogFile::m_szPrefix[32] [private]

Definition at line 304 of file svdhash.h.

Referenced by CLogFile(), Flush(), SetPrefix(), StartLogging(), and StopLogging().


The documentation for this class was generated from the following files:
Generated on Mon May 28 04:40:24 2007 for MUX by  doxygen 1.4.7