FileLog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 #include "CallStack.h"
00026
00027 #include "FileLog.h"
00028
00029 namespace FIX
00030 {
00031 Log* FileLogFactory::create()
00032 { QF_STACK_PUSH(FileLogFactory::create)
00033
00034 m_globalLogCount++;
00035 if( m_globalLogCount > 1 ) return m_globalLog;
00036
00037 try
00038 {
00039 if ( m_path.size() ) return new FileLog( m_path );
00040 std::string path;
00041 std::string backupPath;
00042
00043 Dictionary settings = m_settings.get();
00044 path = settings.getString( FILE_LOG_PATH );
00045 backupPath = path;
00046 if( settings.has( FILE_LOG_BACKUP_PATH ) )
00047 backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
00048
00049 return m_globalLog = new FileLog( path );
00050 }
00051 catch( ConfigError& )
00052 {
00053 m_globalLogCount--;
00054 throw;
00055 }
00056
00057 QF_STACK_POP
00058 }
00059
00060 Log* FileLogFactory::create( const SessionID& s )
00061 { QF_STACK_PUSH(FileLogFactory::create)
00062
00063 if ( m_path.size() && m_backupPath.size() )
00064 return new FileLog( m_path, m_backupPath, s );
00065 if ( m_path.size() )
00066 return new FileLog( m_path, s );
00067
00068 std::string path;
00069 Dictionary settings = m_settings.get( s );
00070 path = settings.getString( FILE_LOG_PATH );
00071 return new FileLog( path, s );
00072
00073 QF_STACK_POP
00074 }
00075
00076 void FileLogFactory::destroy( Log* pLog )
00077 { QF_STACK_PUSH(FileLogFactory::destroy)
00078
00079 if( pLog == m_globalLog )
00080 {
00081 m_globalLogCount--;
00082 if( m_globalLogCount == 0 )
00083 {
00084 delete pLog;
00085 m_globalLogCount = 0;
00086 }
00087 }
00088 else
00089 {
00090 delete pLog;
00091 }
00092
00093 QF_STACK_POP
00094 }
00095
00096 FileLog::FileLog( const std::string& path )
00097 : m_millisecondsInTimeStamp( true )
00098 {
00099 init( path, path, "GLOBAL" );
00100 }
00101
00102 FileLog::FileLog( const std::string& path, const SessionID& s )
00103 : m_millisecondsInTimeStamp( true )
00104 {
00105 init( path, path, generatePrefix(s) );
00106 }
00107
00108 FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
00109 : m_millisecondsInTimeStamp( true )
00110 {
00111 init( path, backupPath, generatePrefix(s) );
00112 }
00113
00114 std::string FileLog::generatePrefix( const SessionID& s )
00115 {
00116 const std::string& begin =
00117 s.getBeginString().getString();
00118 const std::string& sender =
00119 s.getSenderCompID().getString();
00120 const std::string& target =
00121 s.getTargetCompID().getString();
00122 const std::string& qualifier =
00123 s.getSessionQualifier();
00124
00125 std::string prefix = begin + "-" + sender + "-" + target;
00126 if( qualifier.size() )
00127 prefix += "-" + qualifier;
00128
00129 return prefix;
00130 }
00131
00132 void FileLog::init( std::string path, std::string backupPath, const std::string& prefix )
00133 { QF_STACK_PUSH(FileLog::init)
00134
00135 file_mkdir( path.c_str() );
00136 file_mkdir( backupPath.c_str() );
00137
00138 if ( path.empty() ) path = ".";
00139 if ( backupPath.empty() ) backupPath = path;
00140
00141 m_fullPrefix
00142 = file_appendpath(path, prefix + ".");
00143 m_fullBackupPrefix
00144 = file_appendpath(backupPath, prefix + ".");
00145
00146 m_messagesFileName = m_fullPrefix + "messages.current.log";
00147 m_eventFileName = m_fullPrefix + "event.current.log";
00148
00149 m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::app );
00150 if ( !m_messages.is_open() ) throw ConfigError( "Could not open messages file: " + m_messagesFileName );
00151 m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::app );
00152 if ( !m_event.is_open() ) throw ConfigError( "Could not open event file: " + m_eventFileName );
00153
00154 QF_STACK_POP
00155 }
00156
00157 FileLog::~FileLog()
00158 {
00159 m_messages.close();
00160 m_event.close();
00161 }
00162
00163 void FileLog::clear()
00164 {
00165 m_messages.close();
00166 m_event.close();
00167
00168 m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
00169 m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
00170 }
00171
00172 void FileLog::backup()
00173 {
00174 m_messages.close();
00175 m_event.close();
00176
00177 int i = 0;
00178 while( true )
00179 {
00180 std::stringstream messagesFileName;
00181 std::stringstream eventFileName;
00182
00183 messagesFileName << m_fullBackupPrefix << "messages.backup." << ++i << ".log";
00184 eventFileName << m_fullBackupPrefix << "event.backup." << i << ".log";
00185 FILE* messagesLogFile = file_fopen( messagesFileName.str().c_str(), "r" );
00186 FILE* eventLogFile = file_fopen( eventFileName.str().c_str(), "r" );
00187
00188 if( messagesLogFile == NULL && eventLogFile == NULL )
00189 {
00190 file_rename( m_messagesFileName.c_str(), messagesFileName.str().c_str() );
00191 file_rename( m_eventFileName.c_str(), eventFileName.str().c_str() );
00192 m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
00193 m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
00194 return;
00195 }
00196
00197 if( messagesLogFile != NULL ) file_fclose( messagesLogFile );
00198 if( eventLogFile != NULL ) file_fclose( eventLogFile );
00199 }
00200 }
00201
00202 }