2

Everytime, std::cout is used from the main function or other functions, I want to copy the contents of a logfile to a Fltk GUI Window.

I've implemented a function which does the copying part. I want to know if its possible to make this function run every time std::cout or maybe, std::endl is executed in the code.

How can I implement this?

EDIT: For more clarification,

The function which needs to run every time is:

void printProgress(const std::string& strProgress)
{
    std::ostringstream strResultTextStream;
    strResultTextStream << m_pLogOutput->value();
    strResultTextStream << strProgress;
    m_pLogOutput->value(strResultTextStream.str().c_str());
}

Adding data to the output window (fl_multiline_output) requires the 4 lines of code above. Right now I have redirected the standard output messages to a log file. I wanted to run this function everytime to copy the contents of the logfile to m_pLogOutput(the Output window)

Loki
  • 35
  • 6
  • There is a difference between redirect/duplicate the "flux" of `std::cout` and have a watcher on it. – Jarod42 Nov 21 '19 at 09:52
  • 4
    how about a wrapper? – The Philomath Nov 21 '19 at 09:52
  • 1
    Maybe you can use own function, which will do the copying part and then will use `std::cout`? – Raffallo Nov 21 '19 at 09:52
  • Sounds like it may be easier to do outside of the project. Can you run your program under an equivalent of `tee`? – Yksisarvinen Nov 21 '19 at 09:54
  • Hardcoding the use of `std::cout` is bad practice anyhow. If you pass a output stream whereever you want to output something it is trivial to replace `std::cout` with some stream that does what you want. – 463035818_is_not_an_ai Nov 21 '19 at 09:55
  • 5
    If I wanted to redirect cout output from inside the program, I'd use rdbuf to set cout streambuf to a streambuf which does what I want. – AProgrammer Nov 21 '19 at 09:59
  • Maybe have another thread that does blocking read from the fd associated with std::cout ? – Darren Smith Nov 21 '19 at 10:06
  • @ formerlyknownas_463035818 @AProgrammer The thing is, the output box (Fl_Multiline_Output) where I want to print the standard output messages requires the copying of its contents to a temporary stream, appending the new standard output message, then finally copying it back again. – Loki Nov 21 '19 at 10:12
  • 1
    @Loki - A tailored `streambuf` can do that. – Peter Nov 21 '19 at 11:13
  • I think that this example will help you : [https://stackoverflow.com/questions/7129037/how-do-i-change-a-c-output-stream-to-refer-to-cout](https://stackoverflow.com/questions/7129037/how-do-i-change-a-c-output-stream-to-refer-to-cout) – user2020 Nov 21 '19 at 12:10
  • @Peter How can I modify streambuf to run the three lines of code in the function above everytime something is placed in the cout buffer? – Loki Nov 21 '19 at 14:48

1 Answers1

-5

I think you could use #define to your advantage:

#define cout f(parameter),cout

where f() is your function. Change cout with std::cout if you want, I don't think that will provoke errors.

Palc
  • 1
  • 1
  • 4
    this will exapnd to eg `f(),cout << "foo"` , ie it would call `f()` with no parameters. How is this supposed to help? Also replacing `cout` with a macro is calling for trouble. "I don't think that will provoke errors." I think it does – 463035818_is_not_an_ai Nov 21 '19 at 10:51
  • I added a parameter, are you happy now? His only real options is to replace `cout` with a macro or use `Ctrl+R` to replace `cout` with `f(parameter),cout`. He asked for the function to run **every** time `cout` is used, so a macro looks fine for me. – Palc Nov 22 '19 at 15:50
  • consider what happens when I declare a function `void foo(std::ostream& x = cout);` – 463035818_is_not_an_ai Nov 23 '19 at 09:09