Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
pcout.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Nicola De March <nicola.demarch@polimi.it>
22
23#ifndef VULPES_UTILS_PCOUT_HPP_
24#define VULPES_UTILS_PCOUT_HPP_
25
26#include <ios>
27#include <iostream>
28
30
31namespace vulpes {
32namespace utils {
33
37enum class LogLevel { ERROR = 0, INFO = 1, DEBUG = 2 };
38
47class pcout_t {
48 public:
54 explicit pcout_t(std::ostream& os = std::cout)
55 : os_(&os),
58
66 msg_level_ = lvl;
67 return *this;
68 }
69
73 template <typename T>
74 pcout_t& operator<<(const T& value) {
75 if (shouldPrint()) {
76 (*os_) << value;
77 }
79 return *this;
80 }
81
85 pcout_t& operator<<(std::ostream& (*pf)(std::ostream&)) {
86 if (shouldPrint()) {
87 (*os_) << pf;
88 }
90 return *this;
91 }
92
93 pcout_t& operator<<(std::ios& (*pf)(std::ios&)) {
94 if (shouldPrint()) {
95 (*os_) << pf;
96 }
98 return *this;
99 }
100
101 pcout_t& operator<<(std::ios_base& (*pf)(std::ios_base&)) {
102 if (shouldPrint()) {
103 (*os_) << pf;
104 }
106 return *this;
107 }
108
112 void flush() {
113 if (shouldPrint()) {
114 os_->flush();
115 }
116 }
117
118 private:
124 static constexpr LogLevel defaultLogLevel() {
125#ifdef VULPES_DEBUG
126 return LogLevel::DEBUG;
127#else
128 return LogLevel::INFO;
129#endif
130 }
131
135 bool shouldPrint() const {
136 const bool mpi_ok = !vulpes::mpi::isInitialized() || vulpes::mpi::root();
137
138 return mpi_ok && msg_level_ <= current_level_;
139 }
140
147
148 std::ostream* os_;
151};
152
153// Global instance: vulpes::utils::pcout
155
156} // namespace utils
157} // namespace vulpes
158
159// Global alias for convenience
162#endif // VULPES_UTILS_PCOUT_HPP_
Parallel-aware output stream.
Definition pcout.hpp:47
pcout_t(std::ostream &os=std::cout)
Construct a pcout object.
Definition pcout.hpp:54
pcout_t & level(LogLevel lvl)
Set the log level for the next << only.
Definition pcout.hpp:65
bool shouldPrint() const
Check whether the current message should be printed.
Definition pcout.hpp:135
pcout_t & operator<<(const T &value)
Generic stream output operator.
Definition pcout.hpp:74
pcout_t & operator<<(std::ios &(*pf)(std::ios &))
Definition pcout.hpp:93
void flush()
Explicitly flush the underlying stream.
Definition pcout.hpp:112
LogLevel msg_level_
Definition pcout.hpp:150
void resetMessageLevel()
Reset the message log level to INFO.
Definition pcout.hpp:146
static constexpr LogLevel defaultLogLevel()
Default log level depending on build type.
Definition pcout.hpp:124
pcout_t & operator<<(std::ostream &(*pf)(std::ostream &))
Stream manipulators (e.g. std::endl, std::fixed).
Definition pcout.hpp:85
std::ostream * os_
Definition pcout.hpp:148
pcout_t & operator<<(std::ios_base &(*pf)(std::ios_base &))
Definition pcout.hpp:101
LogLevel current_level_
Definition pcout.hpp:149
static bool root()
Return true if the rank in MPI_COMM_WORLD is zero.
Definition mpi_wrappers.hpp:132
static bool isInitialized()
Return true if MPI has been initialized.
Definition mpi_wrappers.hpp:92
Definition pcout.hpp:32
LogLevel
Logging levels supported by pcout.
Definition pcout.hpp:37
@ INFO
Definition pcout.hpp:37
@ ERROR
Definition pcout.hpp:37
@ DEBUG
Definition pcout.hpp:37
pcout_t pcout
Definition pcout.hpp:154
Definition cell_manager.hpp:38
vulpes::utils::pcout_t & pcout
Definition pcout.hpp:160