Trace.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @file common/utils/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-2021 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
  27. #include <artis-star/common/utils/FormalismType.hpp>
  28. #include <artis-star/common/utils/FunctionType.hpp>
  29. #include <artis-star/common/utils/LevelType.hpp>
  30. #include <algorithm>
  31. #include <iterator>
  32. #include <memory>
  33. #include <mutex>
  34. #include <sstream>
  35. #include <vector>
  36. namespace artis {
  37. namespace common {
  38. template<class Time>
  39. class TraceElement
  40. {
  41. public:
  42. TraceElement()
  43. : _time(Time::null), _formalism_type(FormalismType::NONE),
  44. _function_type(FunctionType::NONE), _level_type(LevelType::NONE)
  45. {}
  46. TraceElement(const std::string &model_name, typename Time::type time,
  47. const FormalismType::Values &formalism_type,
  48. const FunctionType::Values &function_type, const LevelType::Values &level_type)
  49. : _model_name(model_name), _time(time), _formalism_type(formalism_type),
  50. _function_type(function_type), _level_type(level_type)
  51. {}
  52. virtual ~TraceElement()
  53. {}
  54. const std::string &get_comment() const
  55. { return _comment; }
  56. const std::string &get_model_name() const
  57. { return _model_name; }
  58. typename Time::type get_time() const
  59. { return _time; }
  60. const FormalismType::Values &get_formalism_type() const
  61. { return _formalism_type; }
  62. const FunctionType::Values &get_function_type() const
  63. { return _function_type; }
  64. const LevelType::Values &get_level_type() const
  65. { return _level_type; }
  66. void set_comment(const std::string &comment)
  67. { _comment = comment; }
  68. std::string to_string() const
  69. {
  70. std::ostringstream ss;
  71. ss << "TRACE: " << get_model_name() << " at " << get_time() << " <";
  72. ss << FormalismType::to_string(get_formalism_type()) << ", ";
  73. ss << FunctionType::to_string(get_function_type()) << ", ";
  74. ss << LevelType::to_string(get_level_type()) << ">";
  75. if (not get_comment().empty()) {
  76. ss << " => " << get_comment();
  77. }
  78. return ss.str();
  79. }
  80. private:
  81. std::string _model_name;
  82. typename Time::type _time;
  83. FormalismType::Values _formalism_type;
  84. FunctionType::Values _function_type;
  85. LevelType::Values _level_type;
  86. std::string _comment;
  87. };
  88. template<class Time>
  89. class TraceElements : public std::vector<TraceElement<Time> >
  90. {
  91. public:
  92. TraceElements()
  93. {}
  94. virtual ~TraceElements()
  95. {}
  96. TraceElements filter_model_name(const std::string &model_name) const
  97. {
  98. TraceElements<Time> result;
  99. std::copy_if(TraceElements<Time>::begin(),
  100. TraceElements<Time>::end(), std::back_inserter(result),
  101. [model_name](TraceElement<Time> const &x) {
  102. return x.get_model_name() == model_name;
  103. });
  104. return result;
  105. }
  106. TraceElements filter_time(typename Time::type time) const
  107. {
  108. TraceElements result;
  109. std::copy_if(TraceElements<Time>::begin(),
  110. TraceElements<Time>::end(), std::back_inserter(result),
  111. [time](TraceElement<Time> const &x) { return x.get_time() == time; });
  112. return result;
  113. }
  114. TraceElements filter_formalism_type(const FormalismType::Values &type) const
  115. {
  116. TraceElements result;
  117. std::copy_if(TraceElements<Time>::begin(),
  118. TraceElements<Time>::end(), std::back_inserter(result),
  119. [type](TraceElement<Time> const &x) { return x.get_formalism_type() == type; });
  120. return result;
  121. }
  122. TraceElements filter_function_type(const FunctionType::Values &type) const
  123. {
  124. TraceElements result;
  125. std::copy_if(TraceElements<Time>::begin(),
  126. TraceElements<Time>::end(), std::back_inserter(result),
  127. [type](TraceElement<Time> const &x) { return x.get_function_type() == type; });
  128. return result;
  129. }
  130. TraceElements filter_level_type(const LevelType::Values &type) const
  131. {
  132. TraceElements result;
  133. std::copy_if(TraceElements<Time>::begin(),
  134. TraceElements<Time>::end(), std::back_inserter(result),
  135. [type](TraceElement<Time> const &x) { return x.get_level_type() == type; });
  136. return result;
  137. }
  138. std::string to_string() const
  139. {
  140. std::ostringstream ss;
  141. for (typename TraceElements<Time>::const_iterator it = TraceElements<Time>::begin();
  142. it != TraceElements<Time>::end(); ++it) {
  143. ss << "TRACE: " << it->get_model_name() << " at " << it->get_time() << " <";
  144. ss << FormalismType::to_string(it->get_formalism_type()) << ", ";
  145. ss << FunctionType::to_string(it->get_function_type()) << ", ";
  146. ss << LevelType::to_string(it->get_level_type()) << ">";
  147. if (not it->get_comment().empty()) {
  148. ss << " => " << it->get_comment();
  149. }
  150. ss << std::endl;
  151. }
  152. return ss.str();
  153. }
  154. };
  155. template<class Time>
  156. class Trace
  157. {
  158. public:
  159. virtual ~Trace()
  160. {}
  161. static Trace &trace()
  162. {
  163. // std::call_once(_flag, []() { _instance.reset(new Trace()); });
  164. if (!_instance) {
  165. _instance.reset(new Trace());
  166. }
  167. return *_instance;
  168. }
  169. void clear()
  170. { _trace.clear(); }
  171. const TraceElements<Time> &elements() const
  172. { return _trace; }
  173. void flush()
  174. {
  175. std::lock_guard<std::mutex> lock(_mutex);
  176. if (_sstream) {
  177. _element.set_comment(_sstream->str());
  178. delete _sstream;
  179. _sstream = 0;
  180. }
  181. _trace.push_back(_element);
  182. }
  183. std::mutex &mutex()
  184. { return _mutex; }
  185. void set_element(const TraceElement<Time> &element)
  186. { _element = element; }
  187. std::ostringstream &sstream()
  188. {
  189. if (_sstream == 0) {
  190. _sstream = new std::ostringstream();
  191. }
  192. return *_sstream;
  193. }
  194. private:
  195. Trace()
  196. { _sstream = 0; }
  197. static std::shared_ptr<Trace<Time> > _instance;
  198. // static std::once_flag _flag;
  199. TraceElements<Time> _trace;
  200. TraceElement<Time> _element;
  201. std::ostringstream *_sstream;
  202. std::mutex _mutex;
  203. };
  204. }
  205. } // namespace artis common
  206. template<class Time>
  207. artis::common::Trace<Time> &
  208. operator<<(artis::common::Trace<Time> &trace, const artis::common::TraceElement<Time> &e)
  209. {
  210. std::lock_guard<std::mutex> lock(trace.mutex());
  211. trace.set_element(e);
  212. return trace;
  213. }
  214. template<class Time>
  215. artis::common::Trace<Time> &operator<<(artis::common::Trace<Time> &trace, const std::string &str)
  216. {
  217. std::lock_guard<std::mutex> lock(trace.mutex());
  218. trace.sstream() << str;
  219. return trace;
  220. }
  221. template<class Time>
  222. artis::common::Trace<Time> &operator<<(artis::common::Trace<Time> &trace, typename Time::type t)
  223. {
  224. std::lock_guard<std::mutex> lock(trace.mutex());
  225. trace.sstream() << t;
  226. return trace;
  227. }
  228. template<class Time>
  229. std::shared_ptr<artis::common::Trace<Time> > artis::common::Trace<Time>::_instance = nullptr;
  230. //template<class Time>
  231. //std::once_flag artis::common::Trace<Time>::_flag;
  232. #endif