Trace.hpp 7.4 KB

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