Recent

Author Topic: [SOLVED] console host program  (Read 3872 times)

KemBill

  • Jr. Member
  • **
  • Posts: 74
[SOLVED] console host program
« on: December 05, 2017, 04:48:57 pm »
Is it possible (or is there a trick) to attach a console host program when you are debuging a crt program ?

I  lose program output as it ends and the window close and I can't compare with previous runs.

I don't know if I am clear, I'm not talking about allocconsole, In short: I want to always debug my program in the same instance of cmd.exe
« Last Edit: December 06, 2017, 10:43:51 am by KemBill »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: console host program
« Reply #1 on: December 05, 2017, 05:14:52 pm »
windows or *nix?

Windows: compile with -WC  (or in project opts / Target: disable "GUI app")

*nix:
(linux only) The IDE has: Menu > View > Debug windows > Console Output. But that does not have any support for escape sequences or other terminal emulations.

*nix:
open a terminal (outside lazarus). Find which pty it uses (IIRC there is a command pty). Then under Menu > Tools > Options >> Debugger: In the property grid you can enter the pty (there should be a property refering pty, not sure of the exact name).
This redirects all input/output to that pty
This affects all projects, so you need to undo this later

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: console host program
« Reply #2 on: December 05, 2017, 05:16:22 pm »
Add readln to the very end of your program:
Code: Pascal  [Select][+][-]
  1. program stopwhenIamReadyAndDontQuitImmediatelyBecauseIWantToExamineTheOutput;
  2. {$ifdef windows}{$apptype console}{$endif}
  3. uses crt;
  4. begin
  5. // your code
  6. Readln;
  7. end.

Console stays open until you press a key....
« Last Edit: December 05, 2017, 05:18:07 pm by Thaddy »
Specialize a type, not a var.

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: console host program
« Reply #3 on: December 05, 2017, 08:38:32 pm »
windows or *nix?
Windows: compile with -WC  (or in project opts / Target: disable "GUI app")

Sorry Martin_fr, I don't understand what you are meaning, my program is already a No GUI app (by the way is it -WG  you were typing ?)

Add readln to the very end of your program:
I'm under windows  :D, I use the readln trick, but you can't run another instance of your program since the last one is not ended.

I was thinking about redirecting stdout and found this http://lists.freepascal.org/fpc-pascal/2010-July/026163.html, I never knew it was possible

Code: Pascal  [Select][+][-]
  1. Program test;
  2.  
  3. Var
  4.   f: textfile;
  5. begin
  6.  AssignFile(f, 'somefile.txt');
  7.  Rewrite(f);
  8.  output := f;
  9.  [...]
  10.  write('hello');
  11.  [...]
  12. end;
  13.  




Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: console host program
« Reply #4 on: December 05, 2017, 09:33:52 pm »

Add readln to the very end of your program:
I'm under windows  :D, I use the readln trick, but you can't run another instance of your program since the last one is not ended.
That's nonsense.... Windows allows you to run as many instances as memory (all resources) allows.
« Last Edit: December 05, 2017, 09:41:23 pm by Thaddy »
Specialize a type, not a var.

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: console host program
« Reply #5 on: December 05, 2017, 09:47:41 pm »
That's nonsense.... Windows allows you to run as many instances as memory (all resources) allows.

It seem I was not so clear... In the IDE, you can't run more than once the progrma you are debugginng ?
« Last Edit: December 05, 2017, 09:58:53 pm by KemBill »

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: console host program
« Reply #6 on: December 05, 2017, 10:23:47 pm »
Do you have ANY example of ANY compiled language where the IDE can debug multiple instances? (Like.... NONE... >:D )

This should be possible but is really hard to do because it also requires multiple instances of the debugger.... and that is the least of the problems.....

E.g. script kiddy stuff can be handled like this (PHP, JavaScript, -grin   ;D - Java - LUA etc) but not languages that directly compile to native code.
« Last Edit: December 05, 2017, 10:29:32 pm by Thaddy »
Specialize a type, not a var.

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: console host program
« Reply #7 on: December 05, 2017, 10:35:45 pm »
So
Do you have ANY example of ANY compiled language where the IDE can debug multiple instances? (Like.... NONE... >:D )

