如何在调试模式下冲洗FMT输出?

发布于 2025-02-11 04:22:29 字数 1544 浏览 1 评论 0 原文

我正在尝试 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:// https:// thomas。 trocha.com/blog/qt-cereator-make-stdout-work-work-in-application-actput-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", "??");
}

I'm experimenting with fmt and I do get output from code below

#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", "??");
}

However it seems to happen after program exits. If I step over each line in debug mode there is no output?

Perhaps it should flush?

I tried fflush(stdout); unsuccessfuly.

The following seems to help. With it I get the prints with sleep in the middle. However still no print during debug while stepping in each line.

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", "??");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2025-02-18 04:22:29

您可以使用 fmt :: print ,可以使用 fmt ::格式并将其发送到 std :: Cout 并使用 std ::齐平

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

Instead of using fmt::print, you could use fmt::format and send that to std::cout and use 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;
}
情徒 2025-02-18 04:22:29

您可以使用 stderr 立即打印内容。

#include <fmt/core.h>
#include <cstdio> // ::stderr

int main() {
  fmt::print(stderr, "Print right away");
}

描述

FMT使用缓冲输出。它带有其自己的缓冲区类
它输出到 stdout ,我不确定 qdebug ,但是从名称中,它不使用 stdout

确保检查“ 在终端中运行”编译器资源管理器中的复选框。
无论如何:您可以通过将 std :: file*作为第一个参数来更改 fmt :: print 的输出流。

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

Compiler Explorer
我不知道手动冲洗FMT打印缓冲区的任何方法。我正在调查。

我研究了它(2024-03-26编辑)

,所以……没有办法(我知道)将 stdout stderr
同步。
然而,

通过std :: cerr输出到STDERR,从std :: cout上输出输出输出,而通过std :: clog却没有输出到stderr。
(cf: cppReference

fmt不会决定是否可以缓冲输出,它只是将内容转发到libc。

您可以关闭缓冲:

setbuf(stdout, nullptr);
// Strictly equivalent to: setvbuf(stdout, nullptr, _IONBF, 0);

paraphrasing kiwixz

You can use stderr to print the content immediately.

#include <fmt/core.h>
#include <cstdio> // ::stderr

int main() {
  fmt::print(stderr, "Print right away");
}

Description

fmt uses buffered output. It comes with its own buffer class.
It outputs to stdout, I'm not sure about qDebug but from the name it doesn't use stdout.

Make sure to check the "Run in terminal" check box in Compiler Explorer.
In any case: you can change the output stream used by fmt::print by passing a std::FILE* as the first argument.

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

Detailed example on compiler explorer
I'm not aware of any way to flush the fmt print buffer manually. I'm looking into it.

I looked into it (2024-03-26 edit)

So… There's no way (I know off) to sync stdout with stderr.
However,

Output to stderr via std::cerr flushes out the pending output on std::cout, while output to stderr via std::clog does not.
(cf: cppreference)

Finally,

fmt does not take the decision to buffer or not the output, it just forwards the content to the libc.

You can turn off buffering with:

setbuf(stdout, nullptr);
// Strictly equivalent to: setvbuf(stdout, nullptr, _IONBF, 0);

Paraphrasing kiwixz's answer on an issue about this on the fmt repo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文