Trace.hpp 7.4 KB

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