Skip to content

Commit

Permalink
Ues std::format instead of bundled fmt for spdlog. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas authored Apr 22, 2024
1 parent 2320dfa commit c2d4c2f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
3 changes: 1 addition & 2 deletions Engine/Auto/Scripts/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ project("Editor")

if ENABLE_SPDLOG then
defines {
-- TODO : Remove _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING after spdlog updates the format to the right version.
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "FMT_USE_NONTYPE_TEMPLATE_ARGS=0", "_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING",
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
}

includedirs {
Expand Down
3 changes: 1 addition & 2 deletions Engine/Auto/Scripts/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ project("Engine")
}

defines {
-- TODO : Remove _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING after spdlog updates the format to the right version.
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "FMT_USE_NONTYPE_TEMPLATE_ARGS=0", "_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING",
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
}
end

Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ void AssetBrowser::ImportAssetFile(const char* pFilePath)
std::filesystem::path absolutePath = CDPROJECT_RESOURCES_ROOT_PATH;
absolutePath /= relativePath;

CD_INFO("Compile skybox textures to {0}.", absolutePath);
CD_INFO("Compile skybox textures to {0}.", absolutePath.generic_string());

std::string irrdianceOutput = absolutePath.generic_string() + "_irr.dds";
ResourceBuilder::Get().AddIrradianceCubeMapBuildTask(pFilePath, irrdianceOutput.c_str());
Expand Down
35 changes: 10 additions & 25 deletions Engine/Source/Runtime/Log/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,10 @@
namespace engine
{

std::shared_ptr<spdlog::logger> Log::s_engineLogger;
std::shared_ptr<spdlog::logger> Log::s_applicationLogger;
std::shared_ptr<spdlog::logger> Log::s_pEngineLogger;
std::shared_ptr<spdlog::logger> Log::s_pApplicationLogger;
std::ostringstream Log::m_oss;

std::shared_ptr<spdlog::logger>& Log::GetEngineLogger()
{
return s_engineLogger;
}

std::shared_ptr<spdlog::logger>& Log::GetApplicationLogger()
{
return s_applicationLogger;
}

const std::ostringstream& Log::GetSpdOutput()
{
return m_oss;
}

void Log::ClearBuffer()
{
m_oss.str("");
Expand All @@ -45,15 +30,15 @@ void Log::Init() {

std::vector<spdlog::sink_ptr> sinks{ consoleSink, fileSink, ossSink };

s_engineLogger = std::make_shared<spdlog::logger>("ENGINE", sinks.begin(), sinks.end());
spdlog::register_logger(s_engineLogger);
s_engineLogger->set_level(spdlog::level::trace);
s_engineLogger->flush_on(spdlog::level::trace);
s_pEngineLogger = std::make_shared<spdlog::logger>("ENGINE", sinks.begin(), sinks.end());
spdlog::register_logger(s_pEngineLogger);
s_pEngineLogger->set_level(spdlog::level::trace);
s_pEngineLogger->flush_on(spdlog::level::trace);

s_applicationLogger = std::make_shared<spdlog::logger>("EDITOR", sinks.begin(), sinks.end());
spdlog::register_logger(s_applicationLogger);
s_applicationLogger->set_level(spdlog::level::trace);
s_applicationLogger->flush_on(spdlog::level::trace);
s_pApplicationLogger = std::make_shared<spdlog::logger>("EDITOR", sinks.begin(), sinks.end());
spdlog::register_logger(s_pApplicationLogger);
s_pApplicationLogger->set_level(spdlog::level::trace);
s_pApplicationLogger->flush_on(spdlog::level::trace);
}

}
Expand Down
60 changes: 38 additions & 22 deletions Engine/Source/Runtime/Log/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include "Math/Quaternion.hpp"

#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>

#include <sstream>

namespace engine
{
Expand All @@ -16,15 +13,15 @@ class Log
{
public:
static void Init();
static std::shared_ptr<spdlog::logger>& GetEngineLogger();
static std::shared_ptr<spdlog::logger>& GetApplicationLogger();
static std::shared_ptr<spdlog::logger> GetEngineLogger() { return s_pEngineLogger; }
static std::shared_ptr<spdlog::logger> GetApplicationLogger() { return s_pApplicationLogger; }

static const std::ostringstream& GetSpdOutput();
static const std::ostringstream& GetSpdOutput() { return m_oss; }
static void ClearBuffer();

private:
static std::shared_ptr<spdlog::logger> s_engineLogger;
static std::shared_ptr<spdlog::logger> s_applicationLogger;
static std::shared_ptr<spdlog::logger> s_pEngineLogger;
static std::shared_ptr<spdlog::logger> s_pApplicationLogger;

// Note that m_oss will be cleared after OutputLog::AddSpdLog be called.
static std::ostringstream m_oss;
Expand All @@ -47,36 +44,55 @@ class Log
#define CD_FATAL(...) ::engine::Log::GetApplicationLogger()->critical(__VA_ARGS__)

// Runtime assert.
#define CD_ENGINE_ASSERT(x, ...) { if(!(x)) { ::engine::Log::GetEngineLogger()->error(__VA_ARGS__); } }
#define CD_ASSERT(x, ...) { if(!(x)) { ::engine::Log::GetApplicationLogger()->error(__VA_ARGS__); } }
#define CD_ENGINE_ASSERT(x, ...) { if(!(x)) { CD_FATAL(...); __debugbreak(); } }
#define CD_ASSERT(x, ...) { if(!(x)) { CD_FATAL(...); __debugbreak(); } }

inline std::ostream& operator<<(std::ostream& os, const cd::Vec2f& vec)
template<>
struct std::formatter<cd::Vec2f> : std::formatter<std::string>
{
return os << std::format("({0}, {1})", vec.x(), vec.y());
}
auto format(const cd::Vec2f& vec, std::format_context& context) const
{
return formatter<string>::format(std::format("vec2:({}, {})", vec.x(), vec.y()), context);
}
};

inline std::ostream& operator<<(std::ostream& os, const cd::Vec3f& vec)
template<>
struct std::formatter<cd::Vec3f> : std::formatter<std::string>
{
return os << std::format("({0}, {1}, {2})", vec.x(), vec.y(), vec.z());
}
auto format(const cd::Vec3f& vec, std::format_context& context) const
{
return formatter<string>::format(std::format("vec3:({}, {}, {})", vec.x(), vec.y(), vec.z()), context);
}
};

inline std::ostream& operator<<(std::ostream& os, const cd::Vec4f& vec)
template<>
struct std::formatter<cd::Vec4f> : std::formatter<std::string>
{
return os << std::format("({0}, {1}, {2}, {3})", vec.x(), vec.y(), vec.z(), vec.w());
}
auto format(const cd::Vec4f& vec, std::format_context& context) const
{
return formatter<string>::format(std::format("vec4:({}, {}, {}, {})", vec.x(), vec.y(), vec.z(), vec.w()), context);
}
};

inline std::ostream& operator<<(std::ostream& os, const cd::Quaternion& quaternion)
template<>
struct std::formatter<cd::Quaternion> : std::formatter<std::string>
{
return os << std::format("Vector = ({0}, {1}, {2}), Scalar = {3}", quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w());
}
auto format(const cd::Quaternion& qua, std::format_context& context) const
{
return formatter<string>::format(std::format("Vector = ({}, {}, {}), Scalar = {}", qua.x(), qua.y(), qua.z(), qua.w()), context);
}
};

#else

namespace engine
{
class Log
{
public:
static void Init() {}
};
}

#define CD_ENGINE_TRACE(...)
#define CD_ENGINE_INFO(...)
Expand Down

0 comments on commit c2d4c2f

Please sign in to comment.