v0.13.1
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 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 Create a sink object
154 *
155 * @param stream_ptr
156 * @param comm_filter
157 * @return boost::shared_ptr<SinkType>
158 */
159 static boost::shared_ptr<SinkType>
160 createSink(boost::shared_ptr<std::ostream> stream_ptr,
161 std::string comm_filter);
162
163 /**
164 * @brief Create default sinks
165 *
166 */
167 static void createDefaultSinks(MPI_Comm comm);
168
169 /** @brief Get logger option
170 *
171 * This function is called by MoFEM core when this interface is registred
172 * into database.
173 *
174 * @return MoFEMErrorCode
175 */
176 static MoFEMErrorCode getOptions();
177
178 /**
179 * @brief Dummy file pointer (DO NOT USE)
180 *
181 * \note This is for internal use only/
182 *
183 */
184 static FILE *dummy_mofem_fd;
185
186 /**
187 * @brief Use to handle PETSc output
188 *
189 * \note This is for internal use only/
190 *
191 * @param fd
192 * @param format
193 * @param Argp
194 * @return PetscErrorCode
195 */
196 static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[],
197 va_list Argp);
198
199 /**
200 * @brief Converts formatted output to string
201 *
202 * @param fmt
203 * @param args
204 * @return std::string
205 */
206 static std::string getVLikeFormatedString(const char *fmt, va_list args);
207
208 /**
209 * @brief Converts formatted output to string
210 *
211 * @param fmt
212 * @param args
213 * @return std::string
214 */
215 static std::string getCLikeFormatedString(const char *fmt, ...);
216
217 /**
218 * @brief Default record formatter
219 *
220 * @param rec
221 * @param strm
222 */
223 static void recordFormatterDefault(logging::record_view const &rec,
224 logging::formatting_ostream &strm);
225
226private:
228
229 struct InternalData;
230 static boost::shared_ptr<InternalData> internalDataPtr;
231
232 static std::string petscStringCache;
233
234};
235
237
238// The operator puts a human-friendly representation of the severity level to
239// the stream
240std::ostream &operator<<(std::ostream &strm,
241 const LogManager::SeverityLevel &level);
242
243namespace LogKeywords {
244
246BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
247BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
248BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
249BOOST_LOG_ATTRIBUTE_KEYWORD(proc_attr, "Proc", unsigned int)
251 boost::log::attributes::named_scope::value_type)
253 boost::log::attributes::timer::value_type)
254
255} // namespace LogKeywords
256
257} // namespace MoFEM
258
259extern "C" {
260PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp);
261}
262
263/**
264 * @brief Set and reset channel
265 * \ingroup mofem_log_manager
266 *
267 * \code
268 * MOFEM_LOG_CHANNEL("WORLD");
269 * \endcode
270 *
271 * Are three default type of channels, SELF, each processor prints to the
272 * standard output, WORLD, only processor one prints, and SYNC all processors
273 * prints synchronously.
274 *
275 *
276 */
277#define MOFEM_LOG_CHANNEL(channel) \
278 { MoFEM::LogManager::setLog(channel); }
279
280/**
281 * @brief Add attributes to channel
282 * \ingroup mofem_log_manager
283 *
284 * \code
285 * MOFEM_LOG_ATTRIBUTES("SYNC", LogManager::BitLineID | LogManager::BitScope);
286 * \endcode
287 *
288 */
289#define MOFEM_LOG_ATTRIBUTES(channel, bit) \
290 { MoFEM::LogManager::addAttributes(channel, bit); }
291
292/**
293 * @brief Log
294 * \ingroup mofem_log_manager
295 *
296 * \code
297 * MOFEM_LOG("WORLD", LogManager::SeverityLevel::inform) << "Hello world";
298 * \endcode
299 *
300 */
301#define MOFEM_LOG(channel, severity) \
302 BOOST_LOG_SEV(MoFEM::LogManager::getLog(channel), severity)
303
304#define MOFEM_LOG_C(channel, severity, format, ...) \
305 MOFEM_LOG(channel, severity) \
306 << MoFEM::LogManager::getCLikeFormatedString(format, __VA_ARGS__)
307
308/** \brief Set scope
309 * \ingroup mofem_log_manager
310 *
311 * Macro for function scope markup. The scope name is constructed with help of
312 * compiler and contains the current function signature. The scope name is
313 * pushed to the end of the current thread scope list.
314 *
315 * Not all compilers have support for this macro. The exact form of the scope
316 * name may vary from one compiler to another.
317 */
318#define MOFEM_LOG_FUNCTION() \
319 BOOST_LOG_NAMED_SCOPE_INTERNAL( \
320 BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), \
321 PETSC_FUNCTION_NAME, __FILE__, __LINE__, \
322 ::boost::log::attributes::named_scope_entry::function)
323
324/**
325 * @brief Tag channel
326 * \ingroup mofem_log_manager
327 *
328 * Tag channel tag is set until MOFEM_LOG_CHANNEL is called, then new tag can be
329 * set.
330 *
331 */
332#define MOFEM_LOG_TAG(channel, tag) MoFEM::LogManager::addTag(channel, tag);
333
334/**
335 * @brief Synchronise "SYNC" channel
336 *
337 */
338#define MOFEM_LOG_SYNCHRONISE(comm) \
339 PetscSynchronizedFlush(comm, MoFEM::LogManager::dummy_mofem_fd);
340
341/**
342 * @brief Tag and log in channel
343 *
344 */
345#define MOFEM_TAG_AND_LOG(channel, severity, tag) \
346 MOFEM_LOG_TAG(channel, tag) \
347 MOFEM_LOG(channel, severity)
348
349/**
350 * @brief Tag and log in channel
351 *
352 */
353#define MOFEM_TAG_AND_LOG_C(channel, severity, tag, format, ...) \
354 MOFEM_LOG_TAG(channel, tag) \
355 MOFEM_LOG_C(channel, severity, format, __VA_ARGS__)
356
357#endif //__LOGMANAGER_HPP__
358
359/**
360 * \defgroup mofem_log_manager
361 * \brief Log manager
362 *
363 * Logging manager based on Boost.Log
364 * (<a href="https://www.boost.org/doc/libs/1_63_0/libs/log/doc/html/index.html">Boost.Log v2</a>)
365 *
366 * \ingroup mofem
367 */
PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp)
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
Definition: LogManager.cpp:364
SeverityLevel
Severity levels.
Definition: LogManager.hpp:33
LogAttributesBits
Tag attributes switches.
Definition: LogManager.hpp:47
static void addTag(LogManager::LoggerType &lg, const std::string tag)
Add tag to logger.
Definition: LogManager.cpp:356
static void addAttributes(LogManager::LoggerType &lg, const int bit=0)
Add attributes to logger.
Definition: LogManager.cpp:334
static LoggerType & getLog(const std::string channel)
Get logger by channel.
Definition: LogManager.cpp:370
auto bit
set bit
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
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.
Definition: LogManager.hpp:26
static constexpr std::array< char *const, error+1 > severityStrings
Definition: LogManager.hpp:35
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
Definition: LogManager.cpp:279
static MoFEMErrorCode getOptions()
Get logger option.
Definition: LogManager.cpp:133
sinks::synchronous_sink< sinks::text_ostream_backend > SinkType
Definition: LogManager.hpp:66
static std::string getCLikeFormatedString(const char *fmt,...)
Converts formatted output to string.
Definition: LogManager.cpp:441
virtual ~LogManager()=default
static void createDefaultSinks(MPI_Comm comm)
Create default sinks.
Definition: LogManager.cpp:294
boost::log::sources::severity_channel_logger< SeverityLevel, std::string > LoggerType
Definition of the channel logger.
Definition: LogManager.hpp:64
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
Definition: LogManager.cpp:323
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: LogManager.cpp:126
static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[], va_list Argp)
Use to handle PETSc output.
Definition: LogManager.cpp:381
static std::string getVLikeFormatedString(const char *fmt, va_list args)
Converts formatted output to string.
Definition: LogManager.cpp:435
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.
Definition: LogManager.cpp:327
static void recordFormatterDefault(logging::record_view const &rec, logging::formatting_ostream &strm)
Default record formatter.
Definition: LogManager.cpp:185
static bool checkIfChannelExist(const std::string channel)
Check if channel exist.
Definition: LogManager.cpp:374
static boost::shared_ptr< std::ostream > getStrmSelf()
Get the strm self object.
Definition: LogManager.cpp:319
static std::string petscStringCache
Definition: LogManager.hpp:232
MoFEM::Core & cOre
Definition: LogManager.hpp:227
static FILE * dummy_mofem_fd
Dummy file pointer (DO NOT USE)
Definition: LogManager.hpp:184
static boost::shared_ptr< InternalData > internalDataPtr
Definition: LogManager.hpp:230
base class for all interface classes