FileLog.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "FileLog.h"
namespace FIX
{
Log* FileLogFactory::create()
{
m_globalLogCount++;
if( m_globalLogCount > 1 ) return m_globalLog;
try
{
if ( m_path.size() ) return new FileLog( m_path );
std::string path;
std::string backupPath;
Dictionary settings = m_settings.get();
path = settings.getString( FILE_LOG_PATH );
backupPath = path;
if( settings.has( FILE_LOG_BACKUP_PATH ) )
backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
return m_globalLog = new FileLog( path, backupPath );
}
catch( ConfigError& )
{
m_globalLogCount--;
throw;
}
}
Log* FileLogFactory::create( const SessionID& s )
{
if ( m_path.size() && m_backupPath.size() )
return new FileLog( m_path, m_backupPath, s );
if ( m_path.size() )
return new FileLog( m_path, s );
std::string path;
std::string backupPath;
Dictionary settings = m_settings.get( s );
path = settings.getString( FILE_LOG_PATH );
backupPath = path;
if( settings.has( FILE_LOG_BACKUP_PATH ) )
backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
return new FileLog( path, backupPath, s );
}
void FileLogFactory::destroy( Log* pLog )
{
if( pLog == m_globalLog )
{
m_globalLogCount--;
if( m_globalLogCount == 0 )
{
delete pLog;
m_globalLogCount = 0;
}
}
else
{
delete pLog;
}
}
FileLog::FileLog( const std::string& path )
: m_millisecondsInTimeStamp( true )
{
init( path, path, "GLOBAL" );
}
FileLog::FileLog( const std::string& path, const std::string& backupPath )
: m_millisecondsInTimeStamp( true )
{
init( path, backupPath, "GLOBAL" );
}
FileLog::FileLog( const std::string& path, const SessionID& s )
: m_millisecondsInTimeStamp( true )
{
init( path, path, generatePrefix(s) );
}
FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
: m_millisecondsInTimeStamp( true )
{
init( path, backupPath, generatePrefix(s) );
}
std::string FileLog::generatePrefix( const SessionID& s )
{
const std::string& begin =
s.getBeginString().getString();
const std::string& sender =
s.getSenderCompID().getString();
const std::string& target =
s.getTargetCompID().getString();
const std::string& qualifier =
s.getSessionQualifier();
std::string prefix = begin + "-" + sender + "-" + target;
if( qualifier.size() )
prefix += "-" + qualifier;
return prefix;
}
void FileLog::init( std::string path, std::string backupPath, const std::string& prefix )
{
file_mkdir( path.c_str() );
file_mkdir( backupPath.c_str() );
if ( path.empty() ) path = ".";
if ( backupPath.empty() ) backupPath = path;
m_fullPrefix
= file_appendpath(path, prefix + ".");
m_fullBackupPrefix
= file_appendpath(backupPath, prefix + ".");
m_messagesFileName = m_fullPrefix + "messages.current.log";
m_eventFileName = m_fullPrefix + "event.current.log";
m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::app );
if ( !m_messages.is_open() ) throw ConfigError( "Could not open messages file: " + m_messagesFileName );
m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::app );
if ( !m_event.is_open() ) throw ConfigError( "Could not open event file: " + m_eventFileName );
}
FileLog::~FileLog()
{
m_messages.close();
m_event.close();
}
void FileLog::clear()
{
m_messages.close();
m_event.close();
m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
}
void FileLog::backup()
{
m_messages.close();
m_event.close();
int i = 0;
while( true )
{
std::stringstream messagesFileName;
std::stringstream eventFileName;
messagesFileName << m_fullBackupPrefix << "messages.backup." << ++i << ".log";
eventFileName << m_fullBackupPrefix << "event.backup." << i << ".log";
FILE* messagesLogFile = file_fopen( messagesFileName.str().c_str(), "r" );
FILE* eventLogFile = file_fopen( eventFileName.str().c_str(), "r" );
if( messagesLogFile == NULL && eventLogFile == NULL )
{
file_rename( m_messagesFileName.c_str(), messagesFileName.str().c_str() );
file_rename( m_eventFileName.c_str(), eventFileName.str().c_str() );
m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
return;
}
if( messagesLogFile != NULL ) file_fclose( messagesLogFile );
if( eventLogFile != NULL ) file_fclose( eventLogFile );
}
}
} //namespace FIX
FIX::FileLogFactory::m_path
std::string m_path
Definition: FileLog.h:56
FIX::SessionID::getSessionQualifier
const std::string & getSessionQualifier() const
Definition: SessionID.h:59
FIX::FileLog
File based implementation of Log.
Definition: FileLog.h:70
FIX::FileLog::m_fullPrefix
std::string m_fullPrefix
Definition: FileLog.h:106
FIX::FileLog::FileLog
FileLog(const std::string &path)
Definition: FileLog.cpp:91
FIX::FileLogFactory::m_globalLogCount
int m_globalLogCount
Definition: FileLog.h:60
FIX::FileLog::m_messages
std::ofstream m_messages
Definition: FileLog.h:102
FIX::Dictionary::has
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:150
FIX::file_appendpath
std::string file_appendpath(const std::string &path, const std::string &file)
Definition: Utility.cpp:529
FIX::SessionID::getTargetCompID
const TargetCompID & getTargetCompID() const
Definition: SessionID.h:57
FIX::FileLogFactory::m_backupPath
std::string m_backupPath
Definition: FileLog.h:57
FIX::FileLogFactory::destroy
void destroy(Log *log)
Definition: FileLog.cpp:74
FIX::FileLog::m_eventFileName
std::string m_eventFileName
Definition: FileLog.h:105
FIX::FILE_LOG_BACKUP_PATH
const char FILE_LOG_BACKUP_PATH[]
Definition: SessionSettings.h:93
FIX::SessionSettings::get
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Definition: SessionSettings.cpp:125
FIX::Log
This interface must be implemented to log messages and events.
Definition: Log.h:81
FIX::file_fclose
void file_fclose(FILE *file)
Definition: Utility.cpp:498
FIX
Definition: Acceptor.cpp:34
FIX::FileLog::m_event
std::ofstream m_event
Definition: FileLog.h:103
FIX::FileLogFactory::m_settings
SessionSettings m_settings
Definition: FileLog.h:58
FIX::FileLog::backup
void backup()
Definition: FileLog.cpp:170
FIX::FileLog::~FileLog
virtual ~FileLog()
Definition: FileLog.cpp:155
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:87
FIX::file_mkdir
void file_mkdir(const char *path)
Definition: Utility.cpp:467
FIX::FileLog::m_fullBackupPrefix
std::string m_fullBackupPrefix
Definition: FileLog.h:107
FIX::file_rename
int file_rename(const char *oldpath, const char *newpath)
Definition: Utility.cpp:524
FIX::FileLogFactory::create
Log * create()
Definition: FileLog.cpp:30
FIX::FileLog::m_millisecondsInTimeStamp
bool m_millisecondsInTimeStamp
Definition: FileLog.h:108
FIX::FileLog::init
void init(std::string path, std::string backupPath, const std::string &prefix)
Definition: FileLog.cpp:133
FIX::SessionID::getBeginString
const BeginString & getBeginString() const
Definition: SessionID.h:53
FIX::file_fopen
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:487
FIX::Dictionary
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
FileLog.h
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
FIX::FILE_LOG_PATH
const char FILE_LOG_PATH[]
Definition: SessionSettings.h:92
FIX::FileLog::generatePrefix
std::string generatePrefix(const SessionID &sessionID)
Definition: FileLog.cpp:115
FIX::FileLog::m_messagesFileName
std::string m_messagesFileName
Definition: FileLog.h:104
FIX::FileLog::clear
void clear()
Definition: FileLog.cpp:161
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:30
FIX::SessionID::getSenderCompID
const SenderCompID & getSenderCompID() const
Definition: SessionID.h:55
FIX::FileLogFactory::m_globalLog
Log * m_globalLog
Definition: FileLog.h:59