scclib
Stable Cloud Computing C++ Library
logger.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SYS_UTIL_LOGGER_H
32 #define _SYS_UTIL_LOGGER_H
33 
34 #include <string>
35 #include <ostream>
36 #include <memory>
37 
38 namespace scc::util
39 {
40 
85 class Logger : public std::ostream
86 {
87 protected:
88 
89  void dup(const Logger& b);
90 
91 public:
96  Logger(unsigned int = 256);
97  virtual ~Logger();
100  Logger(const std::shared_ptr<std::ostream>& os, unsigned int ll = 256) : Logger(ll)
101  {
102  add(os);
103  }
105  Logger(const Logger&);
107  Logger& operator=(const Logger&);
109  Logger(Logger&&);
111  Logger& operator=(Logger&&);
112 
121  void clear();
122 
127  void add(const std::shared_ptr<std::ostream>&);
129  void remove(const std::shared_ptr<std::ostream>&);
130 
132  void add_cout();
134  void remove_cout();
136  void add_clog();
138  void remove_clog();
140  void add_cerr();
142  void remove_cerr();
143 
148  void id(const std::string& id="");
150  std::string id() const;
151 
153  void id(unsigned int i)
154  {
155  id(std::to_string(i));
156  }
157 
162  void timestamp(const std::string& ts="");
164  std::string timestamp() const;
165 
170  void timestamp_std(bool utc_on=false)
171  {
172  utc(utc_on);
173 
174  if (utc_on)
175  {
176  timestamp("%b %d %Y %T UTC");
177  }
178  else
179  {
180  timestamp("%b %d %Y %T");
181  }
182  }
183 
188  void timestamp_iso(bool utc_on=true)
189  {
190  utc(utc_on);
191 
192  if (utc_on)
193  {
194  timestamp("%FT%TZ");
195  }
196  else
197  {
198  timestamp("%FT%T%z");
199  }
200  }
201 
203  void utc(bool on);
205  bool utc() const;
206 
208  void enable(bool on);
210  bool enable() const;
211 
216  void max_line(unsigned int);
218  unsigned int max_line() const;
219 
224  void multiline(unsigned int max=0);
229  unsigned int multiline() const;
230 };
231 
234 }
235 
236 #endif
Thread-safe stream logger.
Definition: logger.h:86
void multiline(unsigned int max=0)
Set multiline mode.
Logger(unsigned int=256)
Create a logger with maximum line length (default 256).
Definition: logger.cc:338
void id(unsigned int i)
Set a numeric id.
Definition: logger.h:153
void remove_clog()
Remove std::clog (buffered error) console stream.
void remove(const std::shared_ptr< std::ostream > &)
Remove shared stream.
Definition: logger.cc:411
void remove_cerr()
Remove std::cerr (unbuffered error) console stream.
Logger & operator=(const Logger &)
Copy assign logger, using original streams and settings.
Definition: logger.cc:360
void add(const std::shared_ptr< std::ostream > &)
Add a shared stream.
Definition: logger.cc:404
void timestamp_iso(bool utc_on=true)
Set iso 8601 timestamp.
Definition: logger.h:188
std::string id() const
Get the current id string.
Definition: logger.cc:432
void add_cerr()
Add std::cerr (unbuffered error) console stream.
void timestamp_std(bool utc_on=false)
Set to a standard timestamp.
Definition: logger.h:170
void add_clog()
Add std::clog (buffered error) console stream.
unsigned int max_line() const
Get the maximum line length.
Definition: logger.cc:502
void max_line(unsigned int)
Set the maximum line length (default is 256).
bool utc() const
Get utc mode.
Definition: logger.cc:460
unsigned int multiline() const
Get maximum number of multiline lines.
Definition: logger.cc:488
Logger(const std::shared_ptr< std::ostream > &os, unsigned int ll=256)
Create a logger with an attached stream.
Definition: logger.h:100
void remove_cout()
Remove std::cout console stream.
bool enable() const
Is the logger enabled?
Definition: logger.cc:474
void clear()
Clear the logger and reset to defaults.
Definition: logger.cc:397
std::string timestamp() const
Get the current timestamp format.
Definition: logger.cc:446
void add_cout()
Add std::cout console stream.
Definition: logger.cc:418