FileLog.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_FILELOG_H
  21. #define FIX_FILELOG_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "Log.h"
  26. #include "SessionSettings.h"
  27. #include <fstream>
  28. namespace FIX
  29. {
  30. /**
  31. * Creates a file based implementation of Log
  32. *
  33. * This stores all log events into flat files
  34. */
  35. class FileLogFactory : public LogFactory
  36. {
  37. public:
  38. FileLogFactory( const SessionSettings& settings )
  39. : m_settings( settings ), m_globalLog(0), m_globalLogCount(0) {};
  40. FileLogFactory( const std::string& path )
  41. : m_path( path ), m_backupPath( path ), m_globalLog(0), m_globalLogCount(0) {};
  42. FileLogFactory( const std::string& path, const std::string& backupPath )
  43. : m_path( path ), m_backupPath( backupPath ), m_globalLog(0), m_globalLogCount(0) {};
  44. public:
  45. Log* create();
  46. Log* create( const SessionID& );
  47. void destroy( Log* log );
  48. private:
  49. std::string m_path;
  50. std::string m_backupPath;
  51. SessionSettings m_settings;
  52. Log* m_globalLog;
  53. int m_globalLogCount;
  54. };
  55. /**
  56. * File based implementation of Log
  57. *
  58. * Two files are created by this implementation. One for messages,
  59. * and one for events.
  60. *
  61. */
  62. class FileLog : public Log
  63. {
  64. public:
  65. FileLog( const std::string& path );
  66. FileLog( const std::string& path, const std::string& backupPath );
  67. FileLog( const std::string& path, const SessionID& sessionID );
  68. FileLog( const std::string& path, const std::string& backupPath, const SessionID& sessionID );
  69. virtual ~FileLog();
  70. void clear();
  71. void backup();
  72. void onIncoming( const std::string& value )
  73. { m_messages << UtcTimeStampConvertor::convert(UtcTimeStamp(), m_millisecondsInTimeStamp) << " : " << value << std::endl; }
  74. void onOutgoing( const std::string& value )
  75. { m_messages << UtcTimeStampConvertor::convert(UtcTimeStamp(), m_millisecondsInTimeStamp) << " : " << value << std::endl; }
  76. void onEvent( const std::string& value )
  77. {
  78. UtcTimeStamp now;
  79. m_event << UtcTimeStampConvertor::convert( now, m_millisecondsInTimeStamp )
  80. << " : " << value << std::endl;
  81. }
  82. bool getMillisecondsInTimeStamp() const
  83. { return m_millisecondsInTimeStamp; }
  84. void setMillisecondsInTimeStamp ( bool value )
  85. { m_millisecondsInTimeStamp = value; }
  86. private:
  87. std::string generatePrefix( const SessionID& sessionID );
  88. void init( std::string path, std::string backupPath, const std::string& prefix );
  89. std::ofstream m_messages;
  90. std::ofstream m_event;
  91. std::string m_messagesFileName;
  92. std::string m_eventFileName;
  93. std::string m_fullPrefix;
  94. std::string m_fullBackupPrefix;
  95. bool m_millisecondsInTimeStamp;
  96. };
  97. }
  98. #endif //FIX_LOG_H