Trace.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2018 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 { namespace common {
  35. enum TraceType { NONE = 0, I_MESSAGE, POST_EVENT, S_MESSAGE, Y_MESSAGE,
  36. DELTA_INT, DELTA_EXT, DELTA_CONF, TA, LAMBDA, START, OUTPUT };
  37. template < class Time >
  38. class TraceElement
  39. {
  40. public:
  41. TraceElement() : _time(Time::null), _type(NONE)
  42. { }
  43. TraceElement(const std::string& model_name, typename Time::type time,
  44. TraceType type) :
  45. _model_name(model_name), _time(time), _type(type)
  46. { }
  47. virtual ~TraceElement()
  48. { }
  49. const std::string& get_comment() const
  50. { return _comment; }
  51. const std::string& get_model_name() const
  52. { return _model_name; }
  53. typename Time::type get_time() const
  54. { return _time; }
  55. TraceType get_type() const
  56. { return _type; }
  57. void set_comment(const std::string& comment)
  58. { _comment = comment; }
  59. private:
  60. std::string _model_name;
  61. typename Time::type _time;
  62. TraceType _type;
  63. std::string _comment;
  64. };
  65. template < class Time >
  66. class TraceElements : public std::vector < TraceElement < Time > >
  67. {
  68. public:
  69. TraceElements()
  70. { }
  71. virtual ~TraceElements()
  72. { }
  73. TraceElements filter_model_name(
  74. const std::string& model_name) const
  75. {
  76. TraceElements < Time > result;
  77. std::copy_if(TraceElements < Time >::begin(),
  78. TraceElements < Time >::end(), std::back_inserter(result),
  79. [model_name](TraceElement < Time > const & x)
  80. { return x.get_model_name() == model_name; });
  81. return result;
  82. }
  83. TraceElements filter_time(typename Time::type time) const
  84. {
  85. TraceElements result;
  86. std::copy_if(TraceElements < Time >::begin(),
  87. TraceElements < Time >::end(), std::back_inserter(result),
  88. [time](TraceElement < Time > const & x)
  89. { return x.get_time() == time; });
  90. return result;
  91. }
  92. TraceElements filter_type(TraceType type) const
  93. {
  94. TraceElements result;
  95. std::copy_if(TraceElements < Time >::begin(),
  96. TraceElements < Time >::end(), std::back_inserter(result),
  97. [type](TraceElement < Time > const & x)
  98. { return x.get_type() == type; });
  99. return result;
  100. }
  101. std::string to_string() const
  102. {
  103. std::ostringstream ss;
  104. for (typename TraceElements < Time >::const_iterator it =
  105. TraceElements < Time >::begin();
  106. it != TraceElements < Time >::end(); ++it) {
  107. ss << "TRACE: " << it->get_model_name() << " at "
  108. << it->get_time() << " <";
  109. switch (it->get_type())
  110. {
  111. case NONE: ss << "none"; break;
  112. case I_MESSAGE: ss << "i_message"; break;
  113. case POST_EVENT: ss << "post_event"; break;
  114. case S_MESSAGE: ss << "s_message"; break;
  115. case Y_MESSAGE: ss << "y_message"; break;
  116. case DELTA_INT: ss << "delta_int"; break;
  117. case DELTA_EXT: ss << "delta_ext"; break;
  118. case DELTA_CONF: ss << "delta_conf"; break;
  119. case TA: ss << "ta"; break;
  120. case LAMBDA: ss << "lambda"; break;
  121. case START: ss << "start"; break;
  122. case OUTPUT: ss << "output"; break;
  123. };
  124. ss << ">";
  125. if (not it->get_comment().empty()) {
  126. ss << " => " << it->get_comment();
  127. }
  128. ss << std::endl;
  129. }
  130. return ss.str();
  131. }
  132. };
  133. template < class Time >
  134. class Trace
  135. {
  136. public:
  137. virtual ~Trace()
  138. { }
  139. static Trace& trace()
  140. {
  141. std::call_once(_flag, [] ()
  142. { _instance.reset(new Trace()); }
  143. );
  144. return *_instance;
  145. }
  146. void clear()
  147. { _trace.clear(); }
  148. const TraceElements < Time >& elements() const
  149. { return _trace; }
  150. void flush()
  151. {
  152. std::lock_guard < std::mutex > lock(_mutex);
  153. if (_sstream) {
  154. _element.set_comment(_sstream->str());
  155. delete _sstream;
  156. _sstream = 0;
  157. }
  158. _trace.push_back(_element);
  159. }
  160. std::mutex& mutex()
  161. { return _mutex; }
  162. void set_element(const TraceElement < Time >& element)
  163. { _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()
  173. { _sstream = 0; }
  174. static std::shared_ptr < Trace < Time > > _instance;
  175. static std::once_flag _flag;
  176. TraceElements < Time > _trace;
  177. TraceElement < Time > _element;
  178. std::ostringstream* _sstream;
  179. std::mutex _mutex;
  180. };
  181. } } // namespace artis common
  182. template < class Time >
  183. artis::common::Trace < Time >& operator<<(
  184. artis::common::Trace < Time >& trace,
  185. const artis::common::TraceElement < Time >& e)
  186. {
  187. std::lock_guard < std::mutex > lock(trace.mutex());
  188. trace.set_element(e);
  189. return trace;
  190. }
  191. template < class Time >
  192. artis::common::Trace < Time >& operator<<(
  193. artis::common::Trace < Time >& trace,
  194. const std::string& str)
  195. {
  196. std::lock_guard < std::mutex > lock(trace.mutex());
  197. trace.sstream() << str;
  198. return trace;
  199. }
  200. template < class Time >
  201. artis::common::Trace < Time >& operator<<(
  202. artis::common::Trace < Time >& trace,
  203. typename Time::type t)
  204. {
  205. std::lock_guard < std::mutex > lock(trace.mutex());
  206. trace.sstream() << t;
  207. return trace;
  208. }
  209. template < class Time >
  210. std::shared_ptr < artis::common::Trace < Time > >
  211. artis::common::Trace < Time >::_instance = nullptr;
  212. template < class Time >
  213. std::once_flag artis::common::Trace < Time >::_flag;
  214. #endif