Trace.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @file Trace.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.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_UTILS_TRACE
  26. #define COMMON_UTILS_TRACE 1
  27. #include <algorithm>
  28. #include <iterator>
  29. #include <memory>
  30. #include <mutex>
  31. #include <sstream>
  32. #include <string>
  33. #include <vector>
  34. namespace artis {
  35. namespace common {
  36. enum TraceType {
  37. NONE = 0, I_MESSAGE, POST_EVENT, S_MESSAGE, Y_MESSAGE,
  38. DELTA_INT, DELTA_EXT, DELTA_CONF, TA, LAMBDA, START, OUTPUT
  39. };
  40. template<class Time>
  41. class TraceElement {
  42. public:
  43. TraceElement()
  44. :_time(Time::null), _type(NONE) { }
  45. TraceElement(const std::string& model_name, typename Time::type time, TraceType type)
  46. :_model_name(model_name), _time(time), _type(type) { }
  47. virtual ~TraceElement() { }
  48. const std::string& get_comment() const { return _comment; }
  49. const std::string& get_model_name() const { return _model_name; }
  50. typename Time::type get_time() const { return _time; }
  51. TraceType get_type() const { return _type; }
  52. void set_comment(const std::string& comment) { _comment = comment; }
  53. private:
  54. std::string _model_name;
  55. typename Time::type _time;
  56. TraceType _type;
  57. std::string _comment;
  58. };
  59. template<class Time>
  60. class TraceElements : public std::vector<TraceElement<Time> > {
  61. public:
  62. TraceElements() { }
  63. virtual ~TraceElements() { }
  64. TraceElements filter_model_name(const std::string& model_name) const
  65. {
  66. TraceElements<Time> result;
  67. std::copy_if(TraceElements<Time>::begin(),
  68. TraceElements<Time>::end(), std::back_inserter(result),
  69. [model_name](TraceElement<Time> const& x) { return x.get_model_name() == model_name; });
  70. return result;
  71. }
  72. TraceElements filter_time(typename Time::type time) const
  73. {
  74. TraceElements result;
  75. std::copy_if(TraceElements<Time>::begin(),
  76. TraceElements<Time>::end(), std::back_inserter(result),
  77. [time](TraceElement<Time> const& x) { return x.get_time() == time; });
  78. return result;
  79. }
  80. TraceElements filter_type(TraceType type) const
  81. {
  82. TraceElements result;
  83. std::copy_if(TraceElements<Time>::begin(),
  84. TraceElements<Time>::end(), std::back_inserter(result),
  85. [type](TraceElement<Time> const& x) { return x.get_type() == type; });
  86. return result;
  87. }
  88. std::string to_string() const
  89. {
  90. std::ostringstream ss;
  91. for (typename TraceElements<Time>::const_iterator it = TraceElements<Time>::begin();
  92. it != TraceElements<Time>::end(); ++it) {
  93. ss << "TRACE: " << it->get_model_name() << " at " << it->get_time() << " <";
  94. switch (it->get_type()) {
  95. case NONE:
  96. ss << "none";
  97. break;
  98. case I_MESSAGE:
  99. ss << "i_message";
  100. break;
  101. case POST_EVENT:
  102. ss << "post_event";
  103. break;
  104. case S_MESSAGE:
  105. ss << "s_message";
  106. break;
  107. case Y_MESSAGE:
  108. ss << "y_message";
  109. break;
  110. case DELTA_INT:
  111. ss << "delta_int";
  112. break;
  113. case DELTA_EXT:
  114. ss << "delta_ext";
  115. break;
  116. case DELTA_CONF:
  117. ss << "delta_conf";
  118. break;
  119. case TA:
  120. ss << "ta";
  121. break;
  122. case LAMBDA:
  123. ss << "lambda";
  124. break;
  125. case START:
  126. ss << "start";
  127. break;
  128. case OUTPUT:
  129. ss << "output";
  130. break;
  131. };
  132. ss << ">";
  133. if (not it->get_comment().empty()) {
  134. ss << " => " << it->get_comment();
  135. }
  136. ss << std::endl;
  137. }
  138. return ss.str();
  139. }
  140. };
  141. template<class Time>
  142. class Trace {
  143. public:
  144. virtual ~Trace() { }
  145. static Trace& trace()
  146. {
  147. std::call_once(_flag, []() { _instance.reset(new Trace()); });
  148. return *_instance;
  149. }
  150. void clear() { _trace.clear(); }
  151. const TraceElements<Time>& elements() const { return _trace; }
  152. void flush()
  153. {
  154. std::lock_guard<std::mutex> lock(_mutex);
  155. if (_sstream) {
  156. _element.set_comment(_sstream->str());
  157. delete _sstream;
  158. _sstream = 0;
  159. }
  160. _trace.push_back(_element);
  161. }
  162. std::mutex& mutex() { return _mutex; }
  163. void set_element(const TraceElement<Time>& element) { _element = element; }
  164. std::ostringstream& sstream()
  165. {
  166. if (_sstream == 0) {
  167. _sstream = new std::ostringstream();
  168. }
  169. return *_sstream;
  170. }
  171. private:
  172. Trace() { _sstream = 0; }
  173. static std::shared_ptr<Trace<Time> > _instance;
  174. static std::once_flag _flag;
  175. TraceElements<Time> _trace;
  176. TraceElement<Time> _element;
  177. std::ostringstream* _sstream;
  178. std::mutex _mutex;
  179. };
  180. }
  181. } // namespace artis common
  182. template<class Time>
  183. artis::common::Trace<Time>& operator<<(artis::common::Trace<Time>& trace, const artis::common::TraceElement<Time>& e)
  184. {
  185. std::lock_guard<std::mutex> lock(trace.mutex());
  186. trace.set_element(e);
  187. return trace;
  188. }
  189. template<class Time>
  190. artis::common::Trace<Time>& operator<<(artis::common::Trace<Time>& trace, const std::string& str)
  191. {
  192. std::lock_guard<std::mutex> lock(trace.mutex());
  193. trace.sstream() << str;
  194. return trace;
  195. }
  196. template<class Time>
  197. artis::common::Trace<Time>& operator<<(artis::common::Trace<Time>& trace, typename Time::type t)
  198. {
  199. std::lock_guard<std::mutex> lock(trace.mutex());
  200. trace.sstream() << t;
  201. return trace;
  202. }
  203. template<class Time>
  204. std::shared_ptr<artis::common::Trace<Time> > artis::common::Trace<Time>::_instance = nullptr;
  205. template<class Time>
  206. std::once_flag artis::common::Trace<Time>::_flag;
  207. #endif