Home > Software engineering >  C spdlog use variables
C spdlog use variables

Time:09-16

I'm new to spdlog and following a tutorial which looks like this:

Log.h

#pragma once

#include "spdlog/spdlog.h"
#include "spdlog/fmt/ostr.h"

namespace Engine{
    class Log{
    public:
        static void init();
        inline static std::shared_ptr<spdlog::logger>& GetCoreLoger() { return s_CoreLogger; }
        inline static std::shared_ptr<spdlog::logger>& GetClientLogger  () { return s_ClientLogger;}
    private:
        static std::shared_ptr<spdlog::logger> s_CoreLogger;
        static std::shared_ptr<spdlog::logger> s_ClientLogger;
    };
}

//Client log macros
#define TRACE(...)      ::Engine::Log::GetClientLogger()->trace(__VA_ARGS__)
#define INFO(...)      ::Engine::Log::GetClientLogger()->info(__VA_ARGS__)
#define WARN(...)      ::Engine::Log::GetClientLogger()->warn(__VA_ARGS__)
#define ERROR(...)     ::Engine::Log::GetClientLogger()->error(__VA_ARGS__)

Log.cpp

#include "spdlog/sinks/stdout_color_sinks.h"

namespace Engine {
    std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
    std::shared_ptr<spdlog::logger> Log::s_ClientLogger;

    void Log::init() {
        //The printing pattern, can be changed for preferance,
        spdlog::set_pattern("%^[%T] %n: %v%$");
        s_CoreLogger = spdlog::stdout_color_mt("VIO");
        s_CoreLogger->set_level(spdlog::level::trace);

        s_ClientLogger = spdlog::stdout_color_mt("APP");
        s_ClientLogger->set_level(spdlog::level::trace);
    }
};

This is ample for my work but I cannot seem to use variable's with it. I want to use something like this:

int test_var = 12;
INFO("The variable is: ", test_var, ".");

To get an output of:

[23:01:24] APP: The variable is: 12.

Right now the first [23:01:24] APP: The variable is: part is working but for some reason I can't seem to have it display the variable.

How can I achieve this?

CodePudding user response:

According to the spdlog's wiki pages, your formatting syntax is incorrect.

For formatting a variable, a placeholder {} is required.

Try this:

int test_var = 12;
INFO("The variable is: {}{}", test_var, ".");
//                     ^^^^ adding these placeholders
  • Related