Go to the documentation of this file.00001 #ifndef __LOG_STREAM_CPP__
00002 #define __LOG_STREAM_CPP__
00003
00004 #include <utilities/log_stream.h>
00005 #include <utilities/testing_utilities.h>
00006
00007 namespace nitro
00008 {
00009
00010 LogStream::LogStream( const char * theLogFilePath , std::size_t theSyncMode )
00011 {
00012 try
00013 {
00014 if( theLogFilePath == NULL )
00015 {
00016 LogFilePath = "./system.log";
00017 }
00018 else
00019 {
00020 LogFilePath = theLogFilePath;
00021 }
00022 SyncMode = theSyncMode;
00023 LogMutex = NULL;
00024 }
00025 catch( nitro::exception e )
00026 {
00027 throw( nitro::exception( std::string( "LogStream::LogStream( const char * theLogFilePath /* = NULL */ , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::" ) + e.what() , e.code() ) );
00028 }
00029 catch( ... )
00030 {
00031 throw( nitro::exception( std::string( "LogStream::LogStream( const char * theLogFilePath /* = NULL */ , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::An error occured" ) , 0 ) );
00032 }
00033 }
00034
00035 LogStream::LogStream( const std::string & theLogFilePath , std::size_t theSyncMode )
00036 {
00037 try
00038 {
00039 if( theLogFilePath == "" )
00040 {
00041 LogFilePath = "./system.log";
00042 }
00043 else
00044 {
00045 LogFilePath = theLogFilePath;
00046 }
00047 SyncMode = theSyncMode;
00048 LogMutex = NULL;
00049 }
00050 catch( nitro::exception e )
00051 {
00052 throw( nitro::exception( std::string( "LogStream::LogStream( const std::string & theLogFilePath , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::" ) + e.what() , e.code() ) );
00053 }
00054 catch( ... )
00055 {
00056 throw( nitro::exception( std::string( "LogStream::LogStream( const std::string & theLogFilePath , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::An error occured" ) , 0 ) );
00057 }
00058 }
00059
00060 LogStream & LogStream::operator<<( const char * LogString )
00061 {
00062 try
00063 {
00064 if( SyncMode != 2 )
00065 {
00066 Reset( LogFilePath.c_str() , SyncMode );
00067 SyncMode = 2;
00068 }
00069
00070 if( LogMutex )
00071 {
00072 LogMutex->Lock();
00073 }
00074
00075 FileAccessObject.Write( LogString , strlen( LogString ) );
00076
00077 if( LogMutex )
00078 {
00079 LogMutex->UnLock();
00080 }
00081
00082 return( * this );
00083 }
00084 catch( nitro::exception e )
00085 {
00086 throw( nitro::exception( std::string( "LogStream::operator<<( const char * LogString )::" ) + e.what() , e.code() ) );
00087 }
00088 catch( ... )
00089 {
00090 throw( nitro::exception( std::string( "LogStream::operator<<( const char * LogString )::An error occured" ) , 0 ) );
00091 }
00092 }
00093
00094 LogStream & LogStream::operator<<( const std::string & LogString )
00095 {
00096 try
00097 {
00098 return( ( * this ) << LogString.c_str() );
00099 }
00100 catch( nitro::exception e )
00101 {
00102 throw( nitro::exception( std::string( "LogStream::operator<<( const std::string & LogString )::" ) + e.what() , e.code() ) );
00103 }
00104 catch( ... )
00105 {
00106 throw( nitro::exception( std::string( "LogStream::operator<<( const std::string & LogString )::An error occured" ) , 0 ) );
00107 }
00108 }
00109
00110 void LogStream::Reset( const char * theLogFilePath , std::size_t theSyncMode )
00111 {
00112 try
00113 {
00114 Release();
00115
00116 if( theLogFilePath == NULL )
00117 {
00118 LogFilePath = "./system.log";
00119 }
00120 else
00121 {
00122 LogFilePath = theLogFilePath;
00123 }
00124 SyncMode = theSyncMode;
00125
00126 if( SyncMode == nitro::SYNC_LOGGING )
00127 {
00128 LogMutex = new nitro::MutexAbstraction();
00129 LogMutex->CreateMutex();
00130 }
00131 else
00132 {
00133 LogMutex = NULL;
00134 }
00135
00136 FileAccessObject.Open( LogFilePath , nitro::FA_FILE_BINARY | nitro::FA_FILE_WRITE | FA_FILE_APPEND );
00137 }
00138 catch( nitro::exception e )
00139 {
00140 throw( nitro::exception( std::string( "LogStream::Reset( const char * LogFilePath /* = NULL */ , std::size_t SyncMode /* = ASYNC_LOGGING */ )::" ) + e.what() , e.code() ) );
00141 }
00142 catch( ... )
00143 {
00144 throw( nitro::exception( std::string( "LogStream::Reset( const char * LogFilePath /* = NULL */ , std::size_t SyncMode /* = ASYNC_LOGGING */ )::An error occured" ) , 0 ) );
00145 }
00146 }
00147
00148 void LogStream::Reset( const std::string & theLogFilePath , std::size_t theSyncMode )
00149 {
00150 try
00151 {
00152 Reset( theLogFilePath.c_str() , theSyncMode );
00153 }
00154 catch( nitro::exception e )
00155 {
00156 throw( nitro::exception( std::string( "LogStream::Reset( const std::string & theLogFilePath /* = NULL */ , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::" ) + e.what() , e.code() ) );
00157 }
00158 catch( ... )
00159 {
00160 throw( nitro::exception( std::string( "LogStream::Reset( const std::string & theLogFilePath /* = NULL */ , std::size_t theSyncMode /* = ASYNC_LOGGING */ )::An error occured" ) , 0 ) );
00161 }
00162 }
00163
00164 void LogStream::Release( void )
00165 {
00166 try
00167 {
00168 if( LogMutex )
00169 {
00170 LogMutex->Release();
00171 delete LogMutex;
00172 LogMutex = NULL;
00173 }
00174
00175 FileAccessObject.Close();
00176 }
00177 catch( nitro::exception e )
00178 {
00179 throw( nitro::exception( std::string( "LogStream::Release( void )::" ) + e.what() , e.code() ) );
00180 }
00181 catch( ... )
00182 {
00183 throw( nitro::exception( std::string( "LogStream::Release( void )::An error occured" ) , 0 ) );
00184 }
00185 }
00186
00187 LogStream::~LogStream()
00188 {
00189 try
00190 {
00191 Release();
00192 }
00193 catch( nitro::exception e )
00194 {
00195 throw( nitro::exception( std::string( "LogStream::~LogStream()::" ) + e.what() , e.code() ) );
00196 }
00197 catch( ... )
00198 {
00199 throw( nitro::exception( std::string( "LogStream::~LogStream()::An error occured" ) , 0 ) );
00200 }
00201 }
00202
00203
00204
00205
00206 BEGIN_TESTING_SECTION()
00207
00208 ENABLE_CLASS_TESTING( LogStream )
00209
00210 #ifdef ENABLE_AUTOTESTING
00211
00212 NITRO_EXPORTING void tstOutputString( const char * ObjectName , const char * Str )
00213 {
00214 LogStreamGetter( ObjectName )<<Str;
00215 }
00216
00217 #endif
00218
00219 CLASS_MEMBER_FUNCTION_TESTING_2( LogStream , aliasReset , tstReset , const char * , std::size_t , void , NO_RET )
00220 CLASS_MEMBER_FUNCTION_TESTING_0( LogStream , Release , tstRelease , void , NO_RET )
00221
00222 END_TESTING_SECTION()
00223
00224 };
00225
00226 #endif