Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.2-d6b3a29ca committed by Constantine Khrulev on 2025-03-28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Logger.cc
Go to the documentation of this file.
1/* Copyright (C) 2015, 2017, 2021, 2022, 2023 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 <unistd.h>
21#include <sstream>
22#include <stdarg.h>
23#include <petscsys.h>
24
25#include "pism/util/Logger.hh"
26#include "pism/util/pism_options.hh"
27#include "pism/util/error_handling.hh"
28
29namespace pism {
30
32 MPI_Comm com;
33 bool enabled;
35};
36
37Logger::Logger(MPI_Comm com, int threshold)
38 : m_impl(new Impl) {
39
40 m_impl->com = com;
41 m_impl->enabled = true;
42 m_impl->threshold = threshold;
43}
44
46 delete m_impl;
47}
48
49void Logger::message(int threshold, const char format[], ...) const {
50 if ((not m_impl->enabled) or threshold > m_impl->threshold) {
51 return;
52 }
53
54 char buffer[8192];
55 va_list argp;
56
57 va_start(argp, format);
58 vsnprintf(buffer, sizeof(buffer), format, argp);
59 va_end(argp);
60
61 message_impl(buffer);
62}
63
64void Logger::message(int threshold, const std::string &text) const {
65 if ((not m_impl->enabled) or threshold > m_impl->threshold) {
66 return;
67 }
68
69 message_impl(text.c_str());
70}
71
72void Logger::message_impl(const char buffer[]) const {
73 PetscErrorCode ierr = PetscFPrintf(m_impl->com, PETSC_STDOUT, "%s", buffer);
74 PISM_CHK(ierr, "PetscFPrintf");
75}
76
77void Logger::error(const char format[], ...) const {
78 char buffer[8192];
79 va_list argp;
80
81 va_start(argp, format);
82 vsnprintf(buffer, sizeof(buffer), format, argp);
83 va_end(argp);
84
85 error_impl(buffer);
86}
87
88void Logger::error_impl(const char buffer[]) const {
89 PetscErrorCode ierr = PetscFPrintf(m_impl->com, stderr, "%s", buffer);
90 PISM_CHK(ierr, "PetscFPrintf");
91}
92
93void Logger::set_threshold(int level) {
94 m_impl->threshold = level;
95}
97 return m_impl->threshold;
98}
99void Logger::disable() const {
100 m_impl->enabled = false;
101}
102
103void Logger::enable() const {
104 m_impl->enabled = true;
105}
106
108 Logger::Ptr result(new Logger(com, 2));
109
110 options::Integer verbosity("-verbose", "set logger verbosity threshold",
111 result->get_threshold());
112
113 result->set_threshold(verbosity);
114
115 return result;
116}
117
119 std::ostringstream data;
120};
121
122StringLogger::StringLogger(MPI_Comm com, int threshold)
123 : Logger(com, threshold), m_impl(new Impl) {
124 // empty
125}
126
130
131void StringLogger::message_impl(const char buffer[]) const {
132 m_impl->data << buffer;
133}
134
135void StringLogger::error_impl(const char buffer[]) const {
136 m_impl->data << buffer;
137}
138
139std::string StringLogger::get() const {
140 return m_impl->data.str();
141}
142
144 m_impl->data.str("");
145}
146
147
148} // end of namespace pism
void disable() const
Silence the logger.
Definition Logger.cc:99
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:81
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
virtual void error_impl(const char buffer[]) const
Definition Logger.cc:135
virtual void message_impl(const char buffer[]) const
Do the hard work. Override this in a derived class to customize.
Definition Logger.cc:131
virtual ~StringLogger()
Definition Logger.cc:127
std::string get() const
Definition Logger.cc:139
StringLogger(MPI_Comm com, int threshold)
Definition Logger.cc:122
#define PISM_CHK(errcode, name)
Logger::Ptr logger_from_options(MPI_Comm com)
Definition Logger.cc:107
std::ostringstream data
Definition Logger.cc:119