light/modules/logger/logger.cppm

180 lines
4.3 KiB
Text
Raw Normal View History

export module logger;
2025-11-16 14:29:03 +03:30
import lsd;
namespace lt::log {
2025-07-16 13:56:59 +03:30
2025-11-16 14:29:03 +03:30
auto thread_hash_id() noexcept -> u64
{
return static_cast<u64>(lsd::hash<lsd::thread_id> {}(lsd::this_thread_id()));
}
} // namespace lt::log
export namespace lt::log {
2025-07-16 13:56:59 +03:30
/** Severity of a log message. */
2025-11-16 14:29:03 +03:30
enum class Level : u8
2025-07-05 13:28:41 +03:30
{
2025-07-06 16:30:38 +03:30
/** Lowest and most vebose log level, for tracing execution paths and events */
2025-11-16 14:29:03 +03:30
trace = 0u,
2025-07-05 13:28:41 +03:30
2025-07-06 16:30:38 +03:30
/** Vebose log level, for enabling temporarily to debug */
2025-11-16 14:29:03 +03:30
debug = 1u,
2025-07-06 16:30:38 +03:30
/** General information */
2025-11-16 14:29:03 +03:30
info = 2u,
2025-07-06 16:30:38 +03:30
/** Things we should to be aware of and edge cases */
2025-11-16 14:29:03 +03:30
warn = 3u,
2025-07-06 16:30:38 +03:30
/** Defects, bugs and undesired behaviour */
2025-11-16 14:29:03 +03:30
error = 4u,
2025-07-06 16:30:38 +03:30
/** Unrecoverable errors */
2025-11-16 14:29:03 +03:30
critical = 5u,
2025-07-06 16:30:38 +03:30
/** No logging */
2025-11-16 14:29:03 +03:30
off = 6u,
2025-07-06 16:30:38 +03:30
};
template<typename... Args>
struct [[maybe_unused]] print
{
[[maybe_unused]] print(
Level level,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location,
lsd::format_str<Args...> format,
Args &&...arguments
) noexcept
2025-07-05 13:28:41 +03:30
{
constexpr auto to_string = [](Level level, auto location) {
// clang-format off
switch (level)
{
using enum ::lt::log::Level;
case trace : return "\033[1;37m| trc |\033[0m";
case debug : return "\033[1;36m| dbg |\033[0m";
case info : return "\033[1;32m| inf |\033[0m";
case warn : return "\033[1;33m| wrn |\033[0m";
case error : return "\033[1;31m| err |\033[0m";
case critical: return "\033[1;41m| crt |\033[0m";
case off: return "off";
}
// clang-format on
2025-11-17 13:17:05 +03:30
lsd::unreachable();
};
2025-11-17 13:17:05 +03:30
const auto path = lsd::filesystem::path { location.file_name() };
2025-11-17 13:17:05 +03:30
lsd::println(
"{} {} ==> {}",
to_string(level, location),
2025-11-17 13:17:05 +03:30
lsd::format("{}:{}", path.filename().string(), location.line()),
lsd::format(format, lsd::forward<Args>(arguments)...)
);
2025-07-05 13:28:41 +03:30
}
};
template<typename... Args>
2025-11-16 14:29:03 +03:30
print(Level, const lsd::src_location &, lsd::format_str<Args...>, Args &&...) noexcept
-> print<Args...>;
2025-07-05 13:28:41 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] trace
{
[[maybe_unused]] trace(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::trace, location, format, lsd::forward<Args>(arguments)...);
}
};
2025-11-16 14:29:03 +03:30
template<typename... Args>
trace(lsd::format_str<Args...>, Args &&...) noexcept -> trace<Args...>;
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] debug
{
[[maybe_unused]] debug(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::debug, location, format, lsd::forward<Args>(arguments)...);
}
2025-07-05 13:28:41 +03:30
};
2025-11-16 14:29:03 +03:30
template<typename... Args>
debug(lsd::format_str<Args...>, Args &&...) noexcept -> debug<Args...>;
2025-07-05 13:28:41 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] info
2025-07-06 16:30:38 +03:30
{
[[maybe_unused]] info(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::info, location, format, lsd::forward<Args>(arguments)...);
}
};
2025-07-06 16:30:38 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
info(lsd::format_str<Args...>, Args &&...) noexcept -> info<Args...>;
2025-07-06 16:30:38 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] warn
2025-07-06 16:30:38 +03:30
{
[[maybe_unused]] warn(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::warn, location, format, lsd::forward<Args>(arguments)...);
}
};
2025-11-16 14:29:03 +03:30
template<typename... Args>
warn(lsd::format_str<Args...>, Args &&...) noexcept -> warn<Args...>;
2025-07-06 16:30:38 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] error
2025-07-06 16:30:38 +03:30
{
[[maybe_unused]] error(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::error, location, format, lsd::forward<Args>(arguments)...);
}
};
2025-11-16 14:29:03 +03:30
template<typename... Args>
error(lsd::format_str<Args...>, Args &&...) noexcept -> error<Args...>;
2025-07-06 16:30:38 +03:30
2025-11-16 14:29:03 +03:30
template<typename... Args>
struct [[maybe_unused]] critical
2025-07-06 16:30:38 +03:30
{
[[maybe_unused]] critical(
2025-11-16 14:29:03 +03:30
lsd::format_str<Args...> format,
Args &&...arguments,
2025-11-16 14:29:03 +03:30
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
2025-11-17 13:17:05 +03:30
print(Level::critical, location, format, lsd::forward<Args>(arguments)...);
}
};
2025-11-16 14:29:03 +03:30
template<typename... Args>
critical(lsd::format_str<Args...>, Args &&...) noexcept -> critical<Args...>;
} // namespace lt::log