Libosmium  2.17.0
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-2021 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
62
64 time_t m_start;
65
68
71
76 void start_line() {
77 if (m_newline) {
78 const 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(time(nullptr)),
93 m_newline(true) {
94 }
95
96 time_t runtime() const noexcept {
97 return time(nullptr) - m_start;
98 }
99
101 bool verbose() const noexcept {
102 return m_verbose;
103 }
104
106 void verbose(bool verbose) noexcept {
108 }
109
110 template <typename T>
111 void print(const T& value) {
112 if (m_verbose) {
113 start_line();
114 std::cerr << value;
115
116 // check if there was a newline a the end and remember that
117 std::ostringstream output_buffer;
118 output_buffer << value;
119 if (!output_buffer.str().empty() && output_buffer.str().back() == '\n') {
120 m_newline = true;
121 }
122 }
123 }
124
125 }; // class VerboseOutput
126
127 template <typename T>
128 inline VerboseOutput& operator<<(VerboseOutput& verbose_output, const T& value) {
129 verbose_output.print(value);
130 return verbose_output;
131 }
132
133 } // namespace util
134
135} // namespace osmium
136
137#endif // OSMIUM_UTIL_VERBOSE_OUTPUT_HPP
Definition: verbose_output.hpp:61
void start_line()
Definition: verbose_output.hpp:76
void print(const T &value)
Definition: verbose_output.hpp:111
bool m_newline
a newline was written, start next output with runtime
Definition: verbose_output.hpp:70
void verbose(bool verbose) noexcept
Set "verbose" setting.
Definition: verbose_output.hpp:106
bool verbose() const noexcept
Get "verbose" setting.
Definition: verbose_output.hpp:101
time_t m_start
all time output will be relative to this start time
Definition: verbose_output.hpp:64
bool m_verbose
is verbose mode enabled?
Definition: verbose_output.hpp:67
VerboseOutput(bool verbose=false) noexcept
Definition: verbose_output.hpp:90
time_t runtime() const noexcept
Definition: verbose_output.hpp:96
VerboseOutput & operator<<(VerboseOutput &verbose_output, const T &value)
Definition: verbose_output.hpp:128
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53