8#include <spdlog/fmt/bundled/format.h>
11#define LIBSTP_LOG_LEVEL_TRACE 0
12#define LIBSTP_LOG_LEVEL_DEBUG 1
13#define LIBSTP_LOG_LEVEL_INFO 2
14#define LIBSTP_LOG_LEVEL_WARN 3
15#define LIBSTP_LOG_LEVEL_ERROR 4
16#define LIBSTP_LOG_LEVEL_CRITICAL 5
17#define LIBSTP_LOG_LEVEL_OFF 6
23#ifndef FOUNDATION_LOG_ACTIVE_LEVEL
24#define FOUNDATION_LOG_ACTIVE_LEVEL LIBSTP_LOG_LEVEL_DEBUG
44 return static_cast<int>(level);
48 constexpr const char*
basename(
const char* path) {
49 const char* file = path;
51 if (*path ==
'/' || *path ==
'\\') {
73 void log(
Level level, std::string_view message);
77 void log(
Level level,
const char* source_file,
int line,
const char* func,
78 std::string_view message);
81 template <
typename... Args>
82 inline void logf(
Level level, std::string_view fmt_str, Args&&... args) {
86 if constexpr (
sizeof...(Args) == 0) {
89 auto args_tuple = std::make_tuple(std::forward<Args>(args)...);
90 auto message = std::apply(
91 [&](
auto&... unpacked) {
92 return fmt::vformat(fmt::string_view(fmt_str.data(), fmt_str.size()),
93 fmt::make_format_args(unpacked...));
101 template <
typename... Args>
102 inline void logf_loc(
Level level,
const char* file,
int line,
const char* func,
103 std::string_view fmt_str, Args&&... args) {
107 if constexpr (
sizeof...(Args) == 0) {
108 log(level, file, line, func, fmt_str);
110 auto args_tuple = std::make_tuple(std::forward<Args>(args)...);
111 auto message = std::apply(
112 [&](
auto&... unpacked) {
113 return fmt::vformat(fmt::string_view(fmt_str.data(), fmt_str.size()),
114 fmt::make_format_args(unpacked...));
117 log(level, file, line, func, message);
127 std::shared_ptr<spdlog::logger>
core();
130#define LIBSTP_LOG_CALL(level_enum, fmt, ...) \
132 if constexpr (::logging::detail::level_value(level_enum) >= FOUNDATION_LOG_ACTIVE_LEVEL) { \
133 ::logging::logf_loc(level_enum, __FILE__, __LINE__, \
134 static_cast<const char*>(__FUNCTION__), \
135 fmt __VA_OPT__(, __VA_ARGS__)); \
139#define LIBSTP_LOG_TRACE(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::trace, fmt __VA_OPT__(, __VA_ARGS__))
140#define LIBSTP_LOG_DEBUG(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::debug, fmt __VA_OPT__(, __VA_ARGS__))
141#define LIBSTP_LOG_INFO(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::info, fmt __VA_OPT__(, __VA_ARGS__))
142#define LIBSTP_LOG_WARN(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::warn, fmt __VA_OPT__(, __VA_ARGS__))
143#define LIBSTP_LOG_ERROR(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::error, fmt __VA_OPT__(, __VA_ARGS__))
144#define LIBSTP_LOG_CRITICAL(fmt, ...) LIBSTP_LOG_CALL(::logging::Level::critical, fmt __VA_OPT__(, __VA_ARGS__))
#define LIBSTP_LOG_LEVEL_TRACE
#define LIBSTP_LOG_LEVEL_ERROR
#define LIBSTP_LOG_LEVEL_DEBUG
#define LIBSTP_LOG_LEVEL_OFF
#define LIBSTP_LOG_LEVEL_INFO
#define LIBSTP_LOG_LEVEL_CRITICAL
#define LIBSTP_LOG_LEVEL_WARN
constexpr const char * basename(const char *path)
constexpr int level_value(Level level)
void initialize_timer()
Reset the relative elapsed-time clock used by the log formatter.
void init()
Initialize the shared logger, sinks, and formatter. Safe to call more than once.
void logf_loc(Level level, const char *file, int line, const char *func, std::string_view fmt_str, Args &&... args)
void log(Level level, std::string_view message)
Log a preformatted message without source-location context.
Level
Runtime severity used by the foundation logger and its Python bindings.
void logf(Level level, std::string_view fmt_str, Args &&... args)
std::shared_ptr< spdlog::logger > core()
Access the shared spdlog logger after initialization.
bool is_enabled(Level level)