FileLog.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "FileLog.h"
00027 
00028 namespace FIX
00029 {
00030 Log* FileLogFactory::create()
00031 {
00032   m_globalLogCount++;
00033   if( m_globalLogCount > 1 ) return m_globalLog;
00034 
00035   try
00036   {
00037     if ( m_path.size() ) return new FileLog( m_path );
00038     std::string path;
00039     std::string backupPath;
00040 
00041     Dictionary settings = m_settings.get();
00042     path = settings.getString( FILE_LOG_PATH );
00043     backupPath = path;
00044     if( settings.has( FILE_LOG_BACKUP_PATH ) )
00045       backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
00046 
00047     return m_globalLog = new FileLog( path, backupPath );
00048   }
00049   catch( ConfigError& )
00050   {
00051   m_globalLogCount--;
00052   throw;  
00053   }
00054 }
00055 
00056 Log* FileLogFactory::create( const SessionID& s )
00057 {
00058   if ( m_path.size() && m_backupPath.size() )
00059     return new FileLog( m_path, m_backupPath, s );
00060   if ( m_path.size() ) 
00061     return new FileLog( m_path, s );
00062 
00063   std::string path;
00064   std::string backupPath;
00065   Dictionary settings = m_settings.get( s );
00066   path = settings.getString( FILE_LOG_PATH );
00067   backupPath = path;
00068   if( settings.has( FILE_LOG_BACKUP_PATH ) )
00069     backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
00070 
00071   return new FileLog( path, backupPath, s );
00072 }
00073 
00074 void FileLogFactory::destroy( Log* pLog )
00075 {
00076   if( pLog == m_globalLog )
00077   {
00078     m_globalLogCount--;
00079     if( m_globalLogCount == 0 )
00080     {
00081       delete pLog;
00082       m_globalLogCount = 0;
00083     }  
00084   }
00085   else
00086   {
00087     delete pLog;
00088   }
00089 }
00090 
00091 FileLog::FileLog( const std::string& path )
00092 : m_millisecondsInTimeStamp( true )
00093 {
00094   init( path, path, "GLOBAL" );
00095 }
00096 
00097 FileLog::FileLog( const std::string& path, const std::string& backupPath )
00098 : m_millisecondsInTimeStamp( true )
00099 {
00100   init( path, backupPath, "GLOBAL" );
00101 }
00102 
00103 FileLog::FileLog( const std::string& path, const SessionID& s )
00104 : m_millisecondsInTimeStamp( true )
00105 {
00106   init( path, path, generatePrefix(s) );
00107 }
00108 
00109 FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
00110 : m_millisecondsInTimeStamp( true )
00111 {
00112   init( path, backupPath, generatePrefix(s) );
00113 }
00114 
00115 std::string FileLog::generatePrefix( const SessionID& s )
00116 {
00117   const std::string& begin =
00118     s.getBeginString().getString();
00119   const std::string& sender =
00120     s.getSenderCompID().getString();
00121   const std::string& target =
00122     s.getTargetCompID().getString();
00123   const std::string& qualifier =
00124     s.getSessionQualifier();
00125 
00126   std::string prefix = begin + "-" + sender + "-" + target;
00127   if( qualifier.size() )
00128     prefix += "-" + qualifier;
00129 
00130   return prefix;
00131 }
00132 
00133 void FileLog::init( std::string path, std::string backupPath, const std::string& prefix )
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 
00155 FileLog::~FileLog()
00156 {
00157   m_messages.close();
00158   m_event.close();
00159 }
00160 
00161 void FileLog::clear()
00162 {
00163   m_messages.close();
00164   m_event.close();
00165 
00166   m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
00167   m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
00168 }
00169 
00170 void FileLog::backup()
00171 {
00172   m_messages.close();
00173   m_event.close();
00174 
00175   int i = 0;
00176   while( true )
00177   {
00178     std::stringstream messagesFileName;
00179     std::stringstream eventFileName;
00180  
00181     messagesFileName << m_fullBackupPrefix << "messages.backup." << ++i << ".log";
00182     eventFileName << m_fullBackupPrefix << "event.backup." << i << ".log";
00183     FILE* messagesLogFile = file_fopen( messagesFileName.str().c_str(), "r" );
00184     FILE* eventLogFile = file_fopen( eventFileName.str().c_str(), "r" );
00185 
00186     if( messagesLogFile == NULL && eventLogFile == NULL )
00187     {
00188       file_rename( m_messagesFileName.c_str(), messagesFileName.str().c_str() );
00189       file_rename( m_eventFileName.c_str(), eventFileName.str().c_str() );
00190       m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
00191       m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
00192       return;
00193     }
00194     
00195     if( messagesLogFile != NULL ) file_fclose( messagesLogFile );
00196     if( eventLogFile != NULL ) file_fclose( eventLogFile );
00197   }
00198 }
00199 
00200 } //namespace FIX

Generated on Mon Jun 23 2014 23:49:37 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001