Trace.hpp 8.4 KB

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