00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <UnwrittenLogfile.hpp>
00021 #include <assert.h>
00022 #include <stdarg.h>
00023
00024 UnwrittenLogfile::UnwrittenLogfile( const std::string & szFilename, bool bPrintMessageType )
00025 {
00026
00027 this->m_bDisposed = false;
00028 this->m_bWaitForNewLine = false;
00029 this->m_bPrintMessageType = bPrintMessageType;
00030 this->m_szLogfileName = szFilename;
00031 this->m_eLoggingType = UnwrittenDebug;
00032
00033
00034 if( NULL == ( this->m_pFileHandle = fopen( szFilename.c_str(), "w+" ) ) )
00035 throw new std::exception( std::string( "Cannot create the requested logfile '" + szFilename + "'. Maybe you have no rights to write to the given location!" ).c_str() );
00036 }
00037
00038 UnwrittenLogfile::~UnwrittenLogfile()
00039 {
00040
00041
00042
00043 assert( true == this->m_bDisposed );
00044
00045
00046
00047 if( !this->m_bDisposed )
00048 this->Dispose();
00049 }
00050
00051 void UnwrittenLogfile::Dispose()
00052 {
00053
00054 if( this->m_pFileHandle != NULL )
00055 {
00056 fflush( this->m_pFileHandle );
00057 fclose( this->m_pFileHandle );
00058 this->m_pFileHandle = NULL;
00059 }
00060
00061
00062 this->m_bDisposed = true;
00063 }
00064
00065 UnwrittenLogfile::UnwrittenLoggingType UnwrittenLogfile::GetLoggingType()
00066 {
00067 return( this->m_eLoggingType );
00068 }
00069
00070 void UnwrittenLogfile::SetLoggingType( UnwrittenLoggingType newLoggingType )
00071 {
00072 this->m_eLoggingType = newLoggingType;
00073 }
00074
00075 void UnwrittenLogfile::PrintMessageType( UnwrittenLoggingType typeOfMessage )
00076 {
00077 switch( typeOfMessage )
00078 {
00079 case UnwrittenDebug:
00080 fprintf( this->m_pFileHandle, "[DEBUG] " );
00081 break;
00082 case UnwrittenInformation:
00083 fprintf( this->m_pFileHandle, "[INFORMATION] " );
00084 break;
00085 case UnwrittenWarning:
00086 fprintf( this->m_pFileHandle, "[WARNING] " );
00087 break;
00088 case UnwrittenError:
00089 fprintf( this->m_pFileHandle, "[ERROR] " );
00090 break;
00091 }
00092 }
00093
00094 void UnwrittenLogfile::Write( UnwrittenLoggingType typeOfMsg, const char * szText, ... )
00095 {
00096
00097 va_list ap;
00098
00099
00100 assert( this->m_pFileHandle != NULL );
00101
00102
00103 if( typeOfMsg >= this->m_eLoggingType )
00104 {
00105
00106 if( this->m_bPrintMessageType && !this->m_bWaitForNewLine )
00107 this->PrintMessageType( typeOfMsg );
00108
00109
00110 va_start( ap, szText );
00111 vfprintf( this->m_pFileHandle, szText, ap );
00112 va_end( ap );
00113
00114
00115 this->m_bWaitForNewLine = true;
00116 }
00117 }
00118
00119 void UnwrittenLogfile::WriteLine( UnwrittenLoggingType typeOfMsg, const char * szText, ... )
00120 {
00121
00122 va_list ap;
00123
00124
00125 assert( this->m_pFileHandle != NULL );
00126
00127
00128 if( typeOfMsg >= this->m_eLoggingType )
00129 {
00130
00131 if( this->m_bPrintMessageType && !this->m_bWaitForNewLine )
00132 this->PrintMessageType( typeOfMsg );
00133
00134
00135 va_start( ap, szText );
00136 vfprintf( this->m_pFileHandle, szText, ap );
00137 fprintf( this->m_pFileHandle, "\n" );
00138 va_end( ap );
00139 }
00140
00141
00142 this->m_bWaitForNewLine = false;
00143 }
00144
00145 void UnwrittenLogfile::Flush()
00146 {
00147
00148 assert( this->m_pFileHandle != NULL );
00149
00150
00151 fflush( this->m_pFileHandle );
00152 }