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-2011 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);
46  void PopVerbosityLevel();
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&);
65  Logger& operator=(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