OpenStructure
log.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_LOG_HH
20 #define OST_LOG_HH
21 
22 #include <ostream>
23 #include <stack>
24 
25 #include <ost/log_sink.hh>
26 #include <ost/module_config.hh>
27 
28 namespace ost {
29 
30 typedef std::stack<LogSinkPtr> LogSinkStack;
31 
32  // singleton
34 public:
35  enum LogLevel {
36  QUIET =0,
37  WARNING =1,
38  SCRIPT =2,
39  INFO =3,
40  VERBOSE =4,
41  DEBUG =5,
42  TRACE =6
43  };
44 
45  void PushVerbosityLevel(int level);
47  void PushSink(LogSinkPtr& sink);
48  void PushFile(const String& filename);
50  void PopFile();
51  void PopSink();
52 
53  static Logger& Instance();
54  LogSinkPtr GetCurrentSink() { return sink_stack_.top(); }
55  int GetVerbosityLevel() const {return level_;}
56 
57  void ResetSinks() {
58  while (sink_stack_.size()>1) {
59  sink_stack_.pop();
60  }
61  }
62 protected:
63  Logger();
64  Logger(const Logger&);
66 
67 private:
68  int level_;
69  std::stack<int> level_stack_;
70  LogSinkStack sink_stack_;
71 };
72 
73 
74 #define OST_DO_LOGGING_(m, l) if (::ost::Logger::Instance().GetVerbosityLevel()>=l) {\
75  std::stringstream tmp_s__; \
76  tmp_s__ << m << std::endl; \
77  ::ost::Logger::Instance().GetCurrentSink()->LogMessage(tmp_s__.str(), l); \
78  }
79 
80 #define WARN_DEPRECATED(m) OST_DO_LOGGING_(m, ::ost::Logger::WARNING)
81 #define PUSH_VERBOSITY(n) ::ost::Logger::Instance().PushVerbosityLevel(n)
82 #define POP_VERBOSITY(n) ::ost::Logger::Instance().PopVerbosityLevel()
83 
84 #define LOG_ERROR(m) OST_DO_LOGGING_(m, ::ost::Logger::QUIET)
85 #define LOG_WARNING(m) OST_DO_LOGGING_(m, ::ost::Logger::WARNING)
86 #define LOG_SCRIPT(m) OST_DO_LOGGING_(m, ::ost::Logger::SCRIPT)
87 #define LOG_INFO(m) OST_DO_LOGGING_(m, ::ost::Logger::INFO)
88 #define LOG_VERBOSE(m) OST_DO_LOGGING_(m, ::ost::Logger::VERBOSE)
89 #ifdef NDEBUG
90 # define LOG_DEBUG(m)
91 # define LOG_TRACE(m)
92 #else
93 # define LOG_DEBUG(m) OST_DO_LOGGING_(m, ::ost::Logger::DEBUG)
94 # define LOG_TRACE(m) OST_DO_LOGGING_(m, ::ost::Logger::TRACE)
95 #endif
96 
97 }
98 
99 #endif
int GetVerbosityLevel() const
Definition: log.hh:55
void PushFile(const String &filename)
Logger(const Logger &)
void PopVerbosityLevel()
void PushSink(LogSinkPtr &sink)
Logger & operator=(const Logger &)
LogSinkPtr GetCurrentSink()
Definition: log.hh:54
void PopFile()
DEPRECATED use PopSink() instead.
static Logger & Instance()
void PushVerbosityLevel(int level)
LogLevel
Definition: log.hh:35
void PopSink()
void ResetSinks()
Definition: log.hh:57
#define DLLEXPORT_OST_BASE
std::string String
Definition: base.hh:54
Definition: base.dox:1
boost::shared_ptr< LogSink > LogSinkPtr
Definition: log_sink.hh:42
std::stack< LogSinkPtr > LogSinkStack
Definition: log.hh:30