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>
27#include <string>
28
29namespace Opm {
30
31 class LogBackend;
32
33class Logger {
34
35public:
37 void addMessage(int64_t messageType , const std::string& message) const;
38 void addTaggedMessage(int64_t messageType, const std::string& tag, const std::string& message) const;
39
40 static bool enabledDefaultMessageType( int64_t messageType);
41 bool enabledMessageType( int64_t messageType) const;
42 void addMessageType( int64_t messageType , const std::string& prefix);
43 int64_t enabledMessageTypes() const;
44
45 void addBackend(const std::string& name , std::shared_ptr<LogBackend> backend);
49
50 template <class BackendType>
51 std::shared_ptr<BackendType> getBackend(const std::string& name) const {
52 auto pair = m_backends.find( name );
53 if (pair == m_backends.end())
54 throw std::invalid_argument("Invalid backend name: " + name);
55 else
56 return std::static_pointer_cast<BackendType>(m_backends.find(name)->second);
57 }
58
59 template <class BackendType>
60 std::shared_ptr<BackendType> popBackend(const std::string& name) {
61 auto pair = m_backends.find( name );
62 if (pair == m_backends.end())
63 throw std::invalid_argument("Invalid backend name: " + name);
64 else {
65 std::shared_ptr<LogBackend> backend = (*pair).second;
67 return std::static_pointer_cast<BackendType>(backend);
68 }
69 }
70
71
72private:
73 void updateGlobalMask( int64_t mask );
74 static bool enabledMessageType( int64_t enabledTypes , int64_t messageType);
75
76 int64_t m_globalMask;
77 int64_t m_enabledTypes;
78 std::map<std::string , std::shared_ptr<LogBackend> > m_backends;
79};
80
81}
82#endif
const char *const name
Definition: cJSON.h:258
const char *const string
Definition: cJSON.h:170
Definition: Logger.hpp:33
std::shared_ptr< BackendType > getBackend(const std::string &name) const
Definition: Logger.hpp:51
void addMessageType(int64_t messageType, const std::string &prefix)
std::shared_ptr< BackendType > popBackend(const std::string &name)
Definition: Logger.hpp:60
bool removeBackend(const std::string &name)
void addBackend(const std::string &name, std::shared_ptr< LogBackend > backend)
void addTaggedMessage(int64_t messageType, const std::string &tag, const std::string &message) const
bool enabledMessageType(int64_t messageType) const
int64_t enabledMessageTypes() const
void removeAllBackends()
bool hasBackend(const std::string &name)
static bool enabledDefaultMessageType(int64_t messageType)
void addMessage(int64_t messageType, const std::string &message) const
Definition: A.hpp:4