Skip to content

Commit

Permalink
clean up fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Mar 2, 2025
1 parent 2db7664 commit 4ec391a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
20 changes: 6 additions & 14 deletions include/luisa/core/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ template<typename... Args>
auto trace = luisa::backtrace();
for (auto i = 0u; i < trace.size(); i++) {
auto &&t = trace[i];
using namespace std::string_view_literals;
error_message.append(luisa::format(
("\n {:>2} {}"sv),
i, luisa::to_string(t)));
fmt::format_to(std::back_inserter(error_message),
FMT_STRING("\n {:>2} {}"), i, t);
}
detail::default_logger().error(error_message);
std::abort();
Expand All @@ -89,41 +87,35 @@ LC_CORE_API void log_flush() noexcept;

}// namespace luisa

#ifdef LUISA_DISABLE_STATIC_FMT_STRING
#define LUISA_STATIC_FMT_STRING(s) (s)
#else
#define LUISA_STATIC_FMT_STRING(s) FMT_STRING(s)
#endif

/**
* @brief Verbose logging
*
* Ex. LUISA_VERBOSE("function {} returns {}", functionName, functionReturnInt);
*/
#define LUISA_VERBOSE(f, ...) \
::luisa::log_verbose(::fmt::format(LUISA_STATIC_FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
::luisa::log_verbose(::fmt::format(FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
/**
* @brief Info logging
*
* Ex. LUISA_INFO("function {} returns {}", functionName, functionReturnInt);
*/
#define LUISA_INFO(f, ...) \
::luisa::log_info(::fmt::format(LUISA_STATIC_FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
::luisa::log_info(::fmt::format(FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
/**
* @brief Warning logging
*
* Ex. LUISA_WARNING("function {} returns {}", functionName, functionReturnInt);
*/
#define LUISA_WARNING(f, ...) \
::luisa::log_warning(::fmt::format(LUISA_STATIC_FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
::luisa::log_warning(::fmt::format(FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
/**
* @brief Error logging
*
* After logging error message, the program will be aborted.
* Ex. LUISA_ERROR("function {} returns {}", functionName, functionReturnInt);
*/
#define LUISA_ERROR(f, ...) \
::luisa::log_error(::fmt::format(LUISA_STATIC_FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))
::luisa::log_error(::fmt::format(FMT_STRING(f) __VA_OPT__(, ) __VA_ARGS__))

/// LUISA_VERBOSE with file and line information
#define LUISA_VERBOSE_WITH_LOCATION(fmt, ...) \
Expand Down
30 changes: 16 additions & 14 deletions include/luisa/core/stl/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <luisa/core/basic_types.h>
#include <luisa/core/stl/string.h>
#include <luisa/core/platform.h>// for TraceItem

namespace luisa {

Expand Down Expand Up @@ -45,11 +46,11 @@ namespace fmt {

template<typename T, size_t N>
struct formatter<luisa::Vector<T, N>> {
constexpr auto parse(format_parse_context &ctx) const -> decltype(ctx.begin()) {
constexpr auto parse(format_parse_context &ctx) const noexcept {
return ctx.end();
}
template<typename FormatContext>
auto format(const luisa::Vector<T, N> &v, FormatContext &ctx) const -> decltype(ctx.out()) {
auto format(const luisa::Vector<T, N> &v, FormatContext &ctx) const noexcept {
using namespace std::string_view_literals;
using luisa::uint;
using luisa::ushort;
Expand Down Expand Up @@ -91,11 +92,11 @@ struct formatter<luisa::Vector<T, N>> {

template<size_t N>
struct formatter<luisa::Matrix<N>> {
constexpr auto parse(format_parse_context &ctx) const -> decltype(ctx.begin()) {
constexpr auto parse(format_parse_context &ctx) const noexcept {
return ctx.end();
}
template<typename FormatContext>
auto format(const luisa::Matrix<N> &m, FormatContext &ctx) const -> decltype(ctx.out()) {
auto format(const luisa::Matrix<N> &m, FormatContext &ctx) const noexcept {
if constexpr (N == 2u) {
return fmt::format_to(
ctx.out(),
Expand Down Expand Up @@ -132,16 +133,17 @@ struct formatter<luisa::Matrix<N>> {
}
};

// template<typename T, size_t N>
// struct formatter<std::array<T, N>> {
// constexpr auto parse(format_parse_context &ctx) const -> decltype(ctx.begin()) {
// return ctx.end();
// }
// template<typename FormatContext>
// auto format(const std::array<T, N> &a, FormatContext &ctx) const -> decltype(ctx.out()) {
// return fmt::format_to(ctx.out(), FMT_STRING("[{}]"), fmt::join(a, ", "));
// }
// };
template<>
struct formatter<luisa::TraceItem> {
constexpr auto parse(format_parse_context &ctx) const noexcept {
return ctx.end();
}
template<typename FormatContext>
auto format(const luisa::TraceItem &item, FormatContext &ctx) const noexcept {
return luisa::format_to(ctx.out(), FMT_STRING("[0x{:012x}]: {} :: {} + {}"),
item.address, item.module, item.symbol, item.offset);
}
};

}// namespace fmt

Expand Down
4 changes: 1 addition & 3 deletions src/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,6 @@ luisa::string current_executable_path() noexcept {
namespace luisa {
luisa::string to_string(const TraceItem &item) noexcept {
using namespace std::string_view_literals;
return luisa::format(
FMT_STRING("[0x{:012x}]: {} :: {} + {}"sv),
item.address, item.module, item.symbol, item.offset);
return luisa::format("{}", item);
}
}// namespace luisa

0 comments on commit 4ec391a

Please sign in to comment.