vega
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logger.h
Go to the documentation of this file.
1 // Credit to: https://stackoverflow.com/a/25217417/2435405
2 
3 #pragma once
4 
5 namespace vega {
6  // TODO: inherit from ostream? accept std::endl in <<?
7  class Logger {
8  public:
9 
10  explicit Logger(std::ostream& os, bool active = true) : m_os(os), m_indent(0), m_active(active) {}
11  void increase_indent() { ++m_indent; }
12  void decrease_indent() { --m_indent; }
13 
15  if (m_active) {
16  for (size_t i = 0; i < m_indent; ++i) {
17  m_os << " ";
18  }
19  }
20  return *this;
21  }
22 
24  if (m_active) m_os << std::endl;
25  return *this;
26  }
27 
28  template<typename T> friend Logger& operator<<(Logger&, const T&);
29 
30  private:
31  std::ostream& m_os;
32  size_t m_indent;
33  bool m_active;
34  };
35 };
36 
37 template<typename T>
38 vega::Logger& vega::operator<<(vega::Logger& log, const T& op) {
39  if (log.m_active) log.m_os << op;
40  return log;
41 }
Definition: logger.h:7
void increase_indent()
Definition: logger.h:11
friend Logger & operator<<(Logger &, const T &)
Logger(std::ostream &os, bool active=true)
Definition: logger.h:10
Logger & newline()
Definition: logger.h:23
void decrease_indent()
Definition: logger.h:12
Definition: age.h:6
Logger & indent()
Definition: logger.h:14