34#ifndef RAV_ENABLE_SPDLOG
35 #define RAV_ENABLE_SPDLOG 0
40 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
43 #define SPDLOG_FUNCTION __PRETTY_FUNCTION__
46 #include <spdlog/spdlog.h>
49 #define RAV_LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
53 #define RAV_LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
56 #ifndef RAV_LOG_CRITICAL
57 #define RAV_LOG_CRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__)
61 #define RAV_LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
64 #ifndef RAV_LOG_WARNING
65 #define RAV_LOG_WARNING(...) SPDLOG_WARN(__VA_ARGS__)
69 #define RAV_LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
73 #define RAV_LOG(...) SPDLOG_INFO(__VA_ARGS__)
85 #define RAV_LOG_TRACE(...) \
86 if (log_level.load() >= LogLevel::trace) { \
87 fmt::println("[T] " __VA_ARGS__); \
92 #define RAV_LOG_DEBUG(...) \
93 if (log_level.load() >= LogLevel::debug) { \
94 fmt::println("[D] " __VA_ARGS__); \
98 #ifndef RAV_LOG_CRITICAL
99 #define RAV_LOG_CRITICAL(...) \
100 if (log_level.load() >= LogLevel::critical) { \
101 fmt::println("[C] " __VA_ARGS__); \
105 #ifndef RAV_LOG_ERROR
106 #define RAV_LOG_ERROR(...) \
107 if (log_level.load() >= LogLevel::error) { \
108 fmt::println("[E] " __VA_ARGS__); \
112 #ifndef RAV_LOG_WARNING
113 #define RAV_LOG_WARNING(...) \
114 if (log_level.load() >= LogLevel::warning) { \
115 fmt::println("[W] " __VA_ARGS__); \
120 #define RAV_LOG_INFO(...) \
121 if (log_level.load() >= LogLevel::info) { \
122 fmt::println("[I] " __VA_ARGS__); \
127 #define RAV_LOG(...) \
128 if (log_level.load() >= LogLevel::trace) { \
129 fmt::println("[L] " __VA_ARGS__); \
135#define CATCH_LOG_UNCAUGHT_EXCEPTIONS \
136 catch (const rav::Exception& e) { \
137 RAV_LOG_CRITICAL("rav::Exception caught: {} - please handle your exceptions before reaching this point.", e.what()); \
139 catch (const std::exception& e) { \
140 RAV_LOG_CRITICAL("std::exception caucght: {} - please handle your exceptions before reaching this point.", e.what()); \
143 RAV_LOG_CRITICAL("unknown exception caucght - please handle your exceptions before reaching this point."); \
163 spdlog::set_level(spdlog::level::trace);
165 spdlog::set_level(spdlog::level::debug);
167 spdlog::set_level(spdlog::level::info);
169 spdlog::set_level(spdlog::level::warn);
171 spdlog::set_level(spdlog::level::err);
173 spdlog::set_level(spdlog::level::critical);
175 spdlog::set_level(spdlog::level::off);
177 fmt::println(
"Invalid log level: {}. Setting log level to info.", level);
178 spdlog::set_level(spdlog::level::info);
196 fmt::println(
"Invalid log level: {}. Setting log level to info.", level);
217 if (
const auto env_value =
get_env(env_var)) {
void set_log_level_from_env(const char *env_var="RAV_LOG_LEVEL")
Tries to find given environment variable and set the log level accordingly.
std::optional< std::string > get_env(const char *name)
Gets the value of an environment variable.
void set_log_level(const char *level)
Sets the log level for the application based on the given string.
bool string_compare_case_insensitive(const std::string_view lhs, const std::string_view rhs)
Compares 2 strings case-insensitively.