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 
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;
82  m_globalLogCount = 0;
83  }
84  }
85  else
86  {
87  delete pLog;
88  }
89 }
90 
91 FileLog::FileLog( const std::string& path )
92 : m_millisecondsInTimeStamp( true )
93 {
94  init( path, path, "GLOBAL" );
95 }
96 
97 FileLog::FileLog( const std::string& path, const std::string& backupPath )
99 {
100  init( path, backupPath, "GLOBAL" );
101 }
102 
103 FileLog::FileLog( const std::string& path, const SessionID& s )
105 {
106  init( path, path, generatePrefix(s) );
107 }
108 
109 FileLog::FileLog( const std::string& path, const std::string& backupPath, const SessionID& s )
111 {
112  init( path, backupPath, generatePrefix(s) );
113 }
114 
115 std::string FileLog::generatePrefix( const SessionID& s )
116 {
117  const std::string& begin =
118  s.getBeginString().getString();
119  const std::string& sender =
120  s.getSenderCompID().getString();
121  const std::string& target =
122  s.getTargetCompID().getString();
123  const std::string& qualifier =
125 
126  std::string prefix = begin + "-" + sender + "-" + target;
127  if( qualifier.size() )
128  prefix += "-" + qualifier;
129 
130  return prefix;
131 }
132 
133 void FileLog::init( std::string path, std::string backupPath, const std::string& prefix )
134 {
135  file_mkdir( path.c_str() );
136  file_mkdir( backupPath.c_str() );
137 
138  if ( path.empty() ) path = ".";
139  if ( backupPath.empty() ) backupPath = path;
140 
142  = file_appendpath(path, prefix + ".");
144  = file_appendpath(backupPath, prefix + ".");
145 
146  m_messagesFileName = m_fullPrefix + "messages.current.log";
147  m_eventFileName = m_fullPrefix + "event.current.log";
148 
149  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::app );
150  if ( !m_messages.is_open() ) throw ConfigError( "Could not open messages file: " + m_messagesFileName );
151  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::app );
152  if ( !m_event.is_open() ) throw ConfigError( "Could not open event file: " + m_eventFileName );
153 }
154 
156 {
157  m_messages.close();
158  m_event.close();
159 }
160 
162 {
163  m_messages.close();
164  m_event.close();
165 
166  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
167  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
168 }
169 
171 {
172  m_messages.close();
173  m_event.close();
174 
175  int i = 0;
176  while( true )
177  {
178  std::stringstream messagesFileName;
179  std::stringstream eventFileName;
180 
181  messagesFileName << m_fullBackupPrefix << "messages.backup." << ++i << ".log";
182  eventFileName << m_fullBackupPrefix << "event.backup." << i << ".log";
183  FILE* messagesLogFile = file_fopen( messagesFileName.str().c_str(), "r" );
184  FILE* eventLogFile = file_fopen( eventFileName.str().c_str(), "r" );
185 
186  if( messagesLogFile == NULL && eventLogFile == NULL )
187  {
188  file_rename( m_messagesFileName.c_str(), messagesFileName.str().c_str() );
189  file_rename( m_eventFileName.c_str(), eventFileName.str().c_str() );
190  m_messages.open( m_messagesFileName.c_str(), std::ios::out | std::ios::trunc );
191  m_event.open( m_eventFileName.c_str(), std::ios::out | std::ios::trunc );
192  return;
193  }
194 
195  if( messagesLogFile != NULL ) file_fclose( messagesLogFile );
196  if( eventLogFile != NULL ) file_fclose( eventLogFile );
197  }
198 }
199 
200 } //namespace FIX
std::string m_path
Definition: FileLog.h:56
const std::string & getSessionQualifier() const
Definition: SessionID.h:59
File based implementation of Log.
Definition: FileLog.h:70
std::string m_fullPrefix
Definition: FileLog.h:106
FileLog(const std::string &path)
Definition: FileLog.cpp:91
std::ofstream m_messages
Definition: FileLog.h:102
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:150
std::string file_appendpath(const std::string &path, const std::string &file)
Definition: Utility.cpp:529
const TargetCompID & getTargetCompID() const
Definition: SessionID.h:57
std::string m_backupPath
Definition: FileLog.h:57
void destroy(Log *log)
Definition: FileLog.cpp:74
std::string m_eventFileName
Definition: FileLog.h:105
const char FILE_LOG_BACKUP_PATH[]
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
This interface must be implemented to log messages and events.
Definition: Log.h:81
void file_fclose(FILE *file)
Definition: Utility.cpp:498
Definition: Acceptor.cpp:34
std::ofstream m_event
Definition: FileLog.h:103
SessionSettings m_settings
Definition: FileLog.h:58
void backup()
Definition: FileLog.cpp:170
virtual ~FileLog()
Definition: FileLog.cpp:155
Application is not configured correctly
Definition: Exceptions.h:87
void file_mkdir(const char *path)
Definition: Utility.cpp:467
std::string m_fullBackupPrefix
Definition: FileLog.h:107
int file_rename(const char *oldpath, const char *newpath)
Definition: Utility.cpp:524
bool m_millisecondsInTimeStamp
Definition: FileLog.h:108
void init(std::string path, std::string backupPath, const std::string &prefix)
Definition: FileLog.cpp:133
const BeginString & getBeginString() const
Definition: SessionID.h:53
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:487
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
const char FILE_LOG_PATH[]
std::string generatePrefix(const SessionID &sessionID)
Definition: FileLog.cpp:115
std::string m_messagesFileName
Definition: FileLog.h:104
void clear()
Definition: FileLog.cpp:161
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:30
const SenderCompID & getSenderCompID() const
Definition: SessionID.h:55

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001