我正在尝试 fmt 并且我确实从下面的代码中获得了输出
#include <fmt/color.h>
int main() {
fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
"Hello, {}!\n", "world");
fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
fmt::emphasis::underline, "Hello, {}!\n", "???");
fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
"Hello, {}!\n", "??");
}
但是它似乎是在程序退出后发生的。如果我在调试模式下单步执行每一行,则没有输出?
也许它应该冲洗?
我尝试了
fflush(stdout);
但没有成功。
以下内容似乎有帮助。有了它,我就得到了中间有睡眠的指纹。但是,在调试期间单步执行每一行时仍然没有打印。
setbuf(stdout, NULL);
https://thomas.trocha.com/blog/qt-creator--make-stdout-work-in-application-output-view/
int main() {
setbuf(stdout, NULL);
fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
"Hello, {}!\n", "world");
qDebug() << "1";
QThread::msleep(2000);
qDebug() << "2";
fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
fmt::emphasis::underline, "Hello, {}!\n", "???");
QThread::msleep(2000);
fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
"Hello, {}!\n", "??");
}
您可以使用
fmt::print
并将其发送到 fmt::format
并使用 std::cout
,而不是使用 std::flush
:
#include <fmt/color.h>
#include <iostream>
int main() {
std::cout << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold,
"Hello, {}!\n", "world") << std::flush;
std::cout << fmt::format(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
fmt::emphasis::underline, "Hello, {}!\n", "???") << std::flush;
std::cout << fmt::format(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
"Hello, {}!\n", "??") << std::flush;
}
您可以使用
stderr
立即打印内容。
#include <fmt/core.h>
#include <cstdio> // ::stderr
int main() {
fmt::print(stderr, "Print right away");
}
fmt 使用缓冲输出。它带有自己的缓冲区类。
它输出到
stdout
,我不确定qDebug
,但从名称来看它不使用stdout
。
确保选中编译器资源管理器中的“在终端中运行”复选框。 在任何情况下:您可以通过传递
fmt::print
作为第一个参数来更改 std::FILE*
使用的输出流。
fmt::print(stderr, "This is printed first");
std::cerr << "This is printed second.";
std::cout << "When this is printed is implementation defined.";
std::clog << "Unlike std::cerr and std::cin, std::clog will not flush std::cout.";
关于编译器资源管理器的详细示例
我不知道有什么方法可以手动刷新 fmt 打印缓冲区。我正在研究。
所以……没有办法(我知道)将
stdout
与 stderr
同步。通过 std::cerr 输出到 stderr 会清除 std::cout 上的待处理输出,而通过 std::clog 输出到 stderr 则不会。 (参见:cppreference)
此外:
您可以通过以下方式关闭缓冲:
setbuf(stdout, nullptr);
// Strictly equivalent to: setvbuf(stdout, nullptr, _IONBF, 0);
解释 kiwixz 在 github 问题上对此的回答。