ReFRACtor
logger.h
Go to the documentation of this file.
1 #ifndef LOGGER_H
2 #define LOGGER_H
3 #include "printable.h"
4 #include <boost/shared_ptr.hpp>
5 #include <sstream>
6 
7 namespace FullPhysics {
8 /****************************************************************/
11 class LogImp : public Printable<LogImp> {
12 public:
13  virtual ~LogImp() {}
14  enum log_level {DEBUG = 4, INFO=3, WARNING=2, ERROR=1, FATAL=0};
15  template<class T> void write(log_level l, const T& v)
16  { os << v; }
17  void write(log_level l, const std::string& v);
18  void write(log_level l, const char* v)
19  { write(l, std::string(v)); }
20 
21 //-----------------------------------------------------------------------
23 //-----------------------------------------------------------------------
24 
25  virtual std::ostream* stream() = 0;
26 
27 //-----------------------------------------------------------------------
29 //-----------------------------------------------------------------------
30 
31  virtual void flush(log_level l) = 0;
32 
33  void print(std::ostream& Os) {Os << "LogImp";}
34 protected:
35  std::ostringstream os;
36 };
37 
38 /****************************************************************/
43 class LogHelper {
44 public:
46  : l_(l), imp_(imp) {}
47  template<class T> LogHelper& operator<<(T v)
48  {
49  if(imp_)
50  imp_->write(l_, v);
51  return *this;
52  }
53 private:
56 };
57 
58 /****************************************************************/
69 class Logger : public Printable<Logger> {
70 public:
71 //-----------------------------------------------------------------------
75 //-----------------------------------------------------------------------
76 
78  { instance().imp_ = imp; }
79 
80 //-----------------------------------------------------------------------
84 //-----------------------------------------------------------------------
85 
86  static void set_implementation(LogImp* imp)
87  { instance().imp_.reset(imp); }
88 
89 //-----------------------------------------------------------------------
91 //-----------------------------------------------------------------------
92 
93  static std::ostream* stream()
94  {
95  if(instance().imp_)
96  return instance().imp_->stream();
97  else
98  return 0;
99  }
100 
101 //-----------------------------------------------------------------------
103 //-----------------------------------------------------------------------
104 
105  static Logger& instance();
107  {return LogHelper(l, instance().imp_);}
108  static LogHelper debug() {return log(LogImp::DEBUG);}
109  static LogHelper info() {return log(LogImp::INFO);}
110  static LogHelper warning() {return log(LogImp::WARNING);}
111  static LogHelper error() {return log(LogImp::ERROR);}
112  static LogHelper fatal() {return log(LogImp::FATAL);}
113  void print(std::ostream& Os) {Os << "Logger";}
114 private:
115  Logger() {}
116  Logger(const Logger&) {}
118 };
119 
120 }
121 #endif
void write(log_level l, const T &v)
Definition: logger.h:15
virtual ~LogImp()
Definition: logger.h:13
virtual std::ostream * stream()=0
Underlying stream, can be null if no underlying stream.
virtual void flush(log_level l)=0
Flush data to the log, at the given level.
static LogHelper info()
Definition: logger.h:109
static LogHelper error()
Definition: logger.h:111
void print(std::ostream &Os)
Definition: logger.h:113
static void set_implementation(LogImp *imp)
Set the implementation.
Definition: logger.h:86
std::ostringstream os
Definition: logger.h:35
static LogHelper debug()
Definition: logger.h:108
void write(log_level l, const char *v)
Definition: logger.h:18
This is a Mixin for classes that can be printed.
Definition: printable.h:24
The actual implementation of the Logger.
Definition: logger.h:11
static std::ostream * stream()
Underlying stream, can be null if no underlying stream.
Definition: logger.h:93
LogHelper(LogImp::log_level l, boost::shared_ptr< LogImp > &imp)
Definition: logger.h:45
LogHelper & operator<<(T v)
Definition: logger.h:47
void print(std::ostream &Os)
Definition: logger.h:33
static LogHelper fatal()
Definition: logger.h:112
This is a simple logger.
Definition: logger.h:69
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
static LogHelper log(LogImp::log_level l)
Definition: logger.h:106
This is a class that holds the level we are logging.
Definition: logger.h:43
static LogHelper warning()
Definition: logger.h:110
static void set_implementation(const boost::shared_ptr< LogImp > &imp)
Set the implementation.
Definition: logger.h:77

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11