Cerata
A library to generate structural hardware designs
logging.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <iostream>
18 #include <functional>
19 #include <string>
20 #include <utility>
21 
22 namespace cerata {
23 
24 // Log levels.
25 
27 using LogLevel = int;
28 
29 constexpr LogLevel CERATA_LOG_DEBUG = -1;
30 constexpr LogLevel CERATA_LOG_INFO = 0;
32 constexpr LogLevel CERATA_LOG_ERROR = 2;
33 constexpr LogLevel CERATA_LOG_FATAL = 3;
34 
36 class Logger {
37  public:
39  using CallbackSignature = void(LogLevel, const std::string &, char const *, char const *, int);
40 
42  inline void enable(std::function<CallbackSignature> callback) {
43  callback_ = std::move(callback);
44  }
45 
47  inline bool IsEnabled() { return callback_ ? true : false; }
48 
50  inline void write(LogLevel level,
51  std::string const &message,
52  char const *source_function,
53  char const *source_file,
54  int line_number) {
55  if (callback_) {
56  callback_(level, message, source_function, source_file, line_number);
57  }
58  }
59 
60  private:
62  std::function<void(LogLevel, const std::string &, char const *, char const *, int)> callback_;
63 };
64 
66 inline Logger &logger() {
67  static Logger l;
68  return l;
69 }
70 
71 // TODO(johanpel): do we want to throw or exit some other way? For debugging, this is quite handy.
72 #define CERATA_LOG(level, msg) \
73 if (CERATA_LOG_##level > CERATA_LOG_WARNING) { \
74  throw std::runtime_error(std::string(__FILE__) + ":" \
75  + std::string(__FUNCTION__) + ":" \
76  + std::to_string(__LINE__) + ":\n" + (msg)); \
77 } else { \
78  logger().write(CERATA_LOG_##level, msg, __FUNCTION__, __FILE__, __LINE__); \
79 } \
80 (void)0
81 
82 } // namespace cerata
cerata::Logger::CallbackSignature
void(LogLevel, const std::string &, char const *, char const *, int) CallbackSignature
Signature of the callback function.
Definition: logging.h:39
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::CERATA_LOG_DEBUG
constexpr LogLevel CERATA_LOG_DEBUG
Debug level.
Definition: logging.h:29
cerata::CERATA_LOG_WARNING
constexpr LogLevel CERATA_LOG_WARNING
Warning level.
Definition: logging.h:31
cerata::Logger::write
void write(LogLevel level, std::string const &message, char const *source_function, char const *source_file, int line_number)
Write something using the logging callback function.
Definition: logging.h:50
cerata::Logger::enable
void enable(std::function< CallbackSignature > callback)
Enable the logger. Can only be done after the callback function was set.
Definition: logging.h:42
cerata::CERATA_LOG_FATAL
constexpr LogLevel CERATA_LOG_FATAL
Fatal level; tool should exit.
Definition: logging.h:33
cerata::CERATA_LOG_ERROR
constexpr LogLevel CERATA_LOG_ERROR
Error level.
Definition: logging.h:32
cerata::Logger
Logger class.
Definition: logging.h:36
cerata::logger
Logger & logger()
Return the global Cerata logger.
Definition: logging.h:66
cerata::LogLevel
int LogLevel
Type used for the logging level.
Definition: logging.h:27
cerata::CERATA_LOG_INFO
constexpr LogLevel CERATA_LOG_INFO
Information level.
Definition: logging.h:30
cerata::Logger::IsEnabled
bool IsEnabled()
Return true if callback was set, false otherwise.
Definition: logging.h:47