Logger.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2015 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_LOGGER_HPP
21 #define OPM_LOGGER_HPP
22 
23 #include <stdexcept>
24 #include <cstdint>
25 #include <map>
26 #include <memory>
29 
30 namespace Opm {
31 
32 class Logger {
33 public:
34  Logger();
35  void addMessage(int64_t messageType , const std::string& message) const;
36 
37  static bool enabledDefaultMessageType( int64_t messageType);
38  bool enabledMessageType( int64_t messageType) const;
39  void addMessageType( int64_t messageType , const std::string& prefix);
40  int64_t enabledMessageTypes() const;
41 
42  void addBackend(const std::string& name , std::shared_ptr<LogBackend> backend);
43  bool hasBackend(const std::string& name);
44  bool removeBackend(const std::string& name);
45 
46  template <class BackendType>
47  std::shared_ptr<BackendType> getBackend(const std::string& name) const {
48  auto pair = m_backends.find( name );
49  if (pair == m_backends.end())
50  throw std::invalid_argument("Invalid backend name: " + name);
51  else
52  return std::static_pointer_cast<BackendType>(m_backends.find(name)->second);
53  }
54 
55  template <class BackendType>
56  std::shared_ptr<BackendType> popBackend(const std::string& name) {
57  auto pair = m_backends.find( name );
58  if (pair == m_backends.end())
59  throw std::invalid_argument("Invalid backend name: " + name);
60  else {
61  std::shared_ptr<LogBackend> backend = (*pair).second;
62  removeBackend( name );
63  return std::static_pointer_cast<BackendType>(backend);
64  }
65  }
66 
67 
68 private:
69  void updateGlobalMask( int64_t mask );
70  static bool enabledMessageType( int64_t enabledTypes , int64_t messageType);
71 
72  int64_t m_globalMask;
73  int64_t m_enabledTypes;
74  std::map<std::string , std::shared_ptr<LogBackend> > m_backends;
75 };
76 
77 }
78 #endif
bool removeBackend(const std::string &name)
Definition: Deck.hpp:29
std::shared_ptr< BackendType > getBackend(const std::string &name) const
Definition: Logger.hpp:47
std::shared_ptr< BackendType > popBackend(const std::string &name)
Definition: Logger.hpp:56
Definition: Logger.hpp:32
bool hasBackend(const std::string &name)
static bool enabledDefaultMessageType(int64_t messageType)
void addMessageType(int64_t messageType, const std::string &prefix)
int64_t enabledMessageTypes() const
void addMessage(int64_t messageType, const std::string &message) const
void addBackend(const std::string &name, std::shared_ptr< LogBackend > backend)
bool enabledMessageType(int64_t messageType) const