Search code examples
c++c++17spdlog

How I can paint a text background with spdlog?


I did a test with the {fmt} and was super easy to change background color, but I can't get the same with spdlog.
I can get the same result with spdlog, but is a weird code

fmt::print( bg( fmt::terminal_color::yellow ) | fg( fmt::terminal_color::black ), " {1:<{0}}\n", 120, "message" );  
        
spdlog::set_pattern( "%v" );
spdlog::info( "\033[43m\033[30m {1:<{0}} \033[m", 120, "message" );

Solution

  • If I understand your question correctly, you want to format your spdlog output using fmt, rather than having to specify the escape sequences yourself. For this, you can use the fmt::format function and use its output as a variable for spdlog:

    #include <spdlog/spdlog.h>
    #include <spdlog/fmt/bundled/color.h>
    
    int main(){
        spdlog::info( "Printing a string formatted with fmt: {}",
            fmt::format(
                fmt::bg( fmt::terminal_color::yellow ) |
                fmt::fg( fmt::terminal_color::black ) |
                fmt::emphasis::bold,
                "message"
            )
        );
        return 0;
    }