v0.9.2
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 Add tag to logger
119  * \ingroup mofem_log_manager
120  *
121  * @param lg
122  * @param tag
123  */
124  static void addTag(LogManager::LoggerType &lg, const std::string tag);
125 
126  /**
127  * @brief Add tag to channel
128  * \ingroup mofem_log_manager
129  *
130  * @param channel
131  * @param tag
132  */
133  static void addTag(const std::string channel, const std::string tag);
134 
135  /**
136  * @brief Get the strm self object
137  *
138  * @return boost::shared_ptr<std::ostream>
139  */
140  static boost::shared_ptr<std::ostream> getStrmSelf();
141 
142  /**
143  * @brief Get the strm world object
144  *
145  * @return boost::shared_ptr<std::ostream>
146  */
147  static boost::shared_ptr<std::ostream> getStrmWorld();
148 
149  /**
150  * @brief Get the strm sync object
151  *
152  * @return boost::shared_ptr<std::ostream>
153  */
154  static boost::shared_ptr<std::ostream> getStrmSync();
155 
156  /**
157  * @brief Create a sink object
158  *
159  * @param stream_ptr
160  * @param comm_filter
161  * @return boost::shared_ptr<SinkType>
162  */
163  static boost::shared_ptr<SinkType>
164  createSink(boost::shared_ptr<std::ostream> stream_ptr,
165  std::string comm_filter);
166 
167  /**
168  * @brief Create default sinks
169  *
170  */
171  static void createDefaultSinks(MPI_Comm comm);
172 
173  /** @brief Get logger option
174  *
175  * This function is called by MoFEM core when this interface is registred
176  * into database.
177  *
178  * @return MoFEMErrorCode
179  */
180  static MoFEMErrorCode getOptions();
181 
182  /**
183  * @brief Dummy file pointer (DO NOT USE)
184  *
185  * \note This is for internal use only/
186  *
187  */
188  static FILE *dummy_mofem_fd;
189 
190  /**
191  * @brief Use to handle PETSc output
192  *
193  * \note This is for internal use only/
194  *
195  * @param fd
196  * @param format
197  * @param Argp
198  * @return PetscErrorCode
199  */
200  static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[],
201  va_list Argp);
202 
203  /**
204  * @brief Converts formatted output to string
205  *
206  * @param fmt
207  * @param args
208  * @return std::string
209  */
210  static std::string getVLikeFormatedString(const char *fmt, va_list args);
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 getCLikeFormatedString(const char *fmt, ...);
220 
221  /**
222  * @brief Default record formatter
223  *
224  * @param rec
225  * @param strm
226  */
227  static void recordFormatterDefault(logging::record_view const &rec,
228  logging::formatting_ostream &strm);
229 
230 private:
232 
233  struct InternalData;
234  static boost::shared_ptr<InternalData> internalDataPtr;
235 
236  static std::string petscStringCache;
237 
238 };
239 
240 // The operator puts a human-friendly representation of the severity level to
241 // the stream
242 std::ostream &operator<<(std::ostream &strm,
243  const LogManager::SeverityLevel &level);
244 
245 namespace LogKeywords {
246 
248 BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
249 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
250 BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
251 BOOST_LOG_ATTRIBUTE_KEYWORD(proc_attr, "Proc", unsigned int)
252 BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope",
253  boost::log::attributes::named_scope::value_type)
255  boost::log::attributes::timer::value_type)
256 
257 } // namespace LogKeywords
258 
259 } // namespace MoFEM
260 
261 extern "C" {
262 PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp);
263 }
264 
265 /**
266  * @brief Set and reset channel
267  * \ingroup mofem_log_manager
268  *
269  * \code
270  * MOFEM_LOG_CHANNEL("WORLD");
271  * \endcode
272  *
273  * Are three default type of channels, SELF, ech processor prints to the
274  * standard output, WORLD, only processor one prints, and SYNC all processors
275  * prints synchronously.
276  *
277  *
278  */
279 #define MOFEM_LOG_CHANNEL(channel) \
280  { MoFEM::LogManager::setLog(channel); }
281 
282 /**
283  * @brief Add attributes to channel
284  * \ingroup mofem_log_manager
285  *
286  * \code
287  * MOFEM_LOG_ATTRIBUTES("SYNC", LogManager::BitLineID | LogManager::BitScope);
288  * \endcode
289  *
290  */
291 #define MOFEM_LOG_ATTRIBUTES(channel, bit) \
292  { MoFEM::LogManager::addAttributes(channel, bit); }
293 
294 /**
295  * @brief Log
296  * \ingroup mofem_log_manager
297  *
298  * \code
299  * MOFEM_LOG("WORLD", LogManager::SeverityLevel::inform) << "Hello world";
300  * \endcode
301  *
302  */
303 #define MOFEM_LOG(channel, severity) \
304  BOOST_LOG_SEV(MoFEM::LogManager::getLog(channel), severity)
305 
306 #define MOFEM_LOG_C(channel, severity, format, ...) \
307  MOFEM_LOG(channel, severity) \
308  << MoFEM::LogManager::getCLikeFormatedString(format, __VA_ARGS__)
309 
310 /** \brief Set scope
311  * \ingroup mofem_log_manager
312  *
313  * Macro for function scope markup. The scope name is constructed with help of
314  * compiler and contains the current function signature. The scope name is
315  * pushed to the end of the current thread scope list.
316  *
317  * Not all compilers have support for this macro. The exact form of the scope
318  * name may vary from one compiler to another.
319  */
320 #define MOFEM_LOG_FUNCTION() \
321  BOOST_LOG_NAMED_SCOPE_INTERNAL( \
322  BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), \
323  PETSC_FUNCTION_NAME, __FILE__, __LINE__, \
324  ::boost::log::attributes::named_scope_entry::function)
325 
326 /**
327  * @brief Tag channel
328  * \ingroup mofem_log_manager
329  *
330  * Tag channel tag is set until MOFEM_LOG_CHANNEL is called, then new tag can be
331  * set.
332  *
333  */
334 #define MOFEM_LOG_TAG(channel, tag) MoFEM::LogManager::addTag(channel, tag);
335 
336 /**
337  * @brief Synchronise "SYNC" channel
338  *
339  */
340 #define MOFEM_LOG_SYNCHORMISE(comm) \
341  PetscSynchronizedFlush(comm, MoFEM::LogManager::dummy_mofem_fd);
342 
343 #endif //__LOGMANAGER_HPP__
344 
345 /**
346  * \defgroup mofem_log_manager
347  * \brief Log manager
348  *
349  * Logging manager based on Boost.Log
350  * (<a href="https://www.boost.org/doc/libs/1_63_0/libs/log/doc/html/index.html">Boost.Log v2</a>)
351  *
352  * \ingroup mofem
353  */
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
Definition: LogManager.cpp:322
MoFEM interface unique ID.
SeverityLevel
Severity levels.
Definition: LogManager.hpp:46
static const MOFEMuuid IDD_MOFEMLogManager
Definition: LogManager.hpp:31
static void createDefaultSinks(MPI_Comm comm)
Create default sinks.
Definition: LogManager.cpp:293
static PetscErrorCode logPetscFPrintf(FILE *fd, const char format[], va_list Argp)
Use to handle PETSc output.
Definition: LogManager.cpp:375
base class for all interface classes
static MoFEMErrorCode getOptions()
Get logger option.
Definition: LogManager.cpp:137
static boost::shared_ptr< std::ostream > getStrmSelf()
Get the strm self object.
Definition: LogManager.cpp:318
PetscErrorCode PetscVFPrintfDefault(FILE *fd, const char *format, va_list Argp)
virtual ~LogManager()=default
Core (interface) class.
Definition: Core.hpp:50
static std::string getCLikeFormatedString(const char *fmt,...)
Converts formatted output to string.
Definition: LogManager.cpp:435
static void addAttributes(LogManager::LoggerType &lg, const int bit=0)
Add attributes to logger.
Definition: LogManager.cpp:333
static FILE * dummy_mofem_fd
Dummy file pointer (DO NOT USE)
Definition: LogManager.hpp:188
std::bitset< BITINTERFACEUID_SIZE > BitIntefaceId
Definition: Types.hpp:55
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
Definition: LogManager.cpp:278
std::ostream & operator<<(std::ostream &os, const DataForcesAndSourcesCore::EntData &e)
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
Definition: LogManager.cpp:363
static void addTag(LogManager::LoggerType &lg, const std::string tag)
Add tag to logger.
Definition: LogManager.cpp:355
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.
Definition: LogManager.cpp:326
LogAttributesBits
Tag attributes switches.
Definition: LogManager.hpp:60
static constexpr std::array< char *const, error+1 > severityStrings
Definition: LogManager.hpp:48
static boost::shared_ptr< InternalData > internalDataPtr
Definition: LogManager.hpp:233
Log manager is used to build and partition problems.
Definition: LogManager.hpp:39
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
sinks::synchronous_sink< sinks::text_ostream_backend > SinkType
Definition: LogManager.hpp:79
MoFEM::Core & cOre
Definition: LogManager.hpp:231
static LoggerType & getLog(const std::string channel)
Get logger by channel.
Definition: LogManager.cpp:369
static void recordFormatterDefault(logging::record_view const &rec, logging::formatting_ostream &strm)
Default record formatter.
Definition: LogManager.cpp:184
boost::log::sources::severity_channel_logger< SeverityLevel, std::string > LoggerType
Definition of the channel logger.
Definition: LogManager.hpp:77
static std::string getVLikeFormatedString(const char *fmt, va_list args)
Converts formatted output to string.
Definition: LogManager.cpp:429
MoFEMErrorCode query_interface(const MOFEMuuid &uuid, UnknownInterface **iface) const
Definition: LogManager.cpp:125
static std::string petscStringCache
Definition: LogManager.hpp:236
LogManager(const MoFEM::Core &core)
Definition: LogManager.cpp:122