KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
logger_message.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 //
12 //
13 
14 
15 #if !defined(KRATOS_LOGGER_MESSAGE_H_INCLUDED )
16 #define KRATOS_LOGGER_MESSAGE_H_INCLUDED
17 
18 
19 
20 // System includes
21 #include <string>
22 #include <sstream>
23 #include <iostream>
24 #include <map>
25 #include <chrono>
26 
27 
28 // External includes
29 
30 
31 // Project includes
33 #include "includes/code_location.h"
35 
36 namespace Kratos
37 {
40 
43 
44 class DataCommunicator; // forward declaration to avoid a cyclic include dependency
45 
47 
52 class KRATOS_API(KRATOS_CORE) LoggerMessage
53 {
54 public:
57 
58  using TimePointType = std::chrono::steady_clock::time_point;
59 
63 
64  enum class Severity {
65  WARNING,
66  INFO,
67  DETAIL,
68  DEBUG,
69  TRACE,
70  };
71 
72  enum class Category {
73  STATUS,
74  CRITICAL,
75  STATISTICS,
76  PROFILING,
77  CHECKING
78  };
79 
81  public:
82 
84  : mIsDistributed(rOther.mIsDistributed), mPrintFromAllRanks(rOther.mPrintFromAllRanks), mSourceRank(rOther.mSourceRank) {}
85 
87  return DistributedFilter(false, false, 0);
88  }
89 
90  static DistributedFilter FromRank(int TheRank) {
91  return DistributedFilter(true, false, TheRank);
92  }
93 
95  return DistributedFilter(true, true, 0);
96  }
97 
98  bool WriteFromRank(int Rank) const {
99  return mPrintFromAllRanks || Rank == mSourceRank;
100  }
101 
102  bool IsDistributed() const {
103  return mIsDistributed;
104  }
105 
106  int GetRank() const {
107  return mSourceRank;
108  }
109 
110  private:
112  : mIsDistributed(false), mPrintFromAllRanks(false), mSourceRank(0) {}
113 
114  DistributedFilter(bool IsDistributed, bool PrintFromAllRanks, int TheRank)
115  : mIsDistributed(IsDistributed), mPrintFromAllRanks(PrintFromAllRanks), mSourceRank(TheRank) {}
116 
117  bool mIsDistributed;
118  bool mPrintFromAllRanks;
119  int mSourceRank;
120  };
121 
123  public:
124 
125  MessageSource();
126 
127  MessageSource(int TheRank)
128  : mRank(TheRank) {}
129 
130  int GetRank() const {
131  return mRank;
132  }
133 
134  private:
135  int mRank;
136  };
137 
141 
142  explicit LoggerMessage(std::string const& TheLabel)
143  : mLabel(TheLabel), mLevel(1), mSeverity(Severity::INFO), mCategory(Category::STATUS), mMessageSource(), mDistributedFilter(DistributedFilter::FromRoot()) {}
144 
146  : mLabel(Other.mLabel), mMessage(Other.mMessage), mLevel(Other.mLevel), mLocation(Other.mLocation), mSeverity(Other.mSeverity), mCategory(Other.mCategory), mMessageSource(Other.mMessageSource), mDistributedFilter(Other.mDistributedFilter) {}
147 
149  virtual ~LoggerMessage() {}
150 
151 
155 
157  mLabel = Other.mLabel;
158  mMessage = Other.mMessage;
159  mLevel = Other.mLevel;
160  // mLocation = Other.mLocation;
161  mSeverity = Other.mSeverity;
162  mCategory = Other.mCategory;
163  mDistributedFilter = Other.mDistributedFilter;
164 
165  return *this;
166  }
167 
171 
172 
176 
177  void SetLabel(std::string const& TheLabel){
178  mLabel = TheLabel;
179  }
180 
181  std::string const& GetLabel() const {
182  return mLabel;
183  }
184 
185  void SetMessage(std::string const& TheMessage) {
186  mMessage = TheMessage;
187  }
188 
189  std::string const& GetMessage() const {
190  return mMessage;
191  }
192 
193  void SetLevel(std::size_t TheLevel) {
194  mLevel = TheLevel;
195  }
196 
197  std::size_t GetLevel() const {
198  return mLevel;
199  }
200 
201  void SetLocation(CodeLocation const& TheLocation) {
202  mLocation = TheLocation;
203  }
204 
206  return mLocation;
207  }
208 
209  void SetSeverity(Severity const& TheSeverity) {
210  mSeverity = TheSeverity;
211  }
212 
214  return mSeverity;
215  }
216 
217  void SetCategory(Category const& TheCategory) {
218  mCategory = TheCategory;
219  }
220 
222  return mCategory;
223  }
224 
225  bool IsDistributed() const {
226  return mDistributedFilter.IsDistributed();
227  }
228 
229  bool WriteInThisRank() const {
230  return mDistributedFilter.WriteFromRank(mMessageSource.GetRank());
231  }
232 
233  int GetSourceRank() const {
234  return mMessageSource.GetRank();
235  }
236 
237  void SetTime() {
238  mTime = std::chrono::steady_clock::now();
239  }
240 
241  TimePointType const& GetTime() const {
242  return mTime;
243  }
244 
248 
252 
254  virtual std::string Info() const;
255 
257  virtual void PrintInfo(std::ostream& rOStream) const;
258 
260  virtual void PrintData(std::ostream& rOStream) const;
261 
262 
264  template<class StreamValueType>
265  LoggerMessage& operator << (StreamValueType const& rValue)
266  {
267  std::stringstream buffer;
268  buffer << rValue;
269 
270  mMessage.append(buffer.str());
271 
272  return *this;
273  }
274 
276  LoggerMessage& operator << (std::ostream& (*pf)(std::ostream&));
277 
279  LoggerMessage& operator << (const char * rString);
280 
282  LoggerMessage& operator << (CodeLocation const& TheLocation);
283 
285  LoggerMessage& operator << (Severity const& TheSeverity);
286 
288  LoggerMessage& operator << (Category const& TheCategory);
289 
291  LoggerMessage& operator << (DistributedFilter const& TheMessageSource);
292 
294  LoggerMessage& operator << (DataCommunicator const& TheDataCommunicator);
295 
297 
298 private:
301 
305 
306  std::string mLabel;
307  std::string mMessage;
308  std::size_t mLevel;
309  CodeLocation mLocation;
310  Severity mSeverity;
311  Category mCategory;
312  MessageSource mMessageSource;
313  DistributedFilter mDistributedFilter;
314  TimePointType mTime;
315 
317 }; // Class LoggerMessage
318 
320 
323 
325 std::ostream& operator << (std::ostream& rOStream,
326  const LoggerMessage& rThis);
327 
331 
332 
334 
336 } // namespace Kratos.
337 
338 #endif // KRATOS_LOGGER_MESSAGE_H_INCLUDED defined
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
Definition: code_location.h:31
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
Definition: logger_message.h:80
DistributedFilter(DistributedFilter const &rOther)
Definition: logger_message.h:83
bool IsDistributed() const
Definition: logger_message.h:102
static DistributedFilter FromRoot()
Definition: logger_message.h:86
static DistributedFilter FromAllRanks()
Definition: logger_message.h:94
static DistributedFilter FromRank(int TheRank)
Definition: logger_message.h:90
int GetRank() const
Definition: logger_message.h:106
bool WriteFromRank(int Rank) const
Definition: logger_message.h:98
Definition: logger_message.h:122
MessageSource(int TheRank)
Definition: logger_message.h:127
int GetRank() const
Definition: logger_message.h:130
LoggerMessage class holdes message and the properties of the message.
Definition: logger_message.h:53
LoggerMessage(LoggerMessage const &Other)
Definition: logger_message.h:145
CodeLocation GetLocation() const
Definition: logger_message.h:205
Severity
Definition: logger_message.h:64
virtual ~LoggerMessage()
Destructor.
Definition: logger_message.h:149
void SetCategory(Category const &TheCategory)
Definition: logger_message.h:217
void SetLocation(CodeLocation const &TheLocation)
Definition: logger_message.h:201
std::string const & GetLabel() const
Definition: logger_message.h:181
std::chrono::steady_clock::time_point TimePointType
Definition: logger_message.h:58
void SetLevel(std::size_t TheLevel)
Definition: logger_message.h:193
bool WriteInThisRank() const
Definition: logger_message.h:229
int GetSourceRank() const
Definition: logger_message.h:233
Category GetCategory() const
Definition: logger_message.h:221
void SetTime()
Definition: logger_message.h:237
Category
Definition: logger_message.h:72
LoggerMessage & operator=(LoggerMessage const &Other)
Definition: logger_message.h:156
bool IsDistributed() const
Definition: logger_message.h:225
TimePointType const & GetTime() const
Definition: logger_message.h:241
void SetLabel(std::string const &TheLabel)
Definition: logger_message.h:177
void SetSeverity(Severity const &TheSeverity)
Definition: logger_message.h:209
std::size_t GetLevel() const
Definition: logger_message.h:197
LoggerMessage(std::string const &TheLabel)
Definition: logger_message.h:142
void SetMessage(std::string const &TheMessage)
Definition: logger_message.h:185
Severity GetSeverity() const
Definition: logger_message.h:213
std::string const & GetMessage() const
Definition: logger_message.h:189
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432