Libosmium  2.3.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-2015 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 <time.h>
37 
38 #include <iomanip>
39 #include <iostream>
40 #include <sstream>
41 
42 namespace osmium {
43 
47  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  time_t elapsed = runtime();
79 
80  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(NULL)),
92  m_verbose(verbose),
93  m_newline(true) {
94  }
95 
96  ~VerboseOutput() = default;
97 
98  VerboseOutput(const VerboseOutput&) = default;
99  VerboseOutput& operator=(const VerboseOutput&) = default;
100  VerboseOutput(VerboseOutput&&) = default;
101  VerboseOutput& operator=(VerboseOutput&&) = default;
102 
103  time_t runtime() const noexcept {
104  return time(NULL) - m_start;
105  }
106 
108  bool verbose() const noexcept {
109  return m_verbose;
110  }
111 
113  void verbose(bool verbose) noexcept {
114  m_verbose = verbose;
115  }
116 
117  template<typename T>
118  friend VerboseOutput& operator<<(VerboseOutput& verbose_output, const T& value) {
119  if (verbose_output.m_verbose) {
120  verbose_output.start_line();
121  std::cerr << value;
122 
123  // check if there was a newline a the end and remember that
124  std::ostringstream output_buffer;
125  output_buffer << value;
126  if (!output_buffer.str().empty() && output_buffer.str().back() == '\n') {
127  verbose_output.m_newline = true;
128  }
129  }
130  return verbose_output;
131  }
132 
133  }; // class VerboseOutput
134 
135  } // namespace util
136 
137 } // namespace osmium
138 
139 #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:108
VerboseOutput(bool verbose=false) noexcept
Definition: verbose_output.hpp:90
VerboseOutput & operator=(const VerboseOutput &)=default
void start_line()
Definition: verbose_output.hpp:76
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
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:103
void verbose(bool verbose) noexcept
Set "verbose" setting.
Definition: verbose_output.hpp:113
Definition: verbose_output.hpp:61
bool m_verbose
is verbose mode enabled?
Definition: verbose_output.hpp:67
friend VerboseOutput & operator<<(VerboseOutput &verbose_output, const T &value)
Definition: verbose_output.hpp:118