Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write to the console window for debugging?

People also ask

How do I run a debug console?

To open the Debug Console, use the Debug Console action at the top of the Debug pane or use the View: Debug Console command (Ctrl+Shift+Y). Expressions are evaluated after you press Enter and the Debug Console REPL shows suggestions as you type.

How do I write to the Output window in Visual Studio?

You can write run-time messages to the Output window using the Debug class or the Trace class, which are part of the System. Diagnostics class library. Use the Debug class if you only want output in the Debug version of your program. Use the Trace class if you want output in both the Debug and Release versions.

How do I show a debug window?

You can open most debugger windows while you are debugging your program. To see a list of debugger windows, set a breakpoint and start debugging. When you hit the breakpoint and execution stops, click Debug > Windows.

How do I step into debugging?

To start your app with the debugger attached, press F11 (Debug > Step Into). F11 is the Step Into command and advances the app execution one statement at a time. When you start the app with F11, the debugger breaks on the first statement that gets executed.


The simplest way is to compile as a console application, but put the normal application framework code back in the dpr.

program Project2;

{$APPTYPE CONSOLE}

uses
  Forms,
  SysUtils,
  Unit1 in 'Unit1.pas' {Form1};

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  writeln('Hello, World!');
  Application.Run;
end.

A slightly more complex way is to use the Windows API AllocConsole call:

program Project2;

uses
  Forms,
  SysUtils,
  Windows,
  Unit1 in 'Unit1.pas' {Form1};

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  AllocConsole;
  writeln('Hello, World!');
  Application.Run;
end.

This method has the (usually) disadvantage of creating a new console if you are calling from the command line. From memory getting redirection to work requires some more code as well. The advantage is that you can decide to allocate the console at run-time, rather than compile time.


In Windows, the simplest way to output debug information is to use OutputDebugString() and then use an application able to receive that output. The event viewer in the Delphi IDE itself is able to receive that input, or you can use the DebugView application from SysInternals to get output on a system that hasn't the IDE installed. AFAIK, GExperts has a similar tool too. That's because a GUI application has not by default a console where to write output, otherwise you have to create one (see Gerry's answer).

One of OutputDebugString()'s advantages is that the application will work without problems even if a call slips into a release build (or is if left there intentionally), but be careful then to not output sensitive information, because they can be read using one of the tools above.

You could also create an ad-hoc form (that is, with a memo control) and route output there.

There are also advanced logging facilities like SmartInspect, CodeSite and others.


Delphi has got an option for this, check "Generate console application" in the linker options for the project. Standard I/O will be directed to a console window which will accompany your GUI application. Then you can use Writeln etc. as you normally would.

Read Output (or Input) from the docs:

Delphi programs have a standard output file if they are linked as console applications.


If you wrote the console application, you can try OutputDebugString function in the console application (I didn't try).

Or you can capture the output of the console application like in Capture the output from a DOS (command/console) Window .

Also, you can check Console Application Runner Classes. I use these classes. I think they are great.