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
28namespace 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
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
75{
76 if( pLog == m_globalLog )
77 {
79 if( m_globalLogCount == 0 )
80 {
81 delete pLog;
83 }
84 }
85 else
86 {
87 delete pLog;
88 }
89}
90
91FileLog::FileLog( const std::string& path )
92{
93 init( path, path, "GLOBAL" );
94}
95
96FileLog::FileLog( const std::string& path, const std::string& backupPath )
97{
98 init( path, backupPath, "GLOBAL" );
99}
100
101FileLog::FileLog( const std::string& path, const SessionID& s )
102{
103 init( path, path, generatePrefix(s) );
104}
105
106FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
107{
108 init( path, backupPath, generatePrefix(s) );
109}
110
111std::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
129void 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
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
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
For storage and retrieval of key/value pairs.
Definition Dictionary.h:37
bool has(const std::string &) const
Check if the dictionary contains a value for key.
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
std::string m_path
Definition FileLog.h:56
std::string m_backupPath
Definition FileLog.h:57
void destroy(Log *log)
Definition FileLog.cpp:74
SessionSettings m_settings
Definition FileLog.h:58
File based implementation of Log.
Definition FileLog.h:71
std::ofstream m_event
Definition FileLog.h:98
std::string m_fullPrefix
Definition FileLog.h:101
std::string generatePrefix(const SessionID &sessionID)
Definition FileLog.cpp:111
std::string m_eventFileName
Definition FileLog.h:100
std::string m_fullBackupPrefix
Definition FileLog.h:102
void clear()
Definition FileLog.cpp:157
void init(std::string path, std::string backupPath, const std::string &prefix)
Definition FileLog.cpp:129
virtual ~FileLog()
Definition FileLog.cpp:151
std::ofstream m_messages
Definition FileLog.h:97
std::string m_messagesFileName
Definition FileLog.h:99
void backup()
Definition FileLog.cpp:166
FileLog(const std::string &path)
Definition FileLog.cpp:91
This interface must be implemented to log messages and events.
Definition Log.h:82
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition SessionID.h:31
const SenderCompID & getSenderCompID() const
Definition SessionID.h:55
const std::string & getSessionQualifier() const
Definition SessionID.h:59
const BeginString & getBeginString() const
Definition SessionID.h:53
const TargetCompID & getTargetCompID() const
Definition SessionID.h:57
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
int file_rename(const char *oldpath, const char *newpath)
Definition Utility.cpp:546
void file_mkdir(const char *path)
Definition Utility.cpp:489
void file_fclose(FILE *file)
Definition Utility.cpp:520
const char FILE_LOG_PATH[]
const char FILE_LOG_BACKUP_PATH[]
FILE * file_fopen(const char *path, const char *mode)
Definition Utility.cpp:509
std::string file_appendpath(const std::string &path, const std::string &file)
Definition Utility.cpp:551
Application is not configured correctly
Definition Exceptions.h:88

Generated on Thu May 22 2025 08:23:50 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001