Libosmium  2.14.0
Fast and flexible C++ library for working with OpenStreetMap data
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 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <ctime>
37 #include <iomanip>
38 #include <iostream>
39 #include <sstream>
40 #include <string>
41 
42 namespace osmium {
43 
47  inline namespace util {
48 
61  class VerboseOutput {
62 
64  time_t m_start;
65 
67  bool m_verbose;
68 
70  bool m_newline;
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)),
92  m_verbose(verbose),
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 {
107  m_verbose = verbose;
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
time_t m_start
all time output will be relative to this start time
Definition: verbose_output.hpp:64
bool verbose() const noexcept
Get "verbose" setting.
Definition: verbose_output.hpp:101
VerboseOutput(bool verbose=false) noexcept
Definition: verbose_output.hpp:90
void start_line()
Definition: verbose_output.hpp:76
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
VerboseOutput & operator<<(VerboseOutput &verbose_output, const T &value)
Definition: verbose_output.hpp:128
bool m_newline
a newline was written, start next output with runtime
Definition: verbose_output.hpp:70
time_t runtime() const noexcept
Definition: verbose_output.hpp:96
void verbose(bool verbose) noexcept
Set "verbose" setting.
Definition: verbose_output.hpp:106
Definition: verbose_output.hpp:61
bool m_verbose
is verbose mode enabled?
Definition: verbose_output.hpp:67
void print(const T &value)
Definition: verbose_output.hpp:111