Trace.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <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. public:
  41. TraceElement()
  42. :_time(Time::null), _formalism_type(FormalismType::NONE),
  43. _function_type(FunctionType::NONE), _level_type(LevelType::NONE) { }
  44. TraceElement(const std::string& model_name, typename Time::type time,
  45. const FormalismType::Values& formalism_type,
  46. const FunctionType::Values& function_type, const LevelType::Values& level_type)
  47. :_model_name(model_name), _time(time), _formalism_type(formalism_type),
  48. _function_type(function_type), _level_type(level_type) { }
  49. virtual ~TraceElement() { }
  50. const std::string& get_comment() const { return _comment; }
  51. const std::string& get_model_name() const { return _model_name; }
  52. typename Time::type get_time() const { return _time; }
  53. const FormalismType::Values& get_formalism_type() const { return _formalism_type; }
  54. const FunctionType::Values& get_function_type() const { return _function_type; }
  55. const LevelType::Values& get_level_type() const { return _level_type; }
  56. void set_comment(const std::string& comment) { _comment = comment; }
  57. std::string to_string() const
  58. {
  59. std::ostringstream ss;
  60. ss << "TRACE: " << get_model_name() << " at " << get_time() << " <";
  61. ss << FormalismType::to_string(get_formalism_type()) << ", ";
  62. ss << FunctionType::to_string(get_function_type()) << ", ";
  63. ss << LevelType::to_string(get_level_type()) << ">";
  64. if (not get_comment().empty()) {
  65. ss << " => " << get_comment();
  66. }
  67. return ss.str();
  68. }
  69. private:
  70. std::string _model_name;
  71. typename Time::type _time;
  72. FormalismType::Values _formalism_type;
  73. FunctionType::Values _function_type;
  74. LevelType::Values _level_type;
  75. std::string _comment;
  76. };
  77. template<class Time>
  78. class TraceElements : public std::vector<TraceElement<Time> > {
  79. public:
  80. TraceElements() { }
  81. virtual ~TraceElements() { }
  82. TraceElements filter_model_name(const std::string& model_name) const
  83. {
  84. TraceElements<Time> result;
  85. std::copy_if(TraceElements<Time>::begin(),
  86. TraceElements<Time>::end(), std::back_inserter(result),
  87. [model_name](TraceElement<Time> const& x) {
  88. return x.get_model_name() == model_name;
  89. });
  90. return result;
  91. }
  92. TraceElements filter_time(typename Time::type time) const
  93. {
  94. TraceElements result;
  95. std::copy_if(TraceElements<Time>::begin(),
  96. TraceElements<Time>::end(), std::back_inserter(result),
  97. [time](TraceElement<Time> const& x) { return x.get_time() == time; });
  98. return result;
  99. }
  100. TraceElements filter_formalism_type(const FormalismType::Values& type) const
  101. {
  102. TraceElements result;
  103. std::copy_if(TraceElements<Time>::begin(),
  104. TraceElements<Time>::end(), std::back_inserter(result),
  105. [type](TraceElement<Time> const& x) { return x.get_formalism_type() == type; });
  106. return result;
  107. }
  108. TraceElements filter_function_type(const FunctionType::Values& type) const
  109. {
  110. TraceElements result;
  111. std::copy_if(TraceElements<Time>::begin(),
  112. TraceElements<Time>::end(), std::back_inserter(result),
  113. [type](TraceElement<Time> const& x) { return x.get_function_type() == type; });
  114. return result;
  115. }
  116. TraceElements filter_level_type(const LevelType::Values& type) const
  117. {
  118. TraceElements result;
  119. std::copy_if(TraceElements<Time>::begin(),
  120. TraceElements<Time>::end(), std::back_inserter(result),
  121. [type](TraceElement<Time> const& x) { return x.get_level_type() == type; });
  122. return result;
  123. }
  124. std::string to_string() const
  125. {
  126. std::ostringstream ss;
  127. for (typename TraceElements<Time>::const_iterator it = TraceElements<Time>::begin();
  128. it != TraceElements<Time>::end(); ++it) {
  129. ss << "TRACE: " << it->get_model_name() << " at " << it->get_time() << " <";
  130. ss << FormalismType::to_string(it->get_formalism_type()) << ", ";
  131. ss << FunctionType::to_string(it->get_function_type()) << ", ";
  132. ss << LevelType::to_string(it->get_level_type()) << ">";
  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. if (!_instance) {
  149. _instance.reset(new Trace());
  150. }
  151. return *_instance;
  152. }
  153. void clear() { _trace.clear(); }
  154. const TraceElements<Time>& elements() const { return _trace; }
  155. void flush()
  156. {
  157. std::lock_guard<std::mutex> lock(_mutex);
  158. if (_sstream) {
  159. _element.set_comment(_sstream->str());
  160. delete _sstream;
  161. _sstream = 0;
  162. }
  163. _trace.push_back(_element);
  164. }
  165. std::mutex& mutex() { return _mutex; }
  166. void set_element(const TraceElement<Time>& element) { _element = element; }
  167. std::ostringstream& sstream()
  168. {
  169. if (_sstream == 0) {
  170. _sstream = new std::ostringstream();
  171. }
  172. return *_sstream;
  173. }
  174. private:
  175. Trace() { _sstream = 0; }
  176. static std::shared_ptr<Trace<Time> > _instance;
  177. // static std::once_flag _flag;
  178. TraceElements<Time> _trace;
  179. TraceElement<Time> _element;
  180. std::ostringstream* _sstream;
  181. std::mutex _mutex;
  182. };
  183. }
  184. } // namespace artis common
  185. template<class Time>
  186. artis::common::Trace<Time>&
  187. operator<<(artis::common::Trace<Time>& trace, const artis::common::TraceElement<Time>& e)
  188. {
  189. std::lock_guard<std::mutex> lock(trace.mutex());
  190. trace.set_element(e);
  191. return trace;
  192. }
  193. template<class Time>
  194. artis::common::Trace<Time>& operator<<(artis::common::Trace<Time>& trace, 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<<(artis::common::Trace<Time>& trace, typename Time::type t)
  202. {
  203. std::lock_guard<std::mutex> lock(trace.mutex());
  204. trace.sstream() << t;
  205. return trace;
  206. }
  207. template<class Time>
  208. std::shared_ptr<artis::common::Trace<Time> > artis::common::Trace<Time>::_instance = nullptr;
  209. //template<class Time>
  210. //std::once_flag artis::common::Trace<Time>::_flag;
  211. #endif