This should be possible but is really hard to do because it also requires multiple instances of the debugger.... and that is the least of the problems.....

E.g. script kiddy stuff can be handled like this (PHP, JavaScript, -grin   ;D - Java - LUA etc) but not languages that directly compile to native code.

Stop mixing stuff

So you think, a console program does not need cmd.exe ?

just run this
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. begin
  13.   readln;
  14. end.                
  15.  

and now look in your task manager, do you see "console window host" ? all I wanted to do is to run my program in the same  "console window host"... no multiple IDE
« Last Edit: December 05, 2017, 10:38:20 pm by KemBill »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: console host program
« Reply #8 on: December 06, 2017, 12:13:23 am »
First I misunderstood you. I thought you had a problem to get any console at all during debug.

Anyway...

If you run them in Lazarus, there will always be a new console.
You can "run without debugging" (from the run menu). This way you can launch more than one instance.
But it may not help you, because:
1) you can not debug
2) the running instance will lock the exe file. So you can not make changes, because compile will not be able to overwrite the exe.

The best you can do is to copy the data from the console before you close it.

Or you can redirect stdout to a file (I havent tested if that works under the debugger.
Try "Run Parameters":
   > file.txt

------------------
On Windows the console is created by the OS. So there is little control. Afaik there are only 2 options.
1) new console
2) use console of parent app (that is what happens if you run your app in a cmd.exe window)

You can not use 2 with the debugger, because the parent app is gdb. And that is started without console. And it must be, because the IDE must read the stdout of gdb.
So there is no way.

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: console host program
« Reply #9 on: December 06, 2017, 10:34:40 am »
Thanks for the exaplanation, anyway redirecting stdout to a file is from far a better solution. this allows to diff output between runs, just do not forget to change the file name. (btw diff always beat me to find differences  :D)

so here's my try to do this, with conditional compilation

Code: Pascal  [Select][+][-]
  1. program console;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses {$IFDEF UNIX} {$IFDEF UseCThreads}
  6.   cthreads, {$ENDIF} {$ENDIF}
  7.   Classes,
  8.   SysUtils { you can add units after this };
  9.  
  10. {$IFDEF DEBUG}
  11.   procedure setuplog;
  12.   var
  13.     i: integer;
  14.     str: string;
  15.     f: textfile;
  16.   begin
  17.     i := 1;
  18.     str := 'console';
  19.     while fileexists(str + IntToStr(i) + '.log') do
  20.       Inc(i);
  21.     str := str + IntToStr(i) + '.log';
  22.     AssignFile(f, str);
  23.     Rewrite(f);
  24.     output := f;
  25.   end;
  26. {$ENDIF}
  27.  
  28. begin
  29.   {$IFDEF DEBUG}
  30.   setuplog;
  31.   {$ENDIF}
  32.   writeln('hello');
  33. end.
  34.  

so all write & writeln are redirected to a file without the need to use a file the common way writeln(f, ...), flush(f), closefile(f)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: [SOLVED] console host program
« Reply #10 on: December 06, 2017, 11:11:12 am »
i am using a logger in a thread. not to bother main thread.
have a call back in main unit that writes to this logger and catches messages from each unit.
Lazarus 2.0.2 64b on Debian LXDE 10

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: [SOLVED] console host program
« Reply #11 on: December 06, 2017, 12:15:14 pm »
i am using a logger in a thread. not to bother main thread.
have a call back in main unit that writes to this logger and catches messages from each unit.

I understand, but here, this is not the purpose, look carefully the code i pasted

When you define DEBUG during compilation time, all the ouput is redirected to a file (for test), so if you do not define DEBUG all the ouput is sent to the screen without any change (for release)
« Last Edit: December 06, 2017, 12:19:08 pm by KemBill »

 

TinyPortal © 2005-2018