dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
logging.h
1
13#pragma once
14
15#ifndef NLOG
16
17#include <fmt/core.h> // IWYU pragma: export
18#include <fmt/ostream.h> // IWYU pragma: export
19#include <fmt/ranges.h> // IWYU pragma: export
20#include <spdlog/logger.h>
21
22#include <memory>
23
24namespace dlinear {
25
26enum class LoggerType { OUT, ERR };
27
28std::shared_ptr<spdlog::logger> get_logger(LoggerType logger_type); // NOLINT
29
30} // namespace dlinear
31
32#define OSTREAM_FORMATTER(type) \
33 template <> \
34 struct fmt::formatter<type> : ostream_formatter {};
35#define DLINEAR_FORMAT(message, ...) fmt::format(message, __VA_ARGS__)
36
37#define DLINEAR_VERBOSITY_TO_LOG_LEVEL(verbosity) \
38 ((verbosity) == 0 \
39 ? spdlog::level::critical \
40 : ((verbosity) == 1 \
41 ? spdlog::level::err \
42 : ((verbosity) == 2 \
43 ? spdlog::level::warn \
44 : ((verbosity) == 3 \
45 ? spdlog::level::info \
46 : ((verbosity) == 4 ? spdlog::level::debug \
47 : ((verbosity) == 5 ? spdlog::level::trace : spdlog::level::off))))))
48#define DLINEAR_LOG_INIT_VERBOSITY(verbosity) DLINEAR_LOG_INIT_LEVEL(DLINEAR_VERBOSITY_TO_LOG_LEVEL(verbosity))
49#define DLINEAR_LOG_INIT_LEVEL(level) \
50 do { \
51 ::dlinear::get_logger(::dlinear::LoggerType::OUT)->set_level(level); \
52 ::dlinear::get_logger(::dlinear::LoggerType::ERR)->set_level(level); \
53 } while (0)
54#define DLINEAR_TRACE(msg) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->trace(msg)
55#define DLINEAR_TRACE_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->trace(msg, __VA_ARGS__)
56#define DLINEAR_DEBUG(msg) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->debug(msg)
57#define DLINEAR_DEBUG_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->debug(msg, __VA_ARGS__)
58#define DLINEAR_INFO(msg) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->info(msg)
59#define DLINEAR_INFO_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::OUT)->info(msg, __VA_ARGS__)
60#define DLINEAR_WARN(msg) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->warn(msg)
61#define DLINEAR_WARN_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->warn(msg, __VA_ARGS__)
62#define DLINEAR_ERROR(msg) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->error(msg)
63#define DLINEAR_ERROR_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->error(msg, __VA_ARGS__)
64#define DLINEAR_CRITICAL(msg) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->critical(msg)
65#define DLINEAR_CRITICAL_FMT(msg, ...) ::dlinear::get_logger(::dlinear::LoggerType::ERR)->critical(msg, __VA_ARGS__)
66#define DLINEAR_INFO_ENABLED (::dlinear::get_logger(::dlinear::LoggerType::OUT)->should_log(spdlog::level::info))
67#define DLINEAR_TRACE_ENABLED (::dlinear::get_logger(::dlinear::LoggerType::OUT)->should_log(spdlog::level::trace))
68
69#ifndef NDEBUG
70
71#include <sstream>
72#include <thread>
73
74#define DLINEAR_DEV(msg) \
75 do { \
76 if (::dlinear::get_logger(::dlinear::LoggerType::ERR)->should_log(spdlog::level::err)) \
77 fmt::println("[{:%Y-%m-%d %H:%M:%S}] [\033[1m\033[35mDEV\033[0m] [thread {}] " msg "", \
78 std::chrono::system_clock::now(), std::hash<std::thread::id>{}(std::this_thread::get_id())); \
79 std::cout << std::flush; \
80 } while (0)
81#define DLINEAR_DEV_FMT(msg, ...) \
82 do { \
83 if (::dlinear::get_logger(::dlinear::LoggerType::ERR)->should_log(spdlog::level::err)) \
84 fmt::println("[{:%Y-%m-%d %H:%M:%S}] [\033[1m\033[35mDEV\033[0m] [thread {}] " msg "", \
85 std::chrono::system_clock::now(), std::hash<std::thread::id>{}(std::this_thread::get_id()), \
86 __VA_ARGS__); \
87 std::cout << std::flush; \
88 } while (0)
89
90#define DLINEAR_DEV_TRACE(msg) DLINEAR_DEV(msg)
91#define DLINEAR_DEV_TRACE_FMT(msg, ...) DLINEAR_DEV_FMT(msg, __VA_ARGS__)
92#define DLINEAR_DEV_DEBUG(msg) DLINEAR_DEV(msg)
93#define DLINEAR_DEV_DEBUG_FMT(msg, ...) DLINEAR_DEV_FMT(msg, __VA_ARGS__)
94#else
95#define DLINEAR_DEV(msg) void(0)
96#define DLINEAR_DEV_FMT(msg, ...) void(0)
97#define DLINEAR_DEV_TRACE(msg) DLINEAR_TRACE(msg)
98#define DLINEAR_DEV_TRACE_FMT(msg, ...) DLINEAR_TRACE_FMT(msg, __VA_ARGS__)
99#define DLINEAR_DEV_DEBUG(msg) DLINEAR_DEBUG(msg)
100#define DLINEAR_DEV_DEBUG_FMT(msg, ...) DLINEAR_DEBUG_FMT(msg, __VA_ARGS__)
101#endif
102
103#else
104
105#define OSTREAM_FORMATTER(type)
106#define DLINEAR_FORMAT(message, ...) fmt::format(message, __VA_ARGS__)
107#define DLINEAR_VERBOSITY_TO_LOG_LEVEL(verbosity) 0
108#define DLINEAR_LOG_INIT_LEVEL(level) void(0)
109#define DLINEAR_LOG_INIT_VERBOSITY(verbosity) void(0)
110#define DLINEAR_TRACE(msg) void(0)
111#define DLINEAR_TRACE_FMT(msg, ...) void(0)
112#define DLINEAR_DEBUG(msg) void(0)
113#define DLINEAR_DEBUG_FMT(msg, ...) void(0)
114#define DLINEAR_INFO(msg) void(0)
115#define DLINEAR_INFO_FMT(msg, ...) void(0)
116#define DLINEAR_WARN(msg) void(0)
117#define DLINEAR_WARN_FMT(msg, ...) void(0)
118#define DLINEAR_ERROR(msg) void(0)
119#define DLINEAR_ERROR_FMT(msg, ...) void(0)
120#define DLINEAR_CRITICAL(msg) void(0)
121#define DLINEAR_CRITICAL_FMT(msg, ...) void(0)
122#define DLINEAR_INFO_ENABLED false
123#define DLINEAR_TRACE_ENABLED false
124#define DLINEAR_DEV(msg) void(0)
125#define DLINEAR_DEV_FMT(msg, ...) void(0)
126#define DLINEAR_DEV_TRACE(msg) void(0)
127#define DLINEAR_DEV_TRACE_FMT(msg, ...) void(0)
128#define DLINEAR_DEV_DEBUG(msg) void(0)
129#define DLINEAR_DEV_DEBUG_FMT(msg, ...) void(0)
130
131#endif
Global namespace for the dlinear library.