TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
report_stream.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include <ostream>
28#include <streambuf>
29#include <string>
30
31namespace triqs::utility {
32
37
46 std::ostream *out;
47 int verbosity;
48
49 public:
56 report_stream(std::ostream *out_, int verbosity_ = 1) : out(out_), verbosity(verbosity_) {}
57
64 report_stream(std::ostream &out_, int verbosity_ = 1) : out(&out_), verbosity(verbosity_) {}
65
72 report_stream operator()(int n) const { return {out, verbosity - n + 1}; }
73
81 template <class T> report_stream &operator<<(T const &x) {
82 if (verbosity > 0) (*out) << x;
83 return *this;
84 }
85
92 report_stream &operator<<(std::ostream &(*manip)(std::ostream &)) {
93 if (verbosity > 0) manip(*out);
94 return *this;
95 }
96 };
97
104 class indented_ostream : public std::ostream {
105 // Custom streambuf that prepends a fixed number of spaces to every line.
106 class indented_streambuf : public std::streambuf {
107 std::streambuf *dest;
108 std::string head;
109 bool at_line_start = true;
110
111 public:
112 indented_streambuf(std::streambuf *dest, int indent) : dest(dest), head(indent, ' ') {}
113
114 protected:
115 int_type overflow(int_type c) override {
116 if (c == EOF) return !EOF;
117 if (at_line_start && c != '\n') dest->sputn(head.c_str(), long(head.size()));
118 at_line_start = (c == '\n');
119 return dest->sputc(c); // NOLINT
120 }
121 int sync() override { return dest->pubsync(); }
122 };
123 indented_streambuf buffer;
124
125 public:
132 indented_ostream(std::ostream &os, int indent) : std::ostream(&buffer), buffer(os.rdbuf(), indent) {}
133 };
134
136
137} // namespace triqs::utility
report_stream(std::ostream *out_, int verbosity_=1)
Construct a report stream from a pointer to an ostream and a verbosity level.
indented_ostream(std::ostream &os, int indent)
Construct an indented stream on top of an existing ostream with a given indentation width.
report_stream(std::ostream *out_, int verbosity_=1)
Construct a report stream from a pointer to an ostream and a verbosity level.
report_stream(std::ostream &out_, int verbosity_=1)
Construct a report stream from a reference to an ostream and a verbosity level.
report_stream & operator<<(std::ostream &(*manip)(std::ostream &))
Streaming operator that accepts manipulators like std::endl.
report_stream & operator<<(T const &x)
Streaming operator to write the given argument if the current verbosity is greater than zero.
report_stream operator()(int n) const
Return a child report stream whose verbosity has been reduced by .