如何使用spdlog绘制文本背景



我用{fmt}做了一个测试,非常容易更改背景色,但我无法用spdlog获得相同的结果
我可以用spdlog得到同样的结果,但这是一个奇怪的代码

fmt::print( bg( fmt::terminal_color::yellow ) | fg( fmt::terminal_color::black ), " {1:<{0}}n", 120, "message" );  

spdlog::set_pattern( "%v" );
spdlog::info( "33[43m33[30m {1:<{0}} 33[m", 120, "message" );

如果我正确理解您的问题,您希望使用fmt格式化spdlog输出,而不必自己指定转义序列。为此,您可以使用fmt::format函数并将其输出用作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;
}

最新更新