Flow123d  release_2.1.2-337-g6b7a56b
logger.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file logger.hh
15  * @brief
16  */
17 
18 #ifndef LOGGER_HH_
19 #define LOGGER_HH_
20 
21 
22 #include <iostream>
23 #include <algorithm> // for forward
24 #include <string> // for string
25 #include <sstream>
26 //#include <vector>
27 
28 #include "system/fmt/format.h"
29 
30 
31 
32 /**
33  * @brief Helper class, store mask specifying streams
34  *
35  * Defines masks of all used streams as static methods and allows combining and comparing masks using
36  * the overloaded operators.
37  */
38 class StreamMask {
39 public:
40  /// Empty constructor
42  : mask_(0) {}
43 
44  /// Constructor set \p mask_ value
45  StreamMask(int mask)
46  : mask_(mask) {}
47 
48  /// Predefined mask of std::cout output
49  static StreamMask cout;
50 
51  /// Predefined mask of std::cerr output
52  static StreamMask cerr;
53 
54  /// Predefined mask of log file output
55  static StreamMask log;
56 
57  // Overload & operator
58  StreamMask operator &(const StreamMask &other);
59 
60  // Overload | operator
61  StreamMask operator |(const StreamMask &other);
62 
63  // Overload () operator
64  int operator()(void);
65 
66 private:
67  int mask_;
68 };
69 
70 
71 /**
72  * @brief Class for storing logger messages.
73  *
74  * Allow define different levels of log messages and distinguish output streams
75  * for individual leves. These output streams are -
76  * - standard console output (std::cout)
77  * - standard error output (std::cerr)
78  * - file output (see \p LoggerOptions class)
79  *
80  * Logger distinguishes four type of levels -
81  * - warning: printed to standard error and file output
82  * - message: printed to standard console and file output
83  * - log: printed to file output
84  * - debug: printed to file output (level is used only in debug mode)
85  *
86  * File output is optional. See \p LoggerOptions for setting this output stream.
87  *
88  * <b>Example of Logger usage:</b>
89  *
90  * For individual levels are defined macros -
91  * - MessageOut()
92  * - WarningOut()
93  * - LogOut()
94  * - DebugOut()
95  * that ensure display of actual code point (source file, line and function).
96  *
97  * Logger message is created by using an operator << and allow to add each type
98  * that has override this operator. Message is terminated with manipulator
99  * std::endl. Implicitly logger message is printed only in processor with rank
100  * zero. If necessary printed message for all process, it provides a method
101  * every_proc().
102  *
103  * Examples of logger messages formating:
104  *
105  @code
106  MessageOut() << "End of simulation at time: " << secondary_eq->solved_time() << "\n";
107  WarningOut() << "Unprocessed key '" << key_name << "' in Record '" << rec->type_name() << "'." << "\n";
108  LogOut() << "Write output to output stream: " << this->_base_filename << " for time: " << time << "\n";
109  DebugOut() << "Calling 'initialize' of empty equation '" << typeid(*this).name() << "'." << "\n";
110  @endcode
111  *
112  * Logger message can be created by more than one line ("\n" can be used multiple
113  * times):
114  *
115  @code
116  MessageOut() << "Start time: " << this->start_time() << "\n" << "End time: " << this->end_time() << "\n";
117  @endcode
118  *
119  * Or Logger allow using fmtlib functionality for simpler formatting of message:
120  *
121  @code
122  MessageOut() << fmt::format("Start time: {}\nEnd time: {}\n", this->start_time(), this->end_time());
123  MessageOut().fmt("Start time: {}\nEnd time: {}\n", this->start_time(), this->end_time());
124  @endcode
125  *
126  * In some cases message can be printed for all processes:
127  *
128  @code
129  MessageOut().every_proc() << "Size distributed at process: " << distr->lsize() << "\n";
130  @endcode
131  *
132  * Implementation of Logger does not support manipulator std::endl. Please, use "\n" instead.
133  */
134 class Logger : public std::ostream {
135 public:
136  /// Enum of types of Logger messages.
137  enum MsgType {
138  warning = 0,
139  message = 1,
140  log = 2,
141  debug = 3,
142  error = 4
143  };
144 
145  /// Return string value of given MsgType in full or shorter format (e.g. "WARNING" of "Wrn")
146  static const std::string msg_type_string(MsgType msg_type, bool full_format = true);
147 
148  /// Constructor.
149  Logger(MsgType type);
150 
151  /// Stores values for printing out line number, function, etc
152  Logger& set_context(const char* file_name, const char* function, const int line);
153 
154  /// Set flag every_process_ to true
155  Logger& every_proc();
156 
157  /**
158  * @brief Allow use functionality of fmtlib for formating message.
159  *
160  * See examples in description of Logger class.
161  */
162  template<class... T>
163  Logger& fmt(T&&... t)
164  {
165  return *this << fmt::format(std::forward<T>(t)...);
166  }
167 
168  /// Destructor.
169  ~Logger();
170 
171 
172 private:
173  /// Set @p streams_mask_ according to the type of message.
174  void set_mask();
175 
176  /// Print formated message to given screen stream if mask corresponds with @p streams_mask_.
177  void print_to_screen(std::ostream& stream, std::stringstream& scr_stream, StreamMask mask);
178 
179  /// Print formated message to given file stream if mask corresponds with @p streams_mask_.
180  void print_to_file(std::ofstream& stream, std::stringstream& file_stream, StreamMask mask);
181 
182  /// Print header to screen stream, helper method called from \p print_to_screen.
183  bool print_screen_header(std::ostream& stream, std::stringstream& scr_stream);
184 
185  /// Print header to file stream, helper method called from \p print_to_file.
186  void print_file_header(std::ofstream& stream, std::stringstream& file_stream);
187 
188  std::stringstream cout_stream_; ///< Store messages printed to cout output stream
189  std::stringstream cerr_stream_; ///< Store messages printed to cerr output stream
190  std::stringstream file_stream_; ///< Store messages printed to file
191 
192  MsgType type_; ///< Type of message.
193  bool every_process_; ///< Flag marked if log message is printing for all processes or only for zero process.
194  std::string file_name_; ///< Actual file.
195  std::string function_; ///< Actual function.
196  int line_; ///< Actual line.
197  std::string date_time_; ///< Actual date and time.
198  int mpi_rank_; ///< Actual process (if MPI is supported)
199  StreamMask streams_mask_; ///< Mask of logger, specifies streams in actual time into which to be written
200  StreamMask full_streams_mask_; ///< Mask of logger, specifies all streams into which to be written in logger message
201 
202  template <class T>
203  friend Logger &operator<<(Logger & log, const T & x);
204  friend Logger &operator<<(Logger & log, std::ostream & (*pf) (std::ostream &) );
205  friend Logger &operator<<(Logger & log, StreamMask mask);
206 };
207 
208 
209 Logger &operator<<(Logger & log, StreamMask mask);
210 
211 
212 Logger &operator<<(Logger & log, std::ostream & (*pf) (std::ostream &) );
213 
214 
215 template <class T>
216 Logger &operator<<(Logger & log, const T & x)
217 {
218  if ( (log.streams_mask_ & StreamMask::cout)() ) log.cout_stream_ << x;
219  if ( (log.streams_mask_ & StreamMask::cerr)() ) log.cerr_stream_ << x;
220  if ( (log.streams_mask_ & StreamMask::log )() ) log.file_stream_ << x;
221  return log;
222 }
223 
224 
225 
226 
227 
228 
229 /// Internal macro defining universal record of log
230 #define _LOG(type) \
231  Logger( type ).set_context( __FILE__, __func__, __LINE__)
232 /// Macro defining 'message' record of log
233 #define MessageOut() \
234  _LOG( Logger::MsgType::message )
235 /// Macro defining 'warning' record of log
236 #define WarningOut() \
237  _LOG( Logger::MsgType::warning )
238 /// Macro defining 'log' record of log
239 #define LogOut() \
240  _LOG( Logger::MsgType::log )
241 /// Macro defining 'debug' record of log
242 #define DebugOut() \
243  _LOG( Logger::MsgType::debug )
244 
245 /**
246  * Print variable name and value.
247  * Usage:
248  * DebugOut() << print_var(x) << print_var(y)
249  */
250 #define print_var(var) \
251  std::string(#var) << "=" << (var) << ", "
252 
253 
254 
255 
256 #endif /* LOGGER_HH_ */
Class for storing logger messages.
Definition: logger.hh:134
static StreamMask cout
Predefined mask of std::cout output.
Definition: logger.hh:49
int operator()(void)
Definition: logger.cc:54
std::stringstream cout_stream_
Store messages printed to cout output stream.
Definition: logger.hh:188
Logger & operator<<(Logger &log, StreamMask mask)
Definition: logger.cc:269
StreamMask streams_mask_
Mask of logger, specifies streams in actual time into which to be written.
Definition: logger.hh:199
StreamMask()
Empty constructor.
Definition: logger.hh:41
std::stringstream cerr_stream_
Store messages printed to cerr output stream.
Definition: logger.hh:189
std::string function_
Actual function.
Definition: logger.hh:195
StreamMask operator|(const StreamMask &other)
Definition: logger.cc:49
Logger & fmt(T &&...t)
Allow use functionality of fmtlib for formating message.
Definition: logger.hh:163
StreamMask(int mask)
Constructor set mask_ value.
Definition: logger.hh:45
static StreamMask cerr
Predefined mask of std::cerr output.
Definition: logger.hh:52
static StreamMask log
Predefined mask of log file output.
Definition: logger.hh:55
StreamMask operator&(const StreamMask &other)
Definition: logger.cc:44
Helper class, store mask specifying streams.
Definition: logger.hh:38
std::string date_time_
Actual date and time.
Definition: logger.hh:197
int mask_
Definition: logger.hh:67
bool every_process_
Flag marked if log message is printing for all processes or only for zero process.
Definition: logger.hh:193
int line_
Actual line.
Definition: logger.hh:196
std::stringstream file_stream_
Store messages printed to file.
Definition: logger.hh:190
StreamMask full_streams_mask_
Mask of logger, specifies all streams into which to be written in logger message. ...
Definition: logger.hh:200
std::string file_name_
Actual file.
Definition: logger.hh:194
int mpi_rank_
Actual process (if MPI is supported)
Definition: logger.hh:198
MsgType type_
Type of message.
Definition: logger.hh:192
MsgType
Enum of types of Logger messages.
Definition: logger.hh:137