PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Logger.cc
Go to the documentation of this file.
1/* Copyright (C) 2015, 2017, 2021, 2022, 2023, 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
20#include <memory>
21#include <unistd.h>
22#include <sstream>
23#include <stdarg.h>
24#include <petscsys.h>
25
26#include "pism/util/Logger.hh"
27#include "pism/util/pism_options.hh"
28#include "pism/util/error_handling.hh"
29
30namespace pism {
31
33 MPI_Comm com;
34 bool enabled;
36};
37
38Logger::Logger(MPI_Comm com, int threshold)
39 : m_impl(new Impl) {
40
41 m_impl->com = com;
42 m_impl->enabled = true;
43 m_impl->threshold = threshold;
44}
45
47 delete m_impl;
48}
49
50void Logger::message(int threshold, const char format[], ...) const {
51 if ((not m_impl->enabled) or threshold > m_impl->threshold) {
52 return;
53 }
54
55 char buffer[8192];
56 va_list argp;
57
58 va_start(argp, format);
59 vsnprintf(buffer, sizeof(buffer), format, argp);
60 va_end(argp);
61
62 message_impl(buffer);
63}
64
65void Logger::message(int threshold, const std::string &text) const {
66 if ((not m_impl->enabled) or threshold > m_impl->threshold) {
67 return;
68 }
69
70 message_impl(text.c_str());
71}
72
73void Logger::message_impl(const char buffer[]) const {
74 PetscErrorCode ierr = PetscFPrintf(m_impl->com, PETSC_STDOUT, "%s", buffer);
75 PISM_CHK(ierr, "PetscFPrintf");
76}
77
78void Logger::error(const char format[], ...) const {
79 char buffer[8192];
80 va_list argp;
81
82 va_start(argp, format);
83 vsnprintf(buffer, sizeof(buffer), format, argp);
84 va_end(argp);
85
86 error_impl(buffer);
87}
88
89void Logger::error_impl(const char buffer[]) const {
90 PetscErrorCode ierr = PetscFPrintf(m_impl->com, stderr, "%s", buffer);
91 PISM_CHK(ierr, "PetscFPrintf");
92}
93
94void Logger::set_threshold(int level) {
95 m_impl->threshold = level;
96}
98 return m_impl->threshold;
99}
100void Logger::disable() const {
101 m_impl->enabled = false;
102}
103
104void Logger::enable() const {
105 m_impl->enabled = true;
106}
107
109 std::ostringstream data;
110};
111
112StringLogger::StringLogger(MPI_Comm com, int threshold)
113 : Logger(com, threshold), m_impl(new Impl) {
114 // empty
115}
116
120
121void StringLogger::message_impl(const char buffer[]) const {
122 m_impl->data << buffer;
123}
124
125void StringLogger::error_impl(const char buffer[]) const {
126 m_impl->data << buffer;
127}
128
129std::string StringLogger::get() const {
130 return m_impl->data.str();
131}
132
134 m_impl->data.str("");
135}
136
137
138} // end of namespace pism
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
Impl * m_impl
Definition Logger.hh:78
virtual void error_impl(const char buffer[]) const
Definition Logger.cc:89
Logger(MPI_Comm com, int threshold)
Definition Logger.cc:38
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
StringLogger(MPI_Comm com, int threshold)
Definition Logger.cc:112
#define PISM_CHK(errcode, name)
std::ostringstream data
Definition Logger.cc:109