Metall  v0.29
A persistent memory allocator for data-centric analytics
logger.hpp
Go to the documentation of this file.
1 // Copyright 2019 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_LOGGER_HPP
7 #define METALL_LOGGER_HPP
8 
9 #include <cstring>
10 #include <optional>
11 #include <sstream>
13 
14 namespace metall {
15 
16 class logger {
17  public:
19  enum struct level {
27  info = metall_info,
32  };
33 
35  static void out(const level lvl, const char* const file_name,
36  const int line_no, const char* const message) noexcept {
37  metall_log(static_cast<metall_log_level>(lvl), file_name, line_no, message);
38  }
39 
41  static void perror(const level lvl, const char* const file_name,
42  const int line_no, const char* const message) noexcept {
43  std::stringstream ss;
44  ss << message << ": " << strerror(errno);
45 
46  auto const m = ss.str();
47  metall_log(static_cast<metall_log_level>(lvl), file_name, line_no, m.c_str());
48  }
49 
50  logger() = delete;
51  ~logger() = delete;
52  logger(const logger&) = delete;
53  logger(logger&&) = delete;
54  logger& operator=(const logger&) = delete;
55  logger& operator=(logger&&) = delete;
56 
57 #ifndef METALL_LOGGER_EXTERN_C
59  struct level_filter {
60  private:
61  std::optional<level> inner;
62 
63  explicit level_filter(std::optional<level> inner) noexcept : inner{inner} {
64  }
65 
66  public:
68  static const level_filter critical;
70  static const level_filter error;
72  static const level_filter warning;
74  static const level_filter info;
76  static const level_filter debug;
78  static const level_filter verbose;
80  static const level_filter silent;
81 
83  bool should_log(level lvl) const noexcept {
84  return inner.has_value() && lvl >= *inner;
85  }
86  };
87 
89  static level_filter log_level() noexcept {
90  return log_message_out_level;
91  }
92 
94  static void set_log_level(const level_filter lvl) noexcept {
95  log_message_out_level = lvl;
96  }
97 
99  static bool abort_on_critical_error() noexcept {
100  return abort_on_critical;
101  }
102 
104  static void abort_on_critical_error(const bool enable) noexcept {
105  abort_on_critical = enable;
106  }
107 
108 private:
109  static level_filter log_message_out_level;
110  static bool abort_on_critical;
111 #endif // METALL_LOGGER_EXTERN_C
112 };
113 
114 #ifndef METALL_LOGGER_EXTERN_C
115 inline const logger::level_filter logger::level_filter::critical{logger::level::critical};
116 inline const logger::level_filter logger::level_filter::error{logger::level::error};
117 inline const logger::level_filter logger::level_filter::warning{logger::level::warning};
118 inline const logger::level_filter logger::level_filter::info{logger::level::info};
119 inline const logger::level_filter logger::level_filter::debug{logger::level::debug};
120 inline const logger::level_filter logger::level_filter::verbose{logger::level::verbose};
121 inline const logger::level_filter logger::level_filter::silent{std::nullopt};
122 
123 inline logger::level_filter logger::log_message_out_level = logger::level_filter::error;
124 inline bool logger::abort_on_critical = true;
125 #endif // METALL_LOGGER_EXTERN_C
126 
127 } // namespace metall
128 
129 
130 #ifndef METALL_LOGGER_EXTERN_C
131 #include <iostream>
132 
133 extern "C" inline void metall_log(metall_log_level lvl_, const char *file_name, size_t line_no, const char *message) {
134  auto const lvl = static_cast<metall::logger::level>(lvl_);
135 
137  return;
138  }
139 
140  try {
141  std::cerr << file_name << " at line " << line_no << " --- " << message
142  << std::endl;
143  } catch (...) {
144  }
145 
147  std::abort();
148  }
149 }
150 #endif // METALL_LOGGER_EXTERN_C
151 
152 #endif // METALL_LOGGER_HPP
Definition: logger.hpp:16
logger(const logger &)=delete
logger & operator=(logger &&)=delete
static void perror(const level lvl, const char *const file_name, const int line_no, const char *const message) noexcept
Log a message about errno.
Definition: logger.hpp:41
static void abort_on_critical_error(const bool enable) noexcept
If true is specified, enable an abort at a critical logger message.
Definition: logger.hpp:104
static void set_log_level(const level_filter lvl) noexcept
Set the minimum logger level to show message.
Definition: logger.hpp:94
~logger()=delete
logger & operator=(const logger &)=delete
static bool abort_on_critical_error() noexcept
Definition: logger.hpp:99
static void out(const level lvl, const char *const file_name, const int line_no, const char *const message) noexcept
Log a message.
Definition: logger.hpp:35
logger()=delete
static level_filter log_level() noexcept
Definition: logger.hpp:89
logger(logger &&)=delete
level
Log message level.
Definition: logger.hpp:19
@ verbose
Verbose (lowest priority) logger message.
@ warning
Warning logger message.
@ critical
Critical logger message — with default logger implementation abort the execution unless disabled.
@ debug
Debug logger message.
@ info
Info logger message.
@ error
Error logger message.
void metall_log(metall_log_level lvl_, const char *file_name, size_t line_no, const char *message)
Implementation of logging behaviour.
Definition: logger.hpp:133
metall_log_level
Log message level.
Definition: logger_interface.h:9
@ metall_debug
Debug logger message.
Definition: logger_interface.h:19
@ metall_critical
Critical logger message.
Definition: logger_interface.h:11
@ metall_error
Error logger message.
Definition: logger_interface.h:13
@ metall_warning
Warning logger message.
Definition: logger_interface.h:15
@ metall_verbose
Verbose (lowest priority) logger message.
Definition: logger_interface.h:21
@ metall_info
Info logger message.
Definition: logger_interface.h:17
The top level of namespace of Metall.
Definition: basic_manager.hpp:22
determines the minimum level of messages that should be loggged
Definition: logger.hpp:59
static const level_filter silent
Don't log any messages.
Definition: logger.hpp:80
static const level_filter info
Only log info, warning, error and critical messages.
Definition: logger.hpp:74
static const level_filter warning
Only log warning, error and critical messages.
Definition: logger.hpp:72
bool should_log(level lvl) const noexcept
returns true if the logger should log a message of level lvl with this level_filter
Definition: logger.hpp:83
static const level_filter debug
Only log debug, info, warning, error and critical messages.
Definition: logger.hpp:76
static const level_filter error
Only log error and critical messages.
Definition: logger.hpp:70
static const level_filter critical
Only log critical messages.
Definition: logger.hpp:68
static const level_filter verbose
Log all messages.
Definition: logger.hpp:78