PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
Logger.cc
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2017, 2021 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 "Logger.hh"
26 #include "pism_utilities.hh"
27 #include "pism_options.hh"
28 #include "error_handling.hh"
29 
30 namespace pism {
31 
32 struct Logger::Impl {
33  MPI_Comm com;
34  bool enabled;
35  int threshold;
36 };
37 
38 Logger::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 
50 void 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 
65 void 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 
73 void 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 
78 void 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 
89 void Logger::error_impl(const char buffer[]) const {
90  PetscErrorCode ierr = PetscFPrintf(m_impl->com, stderr, buffer);
91  PISM_CHK(ierr, "PetscFPrintf");
92 }
93 
94 void Logger::set_threshold(int level) {
95  m_impl->threshold = level;
96 }
97 int Logger::get_threshold() const {
98  return m_impl->threshold;
99 }
100 void Logger::disable() const {
101  m_impl->enabled = false;
102 }
103 
104 void Logger::enable() const {
105  m_impl->enabled = true;
106 }
107 
109  Logger::Ptr result(new Logger(com, 2));
110 
111  options::Integer verbosity("-verbose", "set logger verbosity threshold",
112  result->get_threshold());
113 
114  result->set_threshold(verbosity);
115 
116  return result;
117 }
118 
120  std::ostringstream data;
121 };
122 
123 StringLogger::StringLogger(MPI_Comm com, int threshold)
124  : Logger(com, threshold), m_impl(new Impl) {
125  // empty
126 }
127 
129  delete m_impl;
130 }
131 
132 void StringLogger::message_impl(const char buffer[]) const {
133  m_impl->data << buffer;
134 }
135 
136 void StringLogger::error_impl(const char buffer[]) const {
137  m_impl->data << buffer;
138 }
139 
140 std::string StringLogger::get() const {
141  return m_impl->data.str();
142 }
143 
145  m_impl->data.str("");
146 }
147 
148 
149 } // 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:80
virtual void error_impl(const char buffer[]) const
Definition: Logger.cc:89
Logger(MPI_Comm com, int threshold)
Definition: Logger.cc:38
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: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:136
virtual void message_impl(const char buffer[]) const
Do the hard work. Override this in a derived class to customize.
Definition: Logger.cc:132
virtual ~StringLogger()
Definition: Logger.cc:128
std::string get() const
Definition: Logger.cc:140
StringLogger(MPI_Comm com, int threshold)
Definition: Logger.cc:123
#define PISM_CHK(errcode, name)
Logger::Ptr logger_from_options(MPI_Comm com)
Definition: Logger.cc:108
MPI_Comm com
Definition: Logger.cc:33
std::ostringstream data
Definition: Logger.cc:120