Disable writeinfo using messageboxes if no console in windows?

I had been using my own debug unit for outputting debug information to see in the console. I am trying to reduce dependencies so trying to replace that with cge’s WriteInfo routine. It works great when console mode is on. I am in windows, and I have to alloconsole to launch a console and in fpc I also have to call

IsConsole := True; 

SysInitStdIO;       

And then WriteInfo works great. But if I turn off debug mode and don’t turn on the console, then the output goes to a messagebox, which is NOT wanted and stops everything. In this case I want the message to just go to file if configured or be ignored.

How can disable writeinfo using messageboxes if no console in windows?

Note: The usual way to switch between showing / not showing console on Windows is switching {$apptype GUI} and {$apptype CONSOLE} in the program file. E.g. even {$ifdef DEBUG} {$apptype CONSOLE} {$else} {$apptype GUI} {$endif}.

But of course your approach should work too, so I understand you have always {$apptype GUI} and then use WinAPI to allocate the console at runtime, based on any condition.

We don’t have WriteInfo in CGE, but I guess you mean InfoWrite :slight_smile:

Note: InfoWrite was not really meant to be customizable, it’s just a trivial utility. I would not really advise to use this in your applications, unless you explicitly need what is documented there (“messagebox when GUI Windows, or output to stdout otherwise”, castle-engine/src/base/castleutils_read_write.inc at d0a1724696268fbdc72d3d665db9b0a3472f7e0d · castle-engine/castle-engine · GitHub ).

It does this:

procedure InfoWrite(const s: string);
begin
  {$ifdef MSWINDOWS} if not IsConsole then WindowsInfoBox(s) else {$endif}
  Writeln(s);
end;

So, one answer: If you always want to send to console, then just use Writeln, that’s literally everything that happens when IsConsole = true (or non-Windows system, when the stdout is always available) :slight_smile:

Second answer, recommended: Use our log, Logging | Manual | Castle Game Engine . This was made to be customizable, comfortable, it’s visible in the editor console log too, it can be send to console, file, can be captured by custom callbacks etc. See CastleLog unit. In short, you just call WritelnLog.

1 Like

I conflated InfoWrite and the logging. I want something that writes to console if open. And logs if configured to log. I guess WritelnLog is what I want. Thanks.

So now I am switching to use CastleLog. You say “it is visible in the editor console too”… In Lazarus, how do I see this? I don’t see anything in the event log. Is that an option you enable?

Now I realize you meant the castle editor. Nevermind.