www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Windows / redirect STDERR to see assert messages

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
When developing Windows GUI applications I use:

      // detach from console and attach to a new one, works for x86 and x86_64
      FreeConsole();
      AllocConsole();

      freopen("CONIN$", "r", stdin);
      freopen("CONOUT$", "w", stdout);
      freopen("CONOUT$", "w", stderr);

so that the GUI app opens a console for writeln() output etc. I assumed 
this should work for assert() messages as well. But it seems it 
doesn't. If an assert fails, I don't see any output. Is assert using 
something else? What's wrong about this approach?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
May 12 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 12 May 2019 at 13:39:15 UTC, Robert M. Münch wrote:
 If an assert fails, I don't see any output. Is assert using 
 something else? What's wrong about this approach?
You might need to catch all the throwable exceptions and print your way instead...
May 12 2019
parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-05-12 22:46:37 +0000, Adam D. Ruppe said:

 You might need to catch all the throwable exceptions and print your way 
 instead...
Adam, thanks, that was the missing link. I thought that assert() is dumb hard exit with a short message and not throwing a full exception. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
May 14 2019
prev sibling next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Sunday, 12 May 2019 at 13:39:15 UTC, Robert M. Münch wrote:
 When developing Windows GUI applications I use:

      // detach from console and attach to a new one, works for 
 x86 and x86_64
      FreeConsole();
      AllocConsole();

      freopen("CONIN$", "r", stdin);
      freopen("CONOUT$", "w", stdout);
      freopen("CONOUT$", "w", stderr);

 so that the GUI app opens a console for writeln() output etc. I 
 assumed this should work for assert() messages as well. But it 
 seems it doesn't. If an assert fails, I don't see any output. 
 Is assert using something else? What's wrong about this 
 approach?
Are you sure the last call to freopen doesn't return NULL? You are opening the same file twice and I'm not sure that works. Test with `stderr.writeln("test")`. Bastiaan.
May 14 2019
prev sibling parent Kagamin <spam here.lot> writes:
Assert failure uses system IO API, try 
https://docs.microsoft.com/en-us/windows/console/setstdhandle
May 14 2019