Search code examples
c++glfwspdlog

Cannot format an argument. To make type T formattable provide a frormatter <T>


So im creating a game engine in accordance to thecherno's tutorial and I am adding GLFW Error handling(This is c++) and I cannot figure out where and how to add a formatter for SPDLOG Here is my Log.h:

#define PL_CORE_TRACE(...)      ::Pluton::Log::GetCoreLogger()->trace(__VA_ARGS__)
#define PL_CORE_WARN(...)       ::Pluton::Log::GetCoreLogger()->warn(__VA_ARGS__)
#define PL_CORE_INFO(...)       ::Pluton::Log::GetCoreLogger()->info(__VA_ARGS__)
#define PL_CORE_ERROR(...)      ::Pluton::Log::GetCoreLogger()->error(__VA_ARGS__)
#define PL_CORE_FATAL(...)      ::Pluton::Log::GetCoreLogger()->fatal(__VA_ARGS__)

__VA_ARGS__ is all the arguments given to the logger

I binded an OnEvent Function that logs like so:

    void Application::OnEvent(Event& e) {
    PL_CORE_INFO("{0}", e);
}

I bound the glfwSetErrorCallback event to GLFWErrorCallback function like so:

    static void GLFWErrorCallback(int error, const char* description) {
    PL_CORE_ERROR("GLFW Error ({0}): {1}",error,description);
}
//this is a snippit in Application::Init() which initializes GLFW
    if (!s_GLFWInitialized) {
                int success = glfwInit();
                PL_CORE_ASSERT("GLFW NOT INITIALIZED");
                glfwSetErrorCallback(GLFWErrorCallback);
                s_GLFWInitialized = true;
            }

I keep getting the error:

Cannot format an argument. To make type T formattable provide a formatter<T> specialization.

It is the only error and does not specify which one is causing this error as well as which type that cannot be formatted.

EDIT:

I Created this in which I wanted to return that as the format for the type of Event and I put it into my Log.cpp file which is imported in every file that uses event. The same error occurs

struct fmt::formatter<Pluton::Event> {
    constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
        return ctx.end();
    }

    template <typename FormatContext>
    auto format(const Pluton::Event& input, FormatContext& ctx) -> decltype(ctx.out()) {
        return format_to(ctx.out(),
            "(Name:{})",
            input.GetName() {});
    }

};

Solution

  • Add #include <spdlog/fmt/ostr.h> to Log.h file. With this spdlog would be able to use operator<< in Event.h