v0.15.0
Loading...
Searching...
No Matches
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
10namespace attrs = boost::log::attributes;
11namespace logging = boost::log;
12namespace keywords = boost::log::keywords;
13namespace logging = boost::log;
14namespace sinks = boost::log::sinks;
15namespace src = boost::log::sources;
16namespace attrs = boost::log::attributes;
17namespace expr = boost::log::expressions;
18
19namespace MoFEM {
20
21/**
22 * \brief Log manager is used to build and partition problems
23 * \ingroup mofem_log_manager
24 *
25 */
27
28 /**
29 * @brief Severity levels
30 * \ingroup mofem_log_manager
31 *
32 */
34
35 static inline 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 Get the strm sync object
161 *
162 * @param comm
163 * @param fd
164 * @return boost::shared_ptr<std::ostream>
165 */
166 static boost::shared_ptr<std::ostream> getStrmSync(MPI_Comm comm, FILE *fd);
167
168 /**
169 * @brief Create a sink object
170 *
171 * @param stream_ptr
172 * @param comm_filter
173 * @return boost::shared_ptr<SinkType>
174 */
175 static boost::shared_ptr<SinkType>
176 createSink(boost::shared_ptr<std::ostream> stream_ptr,
177 std::string comm_filter);
178
179 /**
180 * @brief Create default sinks
181 *
182 */
183 static void createDefaultSinks(MPI_Comm comm);
184
185 /** @brief Get logger option
186 *
187 * This function is called by MoFEM core when this interface is registred
188 * into database.
189 *
190 * @return MoFEMErrorCode
191 */
192 static MoFEMErrorCode getOptions();
193
194 /**
195 * @brief Dummy file pointer (DO NOT USE)
196 *
197 * \note This is for internal use only/
198 *
199 */
200 static FILE *dummy_mofem_fd;
201
202 /**
203 * @brief Use to handle PETSc output
204 *
205 * \note This is for internal use only/
206 *
207 * @param fd
208 * @param format
209 * @param Argp
210 * @return PetscErrorCode
211 */
212 static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[],
213 va_list Argp);
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 getVLikeFormatedString(const char *fmt, va_list args);
223
224 /**
225 * @brief Converts formatted output to string
226 *
227 * @param fmt
228 * @param args
229 * @return std::string
230 */
231 static std::string getCLikeFormatedString(const char *fmt, ...);
232
233 /**
234 * @brief Default record formatter
235 *
236 * @param rec
237 * @param strm
238 */
239 static void recordFormatterDefault(logging::record_view const &rec,
240 logging::formatting_ostream &strm);
241
242private:
244
245 struct InternalData;
246 static boost::shared_ptr<InternalData> internalDataPtr;
247
248 static std::string petscStringCache;
249};
250
252
253// The operator puts a human-friendly representation of the severity level to
254// the stream
255std::ostream &operator<<(std::ostream &strm,
256 const LogManager::SeverityLevel &level);
257
258namespace LogKeywords {
259
261BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
262BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
263BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
264BOOST_LOG_ATTRIBUTE_KEYWORD(proc_attr, "Proc", unsigned int)
266 boost::log::attributes::named_scope::value_type)
268 boost::log::attributes::timer::value_type)
269
270} // namespace LogKeywords
271
272} // namespace MoFEM
273
274extern "C" {
275PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp);
276}
277
278 /**
279 * @brief Set and reset channel
280 * \ingroup mofem_log_manager
281 *
282 * \code
283 * MOFEM_LOG_CHANNEL("WORLD");
284 * \endcode
285 *
286 * Are three default type of channels, SELF, each processor prints to the
287 * standard output, WORLD, only processor one prints, and SYNC all processors
288 * prints synchronously.
289 *
290 *
291 */
292 #define MOFEM_LOG_CHANNEL(channel) \
293 { MoFEM::LogManager::setLog(channel); }
294
295 /**
296 * @brief Add attributes to channel
297 * \ingroup mofem_log_manager
298 *
299 * \code
300 * MOFEM_LOG_ATTRIBUTES("SYNC", LogManager::BitLineID | LogManager::BitScope);
301 * \endcode
302 *
303 */
304 #define MOFEM_LOG_ATTRIBUTES(channel, bit) \
305 { MoFEM::LogManager::addAttributes(channel, bit); }
306
307 /**
308 * @brief Log
309 * \ingroup mofem_log_manager
310 *
311 * \code
312 * MOFEM_LOG("WORLD", LogManager::SeverityLevel::inform) << "Hello world";
313 * \endcode
314 *
315 */
316 #define MOFEM_LOG(channel, severity) \
317 BOOST_LOG_SEV(MoFEM::LogManager::getLog(channel), severity)
318
319 #define MOFEM_LOG_C(channel, severity, format, ...) \
320 MOFEM_LOG(channel, severity) \
321 << MoFEM::LogManager::getCLikeFormatedString(format, __VA_ARGS__)
322
323 /** \brief Set scope
324 * \ingroup mofem_log_manager
325 *
326 * Macro for function scope markup. The scope name is constructed with help of
327 * compiler and contains the current function signature. The scope name is
328 * pushed to the end of the current thread scope list.
329 *
330 * Not all compilers have support for this macro. The exact form of the scope
331 * name may vary from one compiler to another.
332 */
333 #define MOFEM_LOG_FUNCTION() \
334 BOOST_LOG_NAMED_SCOPE_INTERNAL( \
335 BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), \
336 PETSC_FUNCTION_NAME, __FILE__, __LINE__, \
337 ::boost::log::attributes::named_scope_entry::function)
338
339 /**
340 * @brief Tag channel
341 * \ingroup mofem_log_manager
342 *
343 * Tag channel tag is set until MOFEM_LOG_CHANNEL is called, then new tag can
344 * be set.
345 *
346 */
347 #define MOFEM_LOG_TAG(channel, tag) MoFEM::LogManager::addTag(channel, tag);
348
349 /**
350 * @brief Synchronise "SYNC" channel
351 *
352 */
353 #define MOFEM_LOG_SYNCHRONISE(comm) \
354 PetscSynchronizedFlush(comm, MoFEM::LogManager::dummy_mofem_fd);
355
356 /**
357 * @brief Synchronise "SYNC" on curtain severity level
358 *
359 */
360 #define MOFEM_LOG_SEVERITY_SYNC(comm, severity) \
361 MOFEM_LOG("NULL", severity) << ([&] { \
362 MOFEM_LOG_SYNCHRONISE(comm); \
363 return 0; \
364 })()
365
366 /**
367 * @brief Synchronise "SYNC" channel
368 *
369 */
370 #define MOFEM_LOG_FILE_SYNCHRONISE(comm, fd) PetscSynchronizedFlush(comm, fd);
371
372 /**
373 * @brief Tag and log in channel
374 *
375 */
376 #define MOFEM_TAG_AND_LOG(channel, severity, tag) \
377 MOFEM_LOG_TAG(channel, tag) \
378 MOFEM_LOG(channel, severity)
379
380 /**
381 * @brief Tag and log in channel
382 *
383 */
384 #define MOFEM_TAG_AND_LOG_C(channel, severity, tag, format, ...) \
385 MOFEM_LOG_TAG(channel, tag) \
386 MOFEM_LOG_C(channel, severity, format, __VA_ARGS__)
387
388#endif //__LOGMANAGER_HPP__
389
390/**
391 * \defgroup mofem_log_manager
392 * \brief Log manager
393 *
394 * Logging manager based on Boost.Log
395 * (<a
396 * href="https://www.boost.org/doc/libs/1_63_0/libs/log/doc/html/index.html">Boost.Log
397 * v2</a>)
398 *
399 * \ingroup mofem
400 */
PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp)
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
SeverityLevel
Severity levels.
LogAttributesBits
Tag attributes switches.
static void addTag(LogManager::LoggerType &lg, const std::string tag)
Add tag to logger.
static void addAttributes(LogManager::LoggerType &lg, const int bit=0)
Add attributes to logger.
static LoggerType & getLog(const std::string channel)
Get logger by channel.
auto bit
set bit
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope", boost::log::attributes::named_scope::value_type) BOOST_LOG_ATTRIBUTE_KEYWORD(timeline
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
std::ostream & operator<<(std::ostream &os, const EntitiesFieldData::EntData &e)
Core (interface) class.
Definition Core.hpp:82
Log manager is used to build and partition problems.
static boost::shared_ptr< std::ostream > getStrmNull()
Get the strm null object.
boost::log::sources::severity_channel_logger< SeverityLevel, std::string > LoggerType
Definition of the channel logger.
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
LogManager(const MoFEM::Core &core)
static MoFEMErrorCode getOptions()
Get logger option.
static std::string getCLikeFormatedString(const char *fmt,...)
Converts formatted output to string.
virtual ~LogManager()=default
static void createDefaultSinks(MPI_Comm comm)
Create default sinks.
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
sinks::synchronous_sink< sinks::text_ostream_backend > SinkType
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[], va_list Argp)
Use to handle PETSc output.
static std::string getVLikeFormatedString(const char *fmt, va_list args)
Converts formatted output to string.
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.
static void recordFormatterDefault(logging::record_view const &rec, logging::formatting_ostream &strm)
Default record formatter.
static bool checkIfChannelExist(const std::string channel)
Check if channel exist.
static boost::shared_ptr< std::ostream > getStrmSelf()
Get the strm self object.
static std::string petscStringCache
MoFEM::Core & cOre
static FILE * dummy_mofem_fd
Dummy file pointer (DO NOT USE)
static boost::shared_ptr< InternalData > internalDataPtr
static constexpr std::array< char *const, error+1 > severityStrings
base class for all interface classes