Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dos command OnNewLine is not getting correct output. #18

Open
birkangit opened this issue Mar 15, 2022 · 1 comment
Open

Dos command OnNewLine is not getting correct output. #18

birkangit opened this issue Mar 15, 2022 · 1 comment

Comments

@birkangit
Copy link

I made a sample console application which writes some console messages with WriteLn and with LoggerPro library

There is missing lines on output strings when ecexuted with DosCommand.

Output is ;

Console message 1
Console message 2
Console message 3
Console message 4
Console message 5
Console message 6
Console message 7
Console message 8
Console message 9
Console message 10
Console message 11
Console message 12
Console message 13
Console message 14
Console message 15
Console message 16
Console message 17
Console message 18
Console message 19
Console message 20
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 1 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 1 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 1 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 1 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 2 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 2 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 2 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 2 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 3 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 3 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 3 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 3 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 4 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 4 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 4 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 4 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 5 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 5 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 5 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 5 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 6 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 6 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 6 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 6 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 7 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 7 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 7 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 7 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 8 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 8 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 8 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 8 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 9 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 9 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 9 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 9 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][DEBUG     ] Log message 10 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][INFO      ] Log message 10 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][WARNING   ] Log message 10 [Test]
2022-03-15 12:24:07:843  [TID 17056   ][ERROR     ] Log message 10 [Test]

But OnNewLine event getting only,

Console message 1
Console message 2
Console message 3
Console message 4
Console message 5
Console message 6
Console message 7
Console message 8
Console message 9
Console message 10
2022-03-15 12:33:47:813  [TID 29524   ][DEBUG     ] Log message 1 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][INFO      ] Log message 1 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][WARNING   ] Log message 1 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][ERROR     ] Log message 1 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][DEBUG     ] Log message 2 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][INFO      ] Log message 2 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][WARNING   ] Log message 2 [Test]
2022-03-15 12:33:47:813  [TID 29524   ][ERROR     ] Log mes20 [Test]

Test projects is on the attachment.

TestDosCommand.zip
ConsoleOutput
OutputWithDosCommand

Console App code ;

program DosCommandTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  LoggerPro,
  LoggerPro.ConsoleAppender;

const c_defTAG = 'Test';
var
  Log: ILogWriter;
begin
  try
    for var i :=  1 to 10 do
      Writeln('Console message ' + i.ToString);

    Log := BuildLogWriter([TLoggerProConsoleAppender.Create]);
    for var i :=  1 to 10 do
    begin
      Log.Debug('Log message ' + i.ToString(), c_defTAG);
      Log.Info('Log message ' + i.ToString(), c_defTAG);
      Log.Warn('Log message ' + i.ToString(), c_defTAG);
      Log.Error('Log message ' + i.ToString(), c_defTAG);
    end;
    Sleep(20000);
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Test App code;

unit ConsoleLogMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DosCommand, Vcl.StdCtrls;

type
  TForm8 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    DosCommand1: TDosCommand;
    procedure Button1Click(Sender: TObject);
    procedure DosCommand1NewLine(ASender: TObject; const ANewLine: string;
      AOutputType: TOutputType);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.Button1Click(Sender: TObject);
begin
  DosCommand1.CommandLine := ExtractFilePath(Application.ExeName) + 'DoscommandTest.exe';
  DosCommand1.CurrentDir := ExtractFilePath(Application.ExeName);
  Memo1.Lines.Clear;
  DosCommand1.Execute;
end;

procedure TForm8.DosCommand1NewLine(ASender: TObject; const ANewLine: string;
  AOutputType: TOutputType);
begin
  if AOutputType = otEntireLine then
  begin
    Memo1.Lines.Add(ANewLine);
  end;
end;

end.
@romankassebaum
Copy link
Contributor

The TDosCommand component runs your application within a thread. It calls the main thread with Queue. If the main thread has time, it iterates the queue.

I just tested your example, without the LoggerPro component, and everything is fine.

In a real application you can consider to call CheckSynchronize within your main thread if it is idle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants