v0.14.0
LogManager.hpp
Go to the documentation of this file.
1 /**
2  * @file LogManager.hpp
3  * @brief Log and register warnings
4  *
5  */
6 
7 #ifndef __LOGMANAGER_HPP__
8 #define __LOGMANAGER_HPP__
9 
10 namespace attrs = boost::log::attributes;
11 namespace logging = boost::log;
12 namespace keywords = boost::log::keywords;
13 namespace logging = boost::log;
14 namespace sinks = boost::log::sinks;
15 namespace src = boost::log::sources;
16 namespace attrs = boost::log::attributes;
17 namespace expr = boost::log::expressions;
18 
19 namespace MoFEM {
20 
21 /**
22  * \brief Log manager is used to build and partition problems
23  * \ingroup mofem_log_manager
24  *
25  */
26 struct LogManager : public UnknownInterface {
27 
28  /**
29  * @brief Severity levels
30  * \ingroup mofem_log_manager
31  *
32  */
34 
35  static constexpr std::array<char *const, error + 1> severityStrings = {
36 
37  (char *)"noisy", (char *)"verbose", (char *)"inform", (char *)"warning",
38  (char *)"error"
39 
40  };
41 
42  /**
43  * @brief Tag attributes switches
44  * \ingroup mofem_log_manager
45  *
46  */
48  BitLineID = 1 << 0,
49  BitScope = 1 << 1,
50  };
51 
52  MoFEMErrorCode query_interface(boost::typeindex::type_index type_index,
53  UnknownInterface **iface) const;
54 
55  LogManager(const MoFEM::Core &core);
56  virtual ~LogManager() = default;
57 
58  /**
59  * @brief Definition of the channel logger
60  *
61  */
62  typedef boost::log::sources::severity_channel_logger<SeverityLevel,
63  std::string>
65 
66  typedef sinks::synchronous_sink<sinks::text_ostream_backend> SinkType;
67 
68  /**
69  * @brief Add attributes to logger
70  * \ingroup mofem_log_manager
71  *
72  * @param lg
73  * @param bit
74  */
75  static void addAttributes(LogManager::LoggerType &lg, const int bit = 0);
76 
77  /**
78  * @brief Add attributes to channel
79  * \ingroup mofem_log_manager
80  *
81  * @param channel
82  * @param bit
83  */
84  static void addAttributes(const std::string channel, const int bit = 0);
85 
86  /**
87  * @brief Set ans resset chanel logger
88  * \ingroup mofem_log_manager
89  *
90  * @param channel
91  * @return LoggerType&
92  */
93  static LoggerType &setLog(const std::string channel);
94 
95  /**
96  * @brief Get logger by channel
97  * \ingroup mofem_log_manager
98  *
99  * @param channel
100  * @return LoggerType&
101  */
102  static LoggerType &getLog(const std::string channel);
103 
104  /**
105  * @brief Check if channel exist
106  *
107  * @param channel
108  * @return true
109  * @return false
110  */
111  static bool checkIfChannelExist(const std::string channel);
112 
113  /**
114  * @brief Add tag to logger
115  * \ingroup mofem_log_manager
116  *
117  * @param lg
118  * @param tag
119  */
120  static void addTag(LogManager::LoggerType &lg, const std::string tag);
121 
122  /**
123  * @brief Add tag to channel
124  * \ingroup mofem_log_manager
125  *
126  * @param channel
127  * @param tag
128  */
129  static void addTag(const std::string channel, const std::string tag);
130 
131  /**
132  * @brief Get the strm self object
133  *
134  * @return boost::shared_ptr<std::ostream>
135  */
136  static boost::shared_ptr<std::ostream> getStrmSelf();
137 
138  /**
139  * @brief Get the strm world object
140  *
141  * @return boost::shared_ptr<std::ostream>
142  */
143  static boost::shared_ptr<std::ostream> getStrmWorld();
144 
145  /**
146  * @brief Get the strm sync object
147  *
148  * @return boost::shared_ptr<std::ostream>
149  */
150  static boost::shared_ptr<std::ostream> getStrmSync();
151 
152  /**
153  * @brief Get the strm null object
154  *
155  * @return boost::shared_ptr<std::ostream>
156  */
157  static boost::shared_ptr<std::ostream> getStrmNull();
158 
159  /**
160  * @brief Create a sink object
161  *
162  * @param stream_ptr
163  * @param comm_filter
164  * @return boost::shared_ptr<SinkType>
165  */
166  static boost::shared_ptr<SinkType>
167  createSink(boost::shared_ptr<std::ostream> stream_ptr,
168  std::string comm_filter);
169 
170  /**
171  * @brief Create default sinks
172  *
173  */
174  static void createDefaultSinks(MPI_Comm comm);
175 
176  /** @brief Get logger option
177  *
178  * This function is called by MoFEM core when this interface is registred
179  * into database.
180  *
181  * @return MoFEMErrorCode
182  */
183  static MoFEMErrorCode getOptions();
184 
185  /**
186  * @brief Dummy file pointer (DO NOT USE)
187  *
188  * \note This is for internal use only/
189  *
190  */
191  static FILE *dummy_mofem_fd;
192 
193  /**
194  * @brief Use to handle PETSc output
195  *
196  * \note This is for internal use only/
197  *
198  * @param fd
199  * @param format
200  * @param Argp
201  * @return PetscErrorCode
202  */
203  static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[],
204  va_list Argp);
205 
206  /**
207  * @brief Converts formatted output to string
208  *
209  * @param fmt
210  * @param args
211  * @return std::string
212  */
213  static std::string getVLikeFormatedString(const char *fmt, va_list args);
214 
215  /**
216  * @brief Converts formatted output to string
217  *
218  * @param fmt
219  * @param args
220  * @return std::string
221  */
222  static std::string getCLikeFormatedString(const char *fmt, ...);
223 
224  /**
225  * @brief Default record formatter
226  *
227  * @param rec
228  * @param strm
229  */
230  static void recordFormatterDefault(logging::record_view const &rec,
231  logging::formatting_ostream &strm);
232 
233 private:
235 
236  struct InternalData;
237  static boost::shared_ptr<InternalData> internalDataPtr;
238 
239  static std::string petscStringCache;
240 
241 };
242 
244 
245 // The operator puts a human-friendly representation of the severity level to
246 // the stream
247 std::ostream &operator<<(std::ostream &strm,
248  const LogManager::SeverityLevel &level);
249 
250 namespace LogKeywords {
251 
253 BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
254 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
255 BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
256 BOOST_LOG_ATTRIBUTE_KEYWORD(proc_attr, "Proc", unsigned int)
257 BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope",
258  boost::log::attributes::named_scope::value_type)
260  boost::log::attributes::timer::value_type)
261 
262 } // namespace LogKeywords
263 
264 } // namespace MoFEM
265 
266 extern "C" {
267 PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp);
268 }
269 
270 /**
271  * @brief Set and reset channel
272  * \ingroup mofem_log_manager
273  *
274  * \code
275  * MOFEM_LOG_CHANNEL("WORLD");
276  * \endcode
277  *
278  * Are three default type of channels, SELF, each processor prints to the
279  * standard output, WORLD, only processor one prints, and SYNC all processors
280  * prints synchronously.
281  *
282  *
283  */
284 #define MOFEM_LOG_CHANNEL(channel) \
285  { MoFEM::LogManager::setLog(channel); }
286 
287 /**
288  * @brief Add attributes to channel
289  * \ingroup mofem_log_manager
290  *
291  * \code
292  * MOFEM_LOG_ATTRIBUTES("SYNC", LogManager::BitLineID | LogManager::BitScope);
293  * \endcode
294  *
295  */
296 #define MOFEM_LOG_ATTRIBUTES(channel, bit) \
297  { MoFEM::LogManager::addAttributes(channel, bit); }
298 
299 /**
300  * @brief Log
301  * \ingroup mofem_log_manager
302  *
303  * \code
304  * MOFEM_LOG("WORLD", LogManager::SeverityLevel::inform) << "Hello world";
305  * \endcode
306  *
307  */
308 #define MOFEM_LOG(channel, severity) \
309  BOOST_LOG_SEV(MoFEM::LogManager::getLog(channel), severity)
310 
311 #define MOFEM_LOG_C(channel, severity, format, ...) \
312  MOFEM_LOG(channel, severity) \
313  << MoFEM::LogManager::getCLikeFormatedString(format, __VA_ARGS__)
314 
315 /** \brief Set scope
316  * \ingroup mofem_log_manager
317  *
318  * Macro for function scope markup. The scope name is constructed with help of
319  * compiler and contains the current function signature. The scope name is
320  * pushed to the end of the current thread scope list.
321  *
322  * Not all compilers have support for this macro. The exact form of the scope
323  * name may vary from one compiler to another.
324  */
325 #define MOFEM_LOG_FUNCTION() \
326  BOOST_LOG_NAMED_SCOPE_INTERNAL( \
327  BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), \
328  PETSC_FUNCTION_NAME, __FILE__, __LINE__, \
329  ::boost::log::attributes::named_scope_entry::function)
330 
331 /**
332  * @brief Tag channel
333  * \ingroup mofem_log_manager
334  *
335  * Tag channel tag is set until MOFEM_LOG_CHANNEL is called, then new tag can be
336  * set.
337  *
338  */
339 #define MOFEM_LOG_TAG(channel, tag) MoFEM::LogManager::addTag(channel, tag);
340 
341 /**
342  * @brief Synchronise "SYNC" channel
343  *
344  */
345 #define MOFEM_LOG_SYNCHRONISE(comm) \
346  PetscSynchronizedFlush(comm, MoFEM::LogManager::dummy_mofem_fd);
347 
348 /**
349  * @brief Synchronise "SYNC" on curtain severity level
350  *
351  */
352 #define MOFEM_LOG_SEVERITY_SYNC(comm, severity) \
353  MOFEM_LOG("NULL", severity) << ([&] { \
354  MOFEM_LOG_SYNCHRONISE(comm); \
355  return 0; \
356  })()
357 
358 /**
359  * @brief Tag and log in channel
360  *
361  */
362 #define MOFEM_TAG_AND_LOG(channel, severity, tag) \
363  MOFEM_LOG_TAG(channel, tag) \
364  MOFEM_LOG(channel, severity)
365 
366 /**
367  * @brief Tag and log in channel
368  *
369  */
370 #define MOFEM_TAG_AND_LOG_C(channel, severity, tag, format, ...) \
371  MOFEM_LOG_TAG(channel, tag) \
372  MOFEM_LOG_C(channel, severity, format, __VA_ARGS__)
373 
374 #endif //__LOGMANAGER_HPP__
375 
376 /**
377  * \defgroup mofem_log_manager
378  * \brief Log manager
379  *
380  * Logging manager based on Boost.Log
381  * (<a href="https://www.boost.org/doc/libs/1_63_0/libs/log/doc/html/index.html">Boost.Log v2</a>)
382  *
383  * \ingroup mofem
384  */
MoFEM::operator<<
std::ostream & operator<<(std::ostream &os, const EntitiesFieldData::EntData &e)
Definition: EntitiesFieldData.cpp:240
MoFEM::LogManager::noisy
@ noisy
Definition: LogManager.hpp:33
MoFEM::LogManager::checkIfChannelExist
static bool checkIfChannelExist(const std::string channel)
Check if channel exist.
Definition: LogManager.cpp:404
MoFEM::CoreTmp< 0 >
Core (interface) class.
Definition: Core.hpp:82
MoFEM::LogManager::getLog
static LoggerType & getLog(const std::string channel)
Get logger by channel.
Definition: LogManager.cpp:395
MoFEM::LogManager::dummy_mofem_fd
static FILE * dummy_mofem_fd
Dummy file pointer (DO NOT USE)
Definition: LogManager.hpp:191
MoFEM::LogManager::petscStringCache
static std::string petscStringCache
Definition: LogManager.hpp:239
convert.args
args
Definition: convert.py:66
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::LogManager::recordFormatterDefault
static void recordFormatterDefault(logging::record_view const &rec, logging::formatting_ostream &strm)
Default record formatter.
Definition: LogManager.cpp:204
MoFEM::LogManager::createSink
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
Definition: LogManager.cpp:298
MoFEM::LogManager::getVLikeFormatedString
static std::string getVLikeFormatedString(const char *fmt, va_list args)
Converts formatted output to string.
Definition: LogManager.cpp:465
MoFEM::LogManager::logPetscFPrintf
static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[], va_list Argp)
Use to handle PETSc output.
Definition: LogManager.cpp:411
MoFEM::LogManager::LoggerType
boost::log::sources::severity_channel_logger< SeverityLevel, std::string > LoggerType
Definition of the channel logger.
Definition: LogManager.hpp:64
MoFEM::LogManager::severityStrings
static constexpr std::array< char *const, error+1 > severityStrings
Definition: LogManager.hpp:35
MoFEM::LogManager::getCLikeFormatedString
static std::string getCLikeFormatedString(const char *fmt,...)
Converts formatted output to string.
Definition: LogManager.cpp:471
MoFEM::LogManager::getStrmNull
static boost::shared_ptr< std::ostream > getStrmNull()
Get the strm null object.
Definition: LogManager.cpp:352
MoFEM::LogManager::BitLineID
@ BitLineID
Definition: LogManager.hpp:48
MoFEM::LogManager::BitScope
@ BitScope
Definition: LogManager.hpp:49
MoFEM::LogManager::inform
@ inform
Definition: LogManager.hpp:33
MoFEM::LogManager::cOre
MoFEM::Core & cOre
Definition: LogManager.hpp:234
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::LogKeywords::BOOST_LOG_ATTRIBUTE_KEYWORD
BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope", boost::log::attributes::named_scope::value_type) BOOST_LOG_ATTRIBUTE_KEYWORD(timeline
bit
auto bit
set bit
Definition: hanging_node_approx.cpp:75
MoFEM::LogManager::getStrmSync
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.
Definition: LogManager.cpp:348
MoFEM::LogManager::internalDataPtr
static boost::shared_ptr< InternalData > internalDataPtr
Definition: LogManager.hpp:236
MoFEM::LogManager::getStrmWorld
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
Definition: LogManager.cpp:344
MoFEM::LogManager::getStrmSelf
static boost::shared_ptr< std::ostream > getStrmSelf()
Get the strm self object.
Definition: LogManager.cpp:340
MoFEM::LogManager::getOptions
static MoFEMErrorCode getOptions()
Get logger option.
Definition: LogManager.cpp:152
MoFEM::LogManager::SeverityLevel
SeverityLevel
Severity levels.
Definition: LogManager.hpp:33
MoFEM::LogManager::SinkType
sinks::synchronous_sink< sinks::text_ostream_backend > SinkType
Definition: LogManager.hpp:66
MoFEM::LogManager::error
@ error
Definition: LogManager.hpp:33
PetscVFPrintfDefault
PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp)
MoFEM::LogManager::verbose
@ verbose
Definition: LogManager.hpp:33
MoFEM::UnknownInterface
base class for all interface classes
Definition: UnknownInterface.hpp:34
MoFEM::LogManager::createDefaultSinks
static void createDefaultSinks(MPI_Comm comm)
Create default sinks.
Definition: LogManager.cpp:313
MoFEM::LogManager::query_interface
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LogManager.cpp:145
MoFEM::LogKeywords::Timeline
Timeline
Definition: LogManager.hpp:259
MoFEM::LogManager::warning
@ warning
Definition: LogManager.hpp:33
MoFEM::LogManager::InternalData
Definition: LogManager.cpp:34
MoFEM::LogManager::setLog
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
Definition: LogManager.cpp:389
MoFEM::LogManager::addAttributes
static void addAttributes(LogManager::LoggerType &lg, const int bit=0)
Add attributes to logger.
Definition: LogManager.cpp:359
MoFEM::LogManager::addTag
static void addTag(LogManager::LoggerType &lg, const std::string tag)
Add tag to logger.
Definition: LogManager.cpp:381
MoFEM::LogManager::LogAttributesBits
LogAttributesBits
Tag attributes switches.
Definition: LogManager.hpp:47
MoFEM::LogManager
Log manager is used to build and partition problems.
Definition: LogManager.hpp:26
MoFEM::LogManager::~LogManager
virtual ~LogManager()=default
MoFEM::LogManager::LogManager
LogManager(const MoFEM::Core &core)
Definition: LogManager.cpp:141