Trace.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file Trace.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_TRACE
  26. #define COMMON_TRACE 1
  27. #include <common/Time.hpp>
  28. #include <algorithm>
  29. #include <iterator>
  30. #include <sstream>
  31. #include <string>
  32. #include <vector>
  33. namespace paradevs { namespace common {
  34. enum TraceType { NONE = 0, I_MESSAGE, POST_MESSAGE, S_MESSAGE, Y_MESSAGE,
  35. DELTA_INT, DELTA_EXT, DELTA_CONF, TA, LAMBDA, START, OUTPUT };
  36. class TraceElement
  37. {
  38. public:
  39. TraceElement() : _time(0), _type(NONE)
  40. { }
  41. TraceElement(const std::string& model_name, Time time, TraceType type) :
  42. _model_name(model_name), _time(time), _type(type)
  43. { }
  44. virtual ~TraceElement()
  45. { }
  46. const std::string& get_comment() const
  47. { return _comment; }
  48. const std::string& get_model_name() const
  49. { return _model_name; }
  50. Time get_time() const
  51. { return _time; }
  52. TraceType get_type() const
  53. { return _type; }
  54. void set_comment(const std::string& comment)
  55. { _comment = comment; }
  56. private:
  57. std::string _model_name;
  58. Time _time;
  59. TraceType _type;
  60. std::string _comment;
  61. };
  62. class TraceElements : public std::vector < TraceElement >
  63. {
  64. public:
  65. TraceElements()
  66. { }
  67. virtual ~TraceElements()
  68. { }
  69. TraceElements filter_model_name(
  70. const std::string& model_name) const
  71. {
  72. TraceElements result;
  73. std::copy_if(begin(), end(), std::back_inserter(result),
  74. [model_name](TraceElement const & x)
  75. { return x.get_model_name() == model_name; });
  76. return result;
  77. }
  78. TraceElements filter_time(Time time) const
  79. {
  80. TraceElements result;
  81. std::copy_if(begin(), end(), std::back_inserter(result),
  82. [time](TraceElement const & x)
  83. { return x.get_time() == time; });
  84. return result;
  85. }
  86. TraceElements filter_type(TraceType type) const
  87. {
  88. TraceElements result;
  89. std::copy_if(begin(), end(), std::back_inserter(result),
  90. [type](TraceElement const & x)
  91. { return x.get_type() == type; });
  92. return result;
  93. }
  94. std::string to_string() const;
  95. };
  96. class Trace
  97. {
  98. public:
  99. static Trace& trace();
  100. void clear()
  101. { _trace.clear(); }
  102. const TraceElements& elements() const
  103. { return _trace; }
  104. void flush();
  105. void set_element(const TraceElement& element)
  106. { _element = element; }
  107. std::ostringstream& sstream();
  108. private:
  109. Trace()
  110. { _sstream = 0; }
  111. virtual ~Trace()
  112. {
  113. delete _instance;
  114. if (_sstream) {
  115. delete _sstream;
  116. }
  117. }
  118. static Trace* _instance;
  119. TraceElements _trace;
  120. TraceElement _element;
  121. std::ostringstream* _sstream;
  122. };
  123. } } // namespace paradevs common
  124. paradevs::common::Trace& operator<<(paradevs::common::Trace& trace,
  125. const paradevs::common::TraceElement& e);
  126. paradevs::common::Trace& operator<<(paradevs::common::Trace& trace,
  127. const std::string& str);
  128. paradevs::common::Trace& operator<<(paradevs::common::Trace& trace,
  129. paradevs::common::Time t);
  130. #endif