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