Skip to content

Commit

Permalink
Use spdlog bundled fmt instead of std::format
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Nov 6, 2024
1 parent 3882753 commit 8698708
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 72 deletions.
6 changes: 3 additions & 3 deletions Engine/Source/Editor/Layer/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,10 @@ void ImGuiLayer::ShowLog()
for (size_t i = 0; i < logInfos.size(); ++i)
{
const auto &info = logInfos[i];
if ((s_levelFilter & (uint8_t)info.m_level) && s_textFilter.PassFilter(info.m_text.data()))
if ((s_levelFilter & (uint8_t)info.m_level) && s_textFilter.PassFilter(info.m_content.data()))
{
ImGui::PushStyleColor(ImGuiCol_Text, LogLevelToColor(info.m_level));
ImGui::TextUnformatted(info.m_text.data());
ImGui::TextUnformatted(info.m_content.data());
ImGui::PopStyleColor();
}
}
Expand All @@ -555,7 +555,7 @@ void ImGuiLayer::ShowLog()
{
const auto &info = logInfos[i];
ImGui::PushStyleColor(ImGuiCol_Text, LogLevelToColor(info.m_level));
ImGui::TextUnformatted(info.m_text.data());
ImGui::TextUnformatted(info.m_content.data());
ImGui::PopStyleColor();
}
}
Expand Down
15 changes: 12 additions & 3 deletions Engine/Source/Engine/Core/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ namespace

constexpr std::array<LogLevel, 6> SpdLevelToSLLevel =
{
LogLevel::Trace, LogLevel::Debug, LogLevel::Info, LogLevel::Warn, LogLevel::Error, LogLevel::Fatal,
LogLevel::Trace,
LogLevel::Debug,
LogLevel::Info,
LogLevel::Warn,
LogLevel::Error,
LogLevel::Fatal,
};

}
} // namespace

void Log::Init()
{
Expand All @@ -39,7 +44,11 @@ void Log::Init()
auto pCallbackSink = std::make_shared<spdlog::sinks::callback_sink_mt>([](const spdlog::details::log_msg &msg)
{
// TODO: Add time information to log buffer.
m_logInfos.emplace_back(SpdLevelToSLLevel[msg.level], msg.payload);
m_logInfos.emplace_back
(
SpdLevelToSLLevel[msg.level],
std::string{ msg.payload.data(), msg.payload.size() }
);
});

std::vector<spdlog::sink_ptr> sinks{ pConsoleSink, pFileSink, pCallbackSink };
Expand Down
80 changes: 22 additions & 58 deletions Engine/Source/Engine/Core/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ enum class LogLevel : uint8_t
{
Trace = 1 << 0,
Debug = 1 << 1,
Info = 1 << 2,
Warn = 1 << 3,
Info = 1 << 2,
Warn = 1 << 3,
Error = 1 << 4,
Fatal = 1 << 5,
};

struct LogInfo
{
LogInfo(LogLevel level, std::string_view text) :
m_level(level), m_text(text)
{

}

LogLevel m_level;
std::string m_text;
std::string m_content;
};

} // namespace sl
Expand All @@ -39,9 +33,9 @@ struct LogInfo
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <spdlog/spdlog.h>
#include <spdlog/fmt/bundled/format.h>

#include <memory>
#include <sstream>

namespace sl
{
Expand All @@ -68,86 +62,56 @@ class Log final
} // namespace sl

template<>
struct std::formatter<glm::vec2> : std::formatter<std::string>
struct fmt::formatter<glm::vec2> : fmt::formatter<std::string>
{
auto format(const glm::vec2 &vec, std::format_context &context) const
auto format(const glm::vec2 &vec, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(std::format("vec2({}, {})", vec.x, vec.y), context);
return fmt::format_to(ctx.out(), "vec2({}, {})", vec.x, vec.y);
}
};

template<>
struct std::formatter<glm::vec3> : std::formatter<std::string>
struct fmt::formatter<glm::vec3> : fmt::formatter<std::string>
{
auto format(const glm::vec3 &vec, std::format_context &context) const
auto format(const glm::vec3 &vec, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(std::format("vec3({}, {}, {})", vec.x, vec.y, vec.z), context);
return fmt::format_to(ctx.out(), "vec3({}, {}, {})", vec.x, vec.y, vec.z);
}
};

template<>
struct std::formatter<glm::vec4> : std::formatter<std::string>
struct fmt::formatter<glm::vec4> : fmt::formatter<std::string>
{
auto format(const glm::vec4 &vec, std::format_context &context) const
auto format(const glm::vec4 &vec, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(std::format("vec4({}, {}, {}, {})", vec.x, vec.y, vec.z, vec.w), context);
return fmt::format_to(ctx.out(), "vec4({}, {}, {}, {})", vec.x, vec.y, vec.z, vec.w);
}
};

