PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Logger.hh
Go to the documentation of this file.
1/* Copyright (C) 2015, 2016, 2025 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
27namespace pism {
28
29typedef 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 */
40class Logger {
41public:
42 Logger(MPI_Comm com, int threshold);
43 virtual ~Logger();
44
45 //! Print a message to the log.
46 /** Does nothing if `threshold` is greater than the value provided to the constructor or set using
47 * set_threshold().
48 */
49 void message(int threshold, const char format[], ...) const __attribute__((format(printf, 3, 4)));
50 void message(int threshold, const std::string &text) const;
51
52 //! Print an error message to the log.
53 /** Always prints the message (regardless of the threshold). The base class implementation prints
54 * to stderr.
55 */
56 void error(const char format[], ...) const __attribute__((format(printf, 2, 3)));
57
58 //! Set verbosity threshold.
59 void set_threshold(int level);
60
61 //! Get verbosity threshold.
62 int get_threshold() const;
63
64 //! Silence the logger.
65 /**
66 * This makes it possible to temporarily silence the logger and then re-enable it with the same
67 * threshold as before, but without explicitly storing the current threshold.
68 */
69 void disable() const;
70 //! (Re-)enable the logger.
71 void enable() const;
72protected:
73 //! Do the hard work. Override this in a derived class to customize.
74 virtual void message_impl(const char buffer[]) const;
75 virtual void error_impl(const char buffer[]) const;
76 private:
77 struct Impl;
79 Logger(const Logger&);
81};
82
83//! A logger that accumulates messages and reports them as a string.
84class StringLogger : public Logger {
85public:
86 StringLogger(MPI_Comm com, int threshold);
87 virtual ~StringLogger();
88
89 void reset();
90
91 std::string get() const;
92protected:
93 virtual void message_impl(const char buffer[]) const;
94 virtual void error_impl(const char buffer[]) const;
95private:
96 struct Impl;
98};
99
100} // end of namespace pism
101
102#endif /* _LOGGER_H_ */
void disable() const
Silence the logger.
Definition Logger.cc:100
void void set_threshold(int level)
Set verbosity threshold.
Definition Logger.cc:94
virtual ~Logger()
Definition Logger.cc:46
void message(int threshold, const char format[],...) const __attribute__((format(printf
Print a message to the log.
Definition Logger.cc:50
Logger & operator=(const Logger &)
Impl * m_impl
Definition Logger.hh:78
virtual void error_impl(const char buffer[]) const
Definition Logger.cc:89
Logger(const Logger &)
virtual void message_impl(const char buffer[]) const
Do the hard work. Override this in a derived class to customize.
Definition Logger.cc:73
void error(const char format[],...) const __attribute__((format(printf
Print an error message to the log.
Definition Logger.cc:78
int get_threshold() const
Get verbosity threshold.
Definition Logger.cc:97
void enable() const
(Re-)enable the logger.
Definition Logger.cc:104
A basic logging class.
Definition Logger.hh:40
virtual void error_impl(const char buffer[]) const
Definition Logger.cc:125
virtual void message_impl(const char buffer[]) const
Do the hard work. Override this in a derived class to customize.
Definition Logger.cc:121
virtual ~StringLogger()
Definition Logger.cc:117
std::string get() const
Definition Logger.cc:129
A logger that accumulates messages and reports them as a string.
Definition Logger.hh:84
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)