FileLog.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "FileLog.h"
27 
28 namespace FIX
29 {
31 {
33  if( m_globalLogCount > 1 ) return m_globalLog;
34 
35  try
36  {
37  if ( m_path.size() ) return new FileLog( m_path );
38  std::string path;
39  std::string backupPath;
40 
41  Dictionary settings = m_settings.get();
42  path = settings.getString( FILE_LOG_PATH );
43  backupPath = path;
44  if( settings.has( FILE_LOG_BACKUP_PATH ) )
45  backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
46 
47  return m_globalLog = new FileLog( path, backupPath );
48  }
49  catch( ConfigError& )
50  {
52  throw;
53  }
54 }
55 
56 Log* FileLogFactory::create( const SessionID& s )
57 {
58  if ( m_path.size() && m_backupPath.size() )
59  return new FileLog( m_path, m_backupPath, s );
60  if ( m_path.size() )
61  return new FileLog( m_path, s );
62 
63  std::string path;
64  std::string backupPath;
65  Dictionary settings = m_settings.get( s );
66  path = settings.getString( FILE_LOG_PATH );
67  backupPath = path;
68  if( settings.has( FILE_LOG_BACKUP_PATH ) )
69  backupPath = settings.getString( FILE_LOG_BACKUP_PATH );
70 
71  return new FileLog( path, backupPath, s );
72 }
73 
74 void FileLogFactory::destroy( Log* pLog )
75 {
76  if( pLog == m_globalLog )
77  {
79  if( m_globalLogCount == 0 )
80  {
81  delete pLog;
82  m_globalLogCount = 0;
83  }
84  }
85  else
86  {
87  delete pLog;
88  }
89 }
90 
91 FileLog::FileLog( const std::string& path )
92 {
93  init( path, path, "GLOBAL" );
94 }
95 
96 FileLog::FileLog( const std::string& path, const std::string& backupPath )
97 {
98  init( path, backupPath, "GLOBAL" );
99 }
100 
101 FileLog::FileLog( const std::string& path, const SessionID& s )
102 {
103  init( path, path, generatePrefix(s) );
104 }
105 
106 FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
107 {
108  init( path, backupPath, generatePrefix(s) );
109 }
110 
111 std::string FileLog::generatePrefix( const SessionID& s )
112 {
113  const std::string& begin =
114  s.getBeginString().getString();
115  const std::string& sender =
116  s.getSenderCompID().getString();
117  const std::string& target =
118  s.getTargetCompID().getString();
119  const std::string& qualifier =
121 
122  std::string prefix = begin + "-" + sender + "-" + target;
123  if( qualifier.size() )
124  prefix += "-" + qualifier;
125 
126  return prefix;
127 }
128 
129 void FileLog::init( std::string path, std::string backupPath, const std::string& prefix )
130 {
131  file_mkdir( path.c_str() );
132  file_mkdir( backupPath.c_str() );
133 
134  if ( path.empty() ) path = ".";
135  if ( backupPath.empty() ) backupPath = path;
136 
138  = file_appendpath(path, prefix + ".");
140  = file_appendpath(backupPath, prefix + ".");
141 
142  m_messagesFileName = m_fullPrefix + "messages.current.log";
143  m_eventFileName = m_fullPrefix + "event.current.log";
144 
145  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::app );
146  if ( !m_messages.is_open() ) throw ConfigError( "Could not open messages file: " + m_messagesFileName );
147  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::app );
148  if ( !m_event.is_open() ) throw ConfigError( "Could not open event file: " + m_eventFileName );
149 }
150 
152 {
153  m_messages.close();
154  m_event.close();
155 }
156 
157 void FileLog::clear()
158 {
159  m_messages.close();
160  m_event.close();
161 
162  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
163  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
164 }
165 
166 void FileLog::backup()
167 {
168  m_messages.close();
169  m_event.close();
170 
171  int i = 0;
172  while( true )
173  {
174  std::stringstream messagesFileName;
175  std::stringstream eventFileName;
176 
177  messagesFileName << m_fullBackupPrefix << "messages.backup." << ++i << ".log";
178  eventFileName << m_fullBackupPrefix << "event.backup." << i << ".log";
179  FILE* messagesLogFile = file_fopen( messagesFileName.str().c_str(), "r" );
180  FILE* eventLogFile = file_fopen( eventFileName.str().c_str(), "r" );
181 
182  if( messagesLogFile == NULL && eventLogFile == NULL )
183  {
184  file_rename( m_messagesFileName.c_str(), messagesFileName.str().c_str() );
185  file_rename( m_eventFileName.c_str(), eventFileName.str().c_str() );
186  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
187  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
188  return;
189  }
190 
191  if( messagesLogFile != NULL ) file_fclose( messagesLogFile );
192  if( eventLogFile != NULL ) file_fclose( eventLogFile );
193  }
194 }
195 
196 } //namespace FIX
FIX::FILE_LOG_BACKUP_PATH
const char FILE_LOG_BACKUP_PATH[]
Definition: SessionSettings.h:114
FIX::FileLog::m_messagesFileName
std::string m_messagesFileName
Definition: FileLog.h:116
FIX::file_appendpath
std::string file_appendpath(const std::string &path, const std::string &file)
Definition: Utility.cpp:568
FIX::FileLog::m_fullPrefix
std::string m_fullPrefix
Definition: FileLog.h:118
FIX::file_fclose
void file_fclose(FILE *file)
Definition: Utility.cpp:537
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:49
FIX::FileLogFactory::m_globalLog
Log * m_globalLog
Definition: FileLog.h:93
FIX::FileLog
File based implementation of Log.
Definition: FileLog.h:87
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:104
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:47
FIX::file_rename
int file_rename(const char *oldpath, const char *newpath)
Definition: Utility.cpp:563
FIX::FileLogFactory::m_globalLogCount
int m_globalLogCount
Definition: FileLog.h:94
FIX::FileLog::m_messages
std::ofstream m_messages
Definition: FileLog.h:114
FIX::SessionID::getSenderCompID
const SenderCompID & getSenderCompID() const
Definition: SessionID.h:89
FIX::FileLogFactory::destroy
void destroy(Log *log)
Definition: FileLog.cpp:91
FIX::FILE_LOG_PATH
const char FILE_LOG_PATH[]
Definition: SessionSettings.h:113
FIX::SessionID::getBeginString
const BeginString & getBeginString() const
Definition: SessionID.h:87
FIX::FileLog::backup
void backup()
Definition: FileLog.cpp:183
FIX::FileLog::~FileLog
virtual ~FileLog()
Definition: FileLog.cpp:168
FIX::FileLog::m_event
std::ofstream m_event
Definition: FileLog.h:115
FIX::FileLogFactory::m_backupPath
std::string m_backupPath
Definition: FileLog.h:91
FileLog.h
FIX::file_mkdir
void file_mkdir(const char *path)
Definition: Utility.cpp:506
FIX
Definition: Acceptor.cpp:34
FIX::FileLog::m_eventFileName
std::string m_eventFileName
Definition: FileLog.h:117
FIX::FileLog::clear
void clear()
Definition: FileLog.cpp:174
FIX::FileLog::generatePrefix
std::string generatePrefix(const SessionID &sessionID)
Definition: FileLog.cpp:128
FIX::SessionID::getTargetCompID
const TargetCompID & getTargetCompID() const
Definition: SessionID.h:91
FIX::SessionID::getSessionQualifier
const std::string & getSessionQualifier() const
Definition: SessionID.h:93
FIX::FileLogFactory::m_settings
SessionSettings m_settings
Definition: FileLog.h:92
FIX::file_fopen
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:526
FIX::FileLog::init
void init(std::string path, std::string backupPath, const std::string &prefix)
Definition: FileLog.cpp:146
FIX::FileLogFactory::create
Log * create()
Definition: FileLog.cpp:47
FIX::FileLog::FileLog
FileLog(const std::string &path)
Definition: FileLog.cpp:108
FIX::SessionSettings::get
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Definition: SessionSettings.cpp:144
FIX::Log
This interface must be implemented to log messages and events.
Definition: Log.h:98
FIX::FileLogFactory::m_path
std::string m_path
Definition: FileLog.h:90
FIX::FileLog::m_fullBackupPrefix
std::string m_fullBackupPrefix
Definition: FileLog.h:119

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001