Libosmium: include/osmium/util/verbose_output.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
verbose_output.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_UTIL_VERBOSE_OUTPUT_HPP
2#define OSMIUM_UTIL_VERBOSE_OUTPUT_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <ctime>
37#include <iomanip>
38#include <iostream>
39#include <sstream>
40#include <string>
41
42namespace osmium {
43
47 inline namespace util {
48
61 class VerboseOutput {
62
64 std::time_t m_start;
65
67 bool m_verbose;
68
70 bool m_newline = true;
71
76 void start_line() {
77 if (m_newline) {
78 const std::time_t elapsed = runtime();
79
80 const char old_fill = std::cerr.fill();
81 std::cerr << '[' << std::setw(2) << (elapsed / 60) << ':' << std::setw(2) << std::setfill('0') << (elapsed % 60) << "] ";
82 std::cerr.fill(old_fill);
83
84 m_newline = false;
85 }
86 }
87
88 public:
89
90 explicit VerboseOutput(bool verbose = false) noexcept :
91 m_start(std::time(nullptr)),
92 m_verbose(verbose) {
93 }
94
95 std::time_t runtime() const noexcept {
96 return std::time(nullptr) - m_start;
97 }
98
100 bool verbose() const noexcept {
101 return m_verbose;
102 }
103
105 void verbose(bool verbose) noexcept {
106 m_verbose = verbose;
107 }
108
109 template <typename T>
110 void print(const T& value) {
111 if (m_verbose) {
112 start_line();
113 std::cerr << value;
114
115 // check if there was a newline a the end and remember that
116 std::ostringstream output_buffer;
117 output_buffer << value;
118 if (!output_buffer.str().empty() && output_buffer.str().back() == '\n') {
119 m_newline = true;
120 }
121 }
122 }
123
124 }; // class VerboseOutput
125
126 template <typename T>
127 inline VerboseOutput& operator<<(VerboseOutput& verbose_output, const T& value) {
128 verbose_output.print(value);
129 return verbose_output;
130 }
131
132 } // namespace util
133
134} // namespace osmium
135
136#endif // OSMIUM_UTIL_VERBOSE_OUTPUT_HPP
osmium::VerboseOutput
Definition verbose_output.hpp:61
osmium::util::VerboseOutput::start_line
void start_line()
Definition verbose_output.hpp:76
osmium::util::VerboseOutput::m_start
std::time_t m_start
all time output will be relative to this start time
Definition verbose_output.hpp:64
osmium::util::VerboseOutput::runtime
std::time_t runtime() const noexcept
Definition verbose_output.hpp:95
osmium::util::VerboseOutput::print
void print(const T &value)
Definition verbose_output.hpp:110
osmium::util::VerboseOutput::m_newline
bool m_newline
a newline was written, start next output with runtime
Definition verbose_output.hpp:70
osmium::util::VerboseOutput::verbose
void verbose(bool verbose) noexcept
Set "verbose" setting.
Definition verbose_output.hpp:105
osmium::util::VerboseOutput::verbose
bool verbose() const noexcept
Get "verbose" setting.
Definition verbose_output.hpp:100
osmium::util::VerboseOutput::m_verbose
bool m_verbose
is verbose mode enabled?
Definition verbose_output.hpp:67
osmium::util::VerboseOutput::VerboseOutput
VerboseOutput(bool verbose=false) noexcept
Definition verbose_output.hpp:90
osmium::util
Helpful utility classes and functions not strictly OSM related.
Definition crc.hpp:55
osmium::util::operator<<
VerboseOutput & operator<<(VerboseOutput &verbose_output, const T &value)
Definition verbose_output.hpp:127
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654