AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
logger.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_LOGGER_LOGGER_H_
21 #define _ANCH_LOGGER_LOGGER_H_
22 
23 #include <iostream>
24 #include <sstream>
25 #include <vector>
26 
27 #include "logger/levels.hpp"
28 #include "logger/writer.hpp"
29 
30 namespace anch {
31  namespace logger {
32 
47  class Logger {
48  private:
50  std::string _name;
51 
53  anch::logger::Level _level;
54 
56  std::vector<anch::logger::Writer*> _writers;
57 
58  public:
66  Logger(const std::string& name,
67  const anch::logger::Level level,
68  const std::vector<anch::logger::Writer*>& writers);
69 
73  virtual ~Logger();
74 
75  public:
82  template<typename T, typename... Q>
83  void trace(const T& value, const Q&... values) const noexcept {
84  log(Level::TRACE, value, values...);
85  }
86 
93  template<typename T, typename... Q>
94  void debug(const T& value, const Q&... values) const noexcept {
95  log(Level::DEBUG, value, values...);
96  }
97 
104  template<typename T, typename... Q>
105  void info(const T& value, const Q&... values) const noexcept {
106  log(Level::INFO, value, values...);
107  }
108 
115  template<typename T, typename... Q>
116  void warn(const T& value, const Q&... values) const noexcept {
117  log(Level::WARN, value, values...);
118  }
119 
126  template<typename T, typename... Q>
127  void error(const T& value, const Q&... values) const noexcept {
128  log(Level::ERROR, value, values...);
129  }
130 
137  template<typename T, typename... Q>
138  void fatal(const T& value, const Q&... values) const noexcept {
139  log(Level::FATAL, value, values...);
140  }
141 
142  private:
151  template<typename T>
152  void log(const Level& level,
153  std::ostringstream& out,
154  const T& value) const noexcept {
155  out << value;
156  const std::string& message = out.str();
157  for(size_t i = 0 ; i < _writers.size() ; i++) {
158  _writers[i]->write(_name, level, message);
159  }
160  }
161 
169  template<typename T, typename... Q>
170  void log(const Level& level,
171  const T& value,
172  const Q&... values) const noexcept {
173  if(level >= _level && !_writers.empty()) {
174  std::ostringstream out;
175  log(level, out, value, values...);
176  }
177  }
178 
187  template<typename T, typename... Q>
188  void log(const Level& level,
189  std::ostringstream& out,
190  const T& value,
191  const Q&... values) const noexcept {
192  out << value;
193  log(level, out, values...);
194  }
195 
196  };
197 
198  }
199 }
200 
201 #endif // _ANCH_LOGGER_LOGGER_H_
Logger(const std::string &name, const anch::logger::Level level, const std::vector< anch::logger::Writer * > &writers)
Definition: logger.cpp:37
void trace(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:83
void info(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:105
Definition: logger.hpp:47
virtual ~Logger()
Definition: logger.cpp:51
void warn(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:116
AnCH framework base namespace.
Definition: base64.hpp:28
void fatal(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:138
void error(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:127
Level
Definition: levels.hpp:34
void debug(const T &value, const Q &...values) const noexcept
Definition: logger.hpp:94