简体   繁体   中英

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" );

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM