PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
Logger.hh
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016 PISM Authors
2  *
3  * This file is part of PISM.
4  *
5  * PISM is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 3 of the License, or (at your option) any later
8  * version.
9  *
10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with PISM; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #ifndef _LOGGER_H_
20 #define _LOGGER_H_
21 
22 #include <string>
23 #include <memory>
24 
25 #include <mpi.h>
26 
27 namespace pism {
28 
29 typedef enum {WARNING=1, DEBUG2=2, DEBUG3=3, TRACE=4} LoggerLevel;
30 
31 //! A basic logging class.
32 /**
33  * The default implementation (message_impl()) just prints to `stdout` on rank 0 of the
34  * communicator.
35  *
36  * This class was created to make it possible to silence PISM's output when it is used as a library
37  * and make it possible to separate outputs from different PISM (IceModel, etc) instances running
38  * side by side.
39  */
40 class Logger {
41 public:
42  Logger(MPI_Comm com, int threshold);
43  virtual ~Logger();
44 
45  typedef std::shared_ptr<Logger> Ptr;
46  typedef std::shared_ptr<const Logger> ConstPtr;
47 
48  //! Print a message to the log.
49  /** Does nothing if `threshold` is greater than the value provided to the constructor or set using
50  * set_threshold().
51  */
52  void message(int threshold, const char format[], ...) const __attribute__((format(printf, 3, 4)));
53  void message(int threshold, const std::string &text) const;
54 
55  //! Print an error message to the log.
56  /** Always prints the message (regardless of the threshold). The base class implementation prints
57  * to stderr.
58  */
59  void error(const char format[], ...) const __attribute__((format(printf, 2, 3)));
60 
61  //! Set verbosity threshold.
62  void set_threshold(int level);
63 
64  //! Get verbosity threshold.
65  int get_threshold() const;
66 
67  //! Silence the logger.
68  /**
69  * This makes it possible to temporarily silence the logger and then re-enable it with the same
70  * threshold as before, but without explicitly storing the current threshold.
71  */
72  void disable() const;
73  //! (Re-)enable the logger.
74  void enable() const;
75 protected:
76  //! Do the hard work. Override this in a derived class to customize.
77  virtual void message_impl(const char buffer[]) const;
78  virtual void error_impl(const char buffer[]) const;
79  private:
80  struct Impl;
82  Logger(const Logger&);
83  Logger & operator=(const Logger &);
84 };
85 
86 //! A logger that accumulates messages and reports them as a string.
87 class StringLogger : public Logger {
88 public:
89  StringLogger(MPI_Comm com, int threshold);
90  virtual ~StringLogger();
91 
92  void reset();
93 
94  std::string get() const;
95 protected:
96  virtual void message_impl(const char buffer[]) const;
97  virtual void error_impl(const char buffer[]) const;
98 private:
99  struct Impl;
101 };
102 
103 Logger::Ptr logger_from_options(MPI_Comm com);
104 
105 } // end of namespace pism
106 
107 #endif /* _LOGGER_H_ */
void disable() const
Silence the logger.
Definition: Logger.cc:99
std::shared_ptr< const Logger > ConstPtr
Definition: Logger.hh:46
void void set_threshold(int level)
Set verbosity threshold.
Definition: Logger.cc:93
virtual ~Logger()
Definition: Logger.cc:45
void message(int threshold, const char format[],...) const __attribute__((format(printf
Print a message to the log.
Definition: Logger.cc:49
Impl * m_impl
Definition: Logger.hh:80
virtual void error_impl(const char buffer[]) const
Definition: Logger.cc:88
Logger(MPI_Comm com, int threshold)
Definition: Logger.cc:37
std::shared_ptr< Logger > Ptr
Definition: Logger.hh:45
virtual void message_impl(const char buffer[]) const
Do the hard work. Override this in a derived class to customize.
Definition: Logger.cc:72
void error(const char format[],...) const __attribute__((format(printf
Print an error message to the log.
Definition: Logger.cc:77
int get_threshold() const
Get verbosity threshold.
Definition: Logger.cc:96
void enable() const
(Re-)enable the logger.
Definition: Logger.cc:103
A basic logging class.
Definition: Logger.hh:40
A logger that accumulates messages and reports them as a string.
Definition: Logger.hh:87
Logger::Ptr logger_from_options(MPI_Comm com)
Definition: Logger.cc:107
std::string printf(const char *format,...)
LoggerLevel
Definition: Logger.hh:29
@ DEBUG2
Definition: Logger.hh:29
@ WARNING
Definition: Logger.hh:29
@ TRACE
Definition: Logger.hh:29
@ DEBUG3
Definition: Logger.hh:29
#define __attribute__(x)