3

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", "??");
}
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • Where are you viewing your program output? Is it possible that that is buffering the output? – Alan Birtles Jun 27 '22 at 06:38
  • What happens if you enclose those statements into a local scope block? – heap underrun Jun 27 '22 at 06:39
  • @AlanBirtles QtCreator application output panel. What's buffering output? – KcFnMi Jun 27 '22 at 06:43
  • Try just putting a sleep statement between the prints and run the program normally in a terminal and see if they're printed correctly – Alan Birtles Jun 27 '22 at 06:48
  • With the sleep in the middle it still prints only at the end – KcFnMi Jun 27 '22 at 07:02
  • Enclose those statements into a local scope block does not change anything. – KcFnMi Jun 27 '22 at 07:42
  • Probably it's platform specific question. Could you elaborate on your OS, distro and compiler, ide and how did you compile your first mre (the one without Qt)? – Louis Go Jul 04 '22 at 06:52
  • I use gcc9.3, gdb9.2, fmt 8.1.1 and `fflush(stdout)` on ubuntu20 and it just works. So the problem might be QtCreator itself. – Louis Go Jul 04 '22 at 08:50
  • I'm on Windows 10 64bits with ms compiler (16.11.31702.278 and cdb 10.0.17763.132, vs 2019) but I use QtCreator, with fmt-7.1.3 installed from vcpkg. To compile it I hit build on QtCreator. Please let me know if I can provide more info. – KcFnMi Jul 05 '22 at 03:00
  • It'd be better if you include the QtCreator and Qt version. – Louis Go Jul 05 '22 at 06:45
  • I'll suggest try to use visual studio and its debugger to check if it's working. – Louis Go Jul 05 '22 at 07:01

2 Answers2

2

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.
Did you remember to check the "Run in terminal" check box?
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.

viraltaco_
  • 814
  • 5
  • 14
2

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;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108