Log.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_LOG_H
  21. #define FIX_LOG_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "Message.h"
  26. #include "Mutex.h"
  27. #include "SessionSettings.h"
  28. #include <map>
  29. #include <vector>
  30. namespace FIX
  31. {
  32. class Log;
  33. /**
  34. * This interface must be implemented to create a Log.
  35. */
  36. class LogFactory
  37. {
  38. public:
  39. virtual ~LogFactory() {}
  40. virtual Log* create() = 0;
  41. virtual Log* create( const SessionID& ) = 0;
  42. virtual void destroy( Log* ) = 0;
  43. };
  44. /**
  45. * Creates a screen based implementation of Log.
  46. *
  47. * This displays all log events onto the standard output
  48. */
  49. class ScreenLogFactory : public LogFactory
  50. {
  51. public:
  52. ScreenLogFactory( const SessionSettings& settings )
  53. : m_useSettings( true ), m_settings( settings ) {};
  54. ScreenLogFactory( bool incoming, bool outgoing, bool event )
  55. : m_incoming( incoming ), m_outgoing( outgoing ), m_event( event ), m_useSettings( false ) {}
  56. Log* create();
  57. Log* create( const SessionID& );
  58. void destroy( Log* log );
  59. private:
  60. void init( const Dictionary& settings, bool& incoming, bool& outgoing, bool& event );
  61. bool m_incoming;
  62. bool m_outgoing;
  63. bool m_event;
  64. bool m_useSettings;
  65. SessionSettings m_settings;
  66. };
  67. /**
  68. * This interface must be implemented to log messages and events
  69. */
  70. class Log
  71. {
  72. public:
  73. virtual ~Log() {}
  74. virtual void clear() = 0;
  75. virtual void backup() = 0;
  76. virtual void onIncoming( const std::string& ) = 0;
  77. virtual void onOutgoing( const std::string& ) = 0;
  78. virtual void onEvent( const std::string& ) = 0;
  79. };
  80. /*! @} */
  81. /**
  82. * Null implementation of Log
  83. *
  84. * This is only for internal use. Used when no log factory is
  85. * passed to the initiator or acceptor.
  86. */
  87. class NullLog : public Log
  88. {
  89. public:
  90. void clear() {}
  91. void backup() {}
  92. void onIncoming( const std::string& ) {}
  93. void onOutgoing( const std::string& ) {}
  94. void onEvent( const std::string& ) {}
  95. };
  96. /**
  97. * Screen based implementation of Log.
  98. *
  99. * This will display all log information onto the standard output
  100. */
  101. class ScreenLog : public Log
  102. {
  103. public:
  104. ScreenLog( bool incoming, bool outgoing, bool event )
  105. : m_prefix( "GLOBAL" ),
  106. m_incoming( incoming ), m_outgoing( outgoing ), m_event( event ), m_millisecondsInTimeStamp( true ) {}
  107. ScreenLog( const SessionID& sessionID,
  108. bool incoming, bool outgoing, bool event )
  109. : m_prefix( sessionID.toString() ),
  110. m_incoming( incoming ), m_outgoing( outgoing ), m_event( event ), m_millisecondsInTimeStamp( true ) {}
  111. void clear() {}
  112. void backup() {}
  113. void onIncoming( const std::string& value )
  114. {
  115. if ( !m_incoming ) return ;
  116. Locker l( s_mutex );
  117. m_time.setCurrent();
  118. std::cout << "<" << UtcTimeStampConvertor::convert(m_time, m_millisecondsInTimeStamp)
  119. << ", " << m_prefix
  120. << ", " << "incoming>" << std::endl
  121. << " (" << value << ")" << std::endl;
  122. }
  123. void onOutgoing( const std::string& value )
  124. {
  125. if ( !m_outgoing ) return ;
  126. Locker l( s_mutex );
  127. m_time.setCurrent();
  128. std::cout << "<" << UtcTimeStampConvertor::convert(m_time, m_millisecondsInTimeStamp)
  129. << ", " << m_prefix
  130. << ", " << "outgoing>" << std::endl
  131. << " (" << value << ")" << std::endl;
  132. }
  133. void onEvent( const std::string& value )
  134. {
  135. if ( !m_event ) return ;
  136. Locker l( s_mutex );
  137. m_time.setCurrent();
  138. std::cout << "<" << UtcTimeStampConvertor::convert(m_time, m_millisecondsInTimeStamp)
  139. << ", " << m_prefix
  140. << ", " << "event>" << std::endl
  141. << " (" << value << ")" << std::endl;
  142. }
  143. bool getMillisecondsInTimeStamp() const
  144. { return m_millisecondsInTimeStamp; }
  145. void setMillisecondsInTimeStamp ( bool value )
  146. { m_millisecondsInTimeStamp = value; }
  147. private:
  148. std::string m_prefix;
  149. UtcTimeStamp m_time;
  150. bool m_incoming;
  151. bool m_outgoing;
  152. bool m_event;
  153. static Mutex s_mutex;
  154. bool m_millisecondsInTimeStamp;
  155. };
  156. }
  157. #endif //FIX_LOG_H