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