slang-netlist  0.9.0
Loading...
Searching...
No Matches
Debug.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <fmt/format.h>
5#include <source_location>
6
7namespace slang::netlist {
8
11class Config {
12public:
13 bool debugEnabled{false};
14 bool quietEnabled{false};
15
16 Config() = default;
17
18 static auto getInstance() -> Config & {
19 static Config instance;
20 return instance;
21 }
22
23 Config(Config const &) = delete;
24 void operator=(Config const &) = delete;
25};
26
27inline auto file_name(const char *file) -> const char * {
28 return (strrchr(file, '/') != nullptr) ? strrchr(file, '/') + 1 : file;
29}
30
32template <typename... T>
33void DebugMessage(const std::source_location &location,
34 fmt::format_string<T...> fmt, T &&...args) {
35 fmt::print("{}:{}: ", file_name(location.file_name()), location.line());
36 fmt::print(fmt, std::forward<T>(args)...);
37}
38
40template <typename... T>
41void InfoMessage(fmt::format_string<T...> fmt, T &&...args) {
42 fmt::print(fmt, std::forward<T>(args)...);
43}
44
45} // namespace slang::netlist
46
47#ifdef SLANG_DEBUG
48#define DEBUG_PRINT(str, ...) \
49 if (netlist::Config::getInstance().debugEnabled) { \
50 DebugMessage(std::source_location::current(), \
51 str __VA_OPT__(, ) __VA_ARGS__); \
52 }
53#else
54#define DEBUG_PRINT(str, ...)
55#endif
56
57#define INFO_PRINT(str, ...) \
58 if (!Config::getInstance().quietEnabled) { \
59 InfoMessage(str __VA_OPT__(, ) __VA_ARGS__); \
60 }
bool quietEnabled
Definition Debug.hpp:14
bool debugEnabled
Definition Debug.hpp:13
void operator=(Config const &)=delete
static auto getInstance() -> Config &
Definition Debug.hpp:18
Config(Config const &)=delete
Definition Utilities.hpp:16
void InfoMessage(fmt::format_string< T... > fmt, T &&...args)
Print an informational message.
Definition Debug.hpp:41
auto file_name(const char *file) -> const char *
Definition Debug.hpp:27
void DebugMessage(const std::source_location &location, fmt::format_string< T... > fmt, T &&...args)
Print a debug message with the current file and line number.
Definition Debug.hpp:33