template<>
struct std::formatter<glm::ivec2> : std::formatter<std::string>
struct fmt::formatter<glm::ivec2> : fmt::formatter<std::string>
{
auto format(const glm::ivec2 &vec, std::format_context &context) const
auto format(const glm::ivec2 &vec, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(std::format("ivec2({}, {})", vec.x, vec.y), context);
return fmt::format_to(ctx.out(), "ivec2({}, {})", vec.x, vec.y);
}
};

template<>
struct std::formatter<glm::ivec3> : std::formatter<std::string>
struct fmt::formatter<glm::ivec3> : fmt::formatter<std::string>
{
auto format(const glm::ivec3 &vec, std::format_context &context) const
auto format(const glm::ivec3 &vec, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(std::format("ivec3({}, {}, {})", vec.x, vec.y, vec.z), context);
return fmt::format_to(ctx.out(), "ivec3({}, {}, {})", vec.x, vec.y, vec.z);
}
};

template<>
struct std::formatter<glm::ivec4> : std::formatter<std::string>
{
auto format(const glm::ivec4 &vec, std::format_context &context) const
{
return formatter<string>::format(std::format("ivec4({}, {}, {}, {})", vec.x, vec.y, vec.z, vec.w), context);
}
};

// TODO: Such a dirty implementation, improve it.
template<glm::length_t C, glm::length_t R, typename T, glm::qualifier Q>
struct std::formatter<glm::mat<C, R, T, Q>> : std::formatter<std::string>
struct fmt::formatter<glm::ivec4> : fmt::formatter<std::string>
{
auto format(const glm::mat<C, R, T, Q> &mat, std::format_context &context) const
auto format(const glm::ivec4 &vec, format_context &ctx) const -> decltype(ctx.out())
{
std::stringstream ss;
ss << "mat" << C << "x" << R << "(";
for (glm::length_t i = 0; i < C; ++i)
{
for (glm::length_t j = 0; j < R; ++j)
{
ss << mat[i][j];
if (j < R - 1)
{
ss << ", ";
}
}
if (i < C - 1)
{
// To approach [xx:xx:xx] Logger: matCxR(
ss << "\n" << " ";
}
}
ss << ")";

return formatter<string>::format(ss.str(), context);
return fmt::format_to(ctx.out(), "ivec4({}, {}, {}, {})", vec.x, vec.y, vec.z, vec.w);
}
};

Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Engine/Core/Time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Clock final
float GetDeltatIme() const { return m_deltaTime; }

private:
std::chrono::steady_clock::time_point m_lastTimePoint;
float m_deltaTime = 0.0f;
std::chrono::steady_clock::time_point m_lastTimePoint;
};

class Timer final
Expand Down
13 changes: 9 additions & 4 deletions Engine/Source/Engine/Event/Event.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "Core/Log.h"

#include <concepts>
#include <cstdint>
#include <format>
#include <functional>

namespace sl
Expand Down Expand Up @@ -89,11 +90,15 @@ using EventCallback = std::function<void(Event &)>;

} // namespace sl

#if !defined(SL_FINAL)

template<>
struct std::formatter<sl::Event> : std::formatter<std::string>
struct fmt::formatter<sl::Event> : fmt::formatter<std::string>
{
auto format(const sl::Event &event, std::format_context &context) const
auto format(const sl::Event &event, format_context &ctx) const -> decltype(ctx.out())
{
return formatter<string>::format(event.ToString(), context);
return fmt::format_to(ctx.out(), "{}", event.ToString());
}
};

#endif
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Third party
[premake-core](https://github.com/premake/premake-core) v5.0.0-beta2

[spdlog](https://github.com/gabime/spdlog) v1.13.0
[spdlog](https://github.com/gabime/spdlog) v1.14.1

[SDL](https://github.com/libsdl-org/SDL) v2.30.7

Expand Down
2 changes: 1 addition & 1 deletion Script/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ project("Editor")
{
"SL_ROOT_PATH=\""..RootPath.."\"",
"SL_ASSET_PATH=\""..path.join(RootPath, "Engine/Asset").."\"",
"SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
"SPDLOG_NO_EXCEPTIONS",
}

-- Include paths
Expand Down
2 changes: 1 addition & 1 deletion Script/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project("Slam")
{
"SL_ROOT_PATH=\""..RootPath.."\"",
"SL_ASSET_PATH=\""..path.join(RootPath, "Engine/Asset").."\"",
"SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
"SPDLOG_NO_EXCEPTIONS",
"YAML_CPP_STATIC_DEFINE",
}

Expand Down

0 comments on commit 8698708

Please sign in to comment.