Author Topic: Redirect std::cout  (Read 11050 times)

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Redirect std::cout
« on: December 05, 2008, 04:45:45 pm »
Hello,

have anybody a suggestion for me how I redirect the std::cout to the IDE´s build log window???

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Redirect std::cout
« Reply #1 on: December 05, 2008, 04:48:49 pm »
The stdout and stderr streams from the compiler tools are already correctly handled in Code::Blocks. So do you mean from your own project? If so, why the build log?

The short answer is that Code::Blocks isn't designed for that usage scenario.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #2 on: December 05, 2008, 04:54:02 pm »
hmm ..

I have a gui app and would like see my variables how they changes during running my app.
I  don´t want an extra console for those outputs I would like see this in the IDE ....

I know that I am able to watch my variables in my debugging session, but I don´t want debugging...

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Redirect std::cout
« Reply #3 on: December 05, 2008, 04:58:09 pm »
Code::Blocks isn't designed for that usage scenario.

Translation:
You can't do that.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #4 on: December 05, 2008, 05:01:00 pm »
Could this be a good solution...

I redirect the std::cout with this approach
Code
 streambuf *psbuf;
  ofstream filestr;
  filestr.open ("test.txt");

  psbuf = filestr.rdbuf();
  cout.rdbuf(psbuf);

  cout << "This is written to the file";

  filestr.close();


and open the file in a tab in the IDE during the execution....or??

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #5 on: December 05, 2008, 05:07:33 pm »
hmmm it is very nice that you give me a

TRANSLATION my friend.

I am a person who discuss or ask every possibility which grows up my horizon in using C::B.

Yes I am a newbie and I have search and read many thing about C::B. (very good IDE I fall down on my knees)--> Translation (   I intended it as a compliment)

But your explanation with your Translation have no good aftertaste for me in my oppinion.....

Offline Grom

  • Almost regular
  • **
  • Posts: 206
Re: Redirect std::cout
« Reply #6 on: December 05, 2008, 06:39:24 pm »
Just take a look here
http://bytes.com/groups/c/132096-redirecting-cout-dialog-window
You can derive a class from streambuf and direct it to a wxMemo class.
 And try that http://osdir.com/ml/lib.wxwindows.general/2004-01/msg01465.html in wx don't know which way is the best, but please give us know about the best way. In fact it is going to be grate to have such rederection component in wxSmith. I did that in some ugly way, such that cout in black and cerr was in red.
Int might be better to try wxStreamToTextRedirector.
« Last Edit: December 05, 2008, 06:46:46 pm by Grom »
gcc+winXP+suse.

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #7 on: December 05, 2008, 07:40:49 pm »
Thank you very much to your suggestions I will give it a try asap and a feddback back to here....

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Redirect std::cout
« Reply #8 on: December 05, 2008, 08:25:14 pm »
a bit of a hack, but OP *might* be able to get his desired redirection in CB using my ShellExtensions plugin. A feature of the plugin allows you to run custom commands that redirect output to a notepage in a CB dockable window.

1. (assuming win32 and recent CB nightly) download a binary for the plugin from: http://developer.berlios.de/project/showfiles.php?group_id=7745&release_id=15437

2. follow installation instructions in the readme

3. then goto Settings -> Environment -> ShellExtensions -> click New

4. then:
* type a name for the command
* the command line (e.g. "mymainprog.exe")
* mode: Windowed console notepage
* menu location: specifies the menu item name e.g. "MyProgram"

5. click OK.

6. The program will then be available from the Extensions menu

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #9 on: December 05, 2008, 10:38:04 pm »
Ohh very good,

so I have also a way to do it.
here is my sugegestion.

In my code everywhere where I wish to see an output I put this line:

Code
std::cout << "Initialiaze Function init() " <<  std::endl;

or

Code
std::cout << "Hello now my database is connented" <<  std::endl;
etc.

after all you build your Project. Then create a user-defined-tool. ;o)
Did you know what is coming ;o)).

Yes take your executable from your builded application and click launch tool hidden with standard output redirected.

after running you see your app and you can click around and every time it occures a std::cout in your code

you see it in your IDE C::B under  Logs & other in the tab Code::blocks.

Any comment??????





Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #10 on: December 05, 2008, 10:44:13 pm »
Dear mr. TDragon

Quote
Translation:
You can't do that.

really??

I can do it.... ;o)))

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Redirect std::cout
« Reply #11 on: December 06, 2008, 12:21:34 am »
Sure. You could also write a plugin which installs a logger that listens on a pipe... but that's not the point. What TDragon was saying is that Code::Blocks wasn't made for that kind of stuff.  8)
The sensible way of watching variables and controlling program flow is to use the debugger, because that's what it's for, and it's something that already works, and it works well.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #12 on: December 06, 2008, 12:22:36 am »
Dear dmoore,

I try out your suggestion. It works fine thanks very much...

Offline LordCB

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: Redirect std::cout
« Reply #13 on: December 06, 2008, 07:33:42 pm »
Dear dmoore,

where can I get the sources of your plugin.

Offline CuteAlien

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: Redirect std::cout
« Reply #14 on: December 07, 2008, 05:38:02 am »
I
The sensible way of watching variables and controlling program flow is to use the debugger, because that's what it's for, and it's something that already works, and it works well.
There are many situations where cerr works better than a debugger. If you have time-outs, if you have to print lots of data or realtime data, threads are a pain and not to forget the rather regular gdb crashes with c++.

But anyway - my solution for that is rather trivial. If I need cerr output, then I start the application from console :-) Having both - debugger + console would certainly be nicer, but so far I got by without it. In Windows you can also start your application as console applications. You have to replace WinMain by main as entry point into your application then you get a console output.