HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
log.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
33 
34 #include <functional>
35 #include <map>
36 #include <memory>
37 #include <mutex>
38 #include <set>
39 #include <string>
40 #include <tuple>
41 
42 #define FMT_HEADER_ONLY
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wshadow"
45 #include <spdlog/sinks/base_sink.h>
46 #include <spdlog/spdlog.h>
47 #pragma GCC diagnostic pop
48 
49 namespace hal
50 {
51 // macros to stringify to transform int to const char*
52 #define STRINGISTRINGIFY(x) #x
53 #define STRINGIFY(x) STRINGISTRINGIFY(x)
54 
55 // macro to get the log channel
56 #define LOG_CHANNEL(channel) LogManager::get_instance()->get_channel(channel)
57 
70 #define log_info(channel, ...) LOG_CHANNEL(channel)->info(__VA_ARGS__)
71 
72 #define log_trace(channel, ...) LOG_CHANNEL(channel)->trace("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__)
73 
74 #define log_debug(channel, ...) LOG_CHANNEL(channel)->debug("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__)
75 
76 #define log_warning(channel, ...) LOG_CHANNEL(channel)->warn("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__)
77 
78 #define log_error(channel, ...) LOG_CHANNEL(channel)->error("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__)
79 
80 #define log_critical(channel, ...) LOG_CHANNEL(channel)->critical("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__)
82 
94 #define die(channel, ...) \
95  do \
96  { \
97  LOG_CHANNEL(channel)->critical("[" __FILE__ ":" STRINGIFY(__LINE__) "] " __VA_ARGS__); \
98  exit(1); \
99  } while (0);
100 
107  {
108  public:
112  struct log_sink
113  {
114  std::shared_ptr<spdlog::sinks::sink> spdlog_sink;
115  std::string sink_type;
117  bool truncate;
118  std::filesystem::path path;
119  };
120 
129  static LogManager* get_instance(const std::filesystem::path& file_name = "");
130 
137  void set_file_name(const std::filesystem::path& file_name);
138 
145  void set_format_pattern(const std::string& format);
146 
154  std::shared_ptr<spdlog::logger> get_channel(const std::string& channel_name = "stdout");
155 
161  std::set<std::string> get_channels() const;
162 
171  std::shared_ptr<spdlog::logger> add_channel(const std::string& channel_name, const std::vector<std::shared_ptr<log_sink>>& sinks, const std::string& level = "info");
172 
178  void remove_channel(const std::string& channel_name);
179 
186  std::string get_level_of_channel(const std::string& channel_name) const;
187 
194  void set_level_of_channel(const std::string& channel_name, const std::string& level);
195 
201  std::set<std::string> get_available_log_levels() const;
202 
209  void activate_channel(const std::string& channel_name);
210 
214  void activate_all_channels();
215 
221  void deactivate_channel(const std::string& channel_name);
222 
226  void deactivate_all_channels();
227 
233  CallbackHook<void(const spdlog::level::level_enum&, const std::string&, const std::string&)>& get_gui_callback();
234 
240  ProgramOptions& get_option_descriptions();
241 
247  void handle_options(ProgramArguments& args);
248 
254  std::vector<std::shared_ptr<hal::LogManager::log_sink>> get_default_sinks();
255 
261  void remove_sink_from_default(const std::string& sink_type);
262 
269  static std::shared_ptr<log_sink> create_stdout_sink(const bool colored = true);
270 
280  static std::shared_ptr<log_sink> create_file_sink(const std::filesystem::path& file_name = "", const bool truncate = false);
281 
287  static std::shared_ptr<log_sink> create_gui_sink();
288 
289  private:
290  static std::map<std::string, std::shared_ptr<log_sink>> m_file_sinks;
291 
292  // LogManager class constructor (private due to singleton)
293  LogManager(const std::filesystem::path& file_name);
294 
295  // LogManager class destructor (private due to singleton)
296  ~LogManager();
297 
298  // LogManager class object non-copyable
299  LogManager(const LogManager&) = delete;
300 
301  // LogManager class object non-copyable
302  LogManager& operator=(const LogManager&) = delete;
303 
304  std::filesystem::path m_file_path;
305 
306  std::map<std::string, spdlog::level::level_enum> m_level;
307 
308  std::map<std::string, std::shared_ptr<spdlog::logger>> m_logger;
309 
310  std::map<std::string, std::vector<std::shared_ptr<log_sink>>> m_logger_sinks;
311 
312  CallbackHook<void(const spdlog::level::level_enum&, const std::string&, const std::string&)> m_gui_callback;
313 
314  ProgramOptions m_descriptions;
315 
316  std::string m_enforce_level;
317 
318  std::vector<std::shared_ptr<hal::LogManager::log_sink>> m_default_sinks;
319 
320  static LogManager* m_instance;
321  };
322 
326  class log_gui_sink : public spdlog::sinks::base_sink<std::mutex>
327  {
328  public:
330  log_gui_sink() = default;
332  ~log_gui_sink() = default;
333 
334  protected:
336  void sink_it_(const spdlog::details::log_msg& msg) override;
337  void flush_() override;
338  };
339 } // namespace hal
#define CORE_API
Definition: arch_linux.h:28
log_gui_sink()=default
void sink_it_(const spdlog::details::log_msg &msg) override
Definition: log.cpp:439
void flush_() override
Definition: log.cpp:446
~log_gui_sink()=default
Definition: defines.h:45
std::string sink_type
Definition: log.h:115
std::filesystem::path path
Definition: log.h:118
std::shared_ptr< spdlog::sinks::sink > spdlog_sink
Definition: log.h:114