Cypress  1.0
C++ Spiking Neural Network Simulation Framework
logger.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2016 Andreas Stöckel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
28 #ifndef CYPRESS_UTIL_LOGGER_HPP
29 #define CYPRESS_UTIL_LOGGER_HPP
30 
31 #include <ctime>
32 #include <cstdint>
33 #include <iostream>
34 #include <fstream>
35 #include <memory>
36 #include <string>
37 
38 namespace cypress {
39 /*
40  * Forward declarations.
41  */
42 class Logger;
43 class LoggerImpl;
44 class LogStreamBackendImpl;
45 
50 enum LogSeverity : int32_t {
51  DEBUG = 10,
52  INFO = 20,
53  WARNING = 30,
54  ERROR = 40,
56 };
57 
62 class LogBackend {
63 public:
68  virtual void log(LogSeverity lvl, std::time_t time,
69  const std::string &module, const std::string &message) = 0;
70  virtual ~LogBackend() {}
71 };
72 
77 class LogStreamBackend : public LogBackend {
78 private:
79  std::unique_ptr<LogStreamBackendImpl> m_impl;
80 
81 public:
82  LogStreamBackend(std::ostream &os, bool use_color = false);
83 
84  void log(LogSeverity lvl, std::time_t time, const std::string &module,
85  const std::string &message) override;
86  ~LogStreamBackend() override;
87 };
88 
94 private:
95  std::ofstream m_stream;
96 
97 public:
98  LogFileBackend(const std::string &prefix = "cypress");
99  ~LogFileBackend() override;
100 };
101 
107 class Logger {
108 private:
109  std::unique_ptr<LoggerImpl> m_impl;
110 
111 public:
115  Logger();
116 
120  Logger(std::shared_ptr<LogBackend> backend,
122 
126  size_t backend_count() const;
127 
132  int add_backend(std::shared_ptr<LogBackend> backend,
134 
140  void min_level(LogSeverity lvl, int idx = -1);
141 
146  LogSeverity min_level(int idx = -1);
147 
152  size_t count(LogSeverity lvl = LogSeverity::DEBUG) const;
153 
154  void log(LogSeverity lvl, std::time_t time, const std::string &module,
155  const std::string &message);
156  void debug(const std::string &module, const std::string &message);
157  void info(const std::string &module, const std::string &message);
158  void warn(const std::string &module, const std::string &message);
159  void error(const std::string &module, const std::string &message);
160  void fatal_error(const std::string &module, const std::string &message);
161 };
162 
164 }
165 
166 #endif /* CYPRESS_UTIL_LOGGER_HPP */
Logger & global_logger()
virtual ~LogBackend()
Definition: logger.hpp:70
Definition: logger.hpp:62
Definition: logger.hpp:54
Definition: logger.hpp:55
LogSeverity
Definition: logger.hpp:50
Definition: logger.hpp:93
virtual void log(LogSeverity lvl, std::time_t time, const std::string &module, const std::string &message)=0
Definition: brainscales_lib.hpp:39
Definition: logger.hpp:52
Definition: logger.hpp:51
Definition: logger.hpp:77
Definition: logger.hpp:53
Definition: logger.hpp:107