#1 2014-10-03 18:41:21

cheemeng
Member
From: Malaysia
Registered: 2011-08-09
Posts: 61

TSynLogFile.GetEventText returns an Empty String?

Hi Arnaud,

If I read back the line that has been added via AddInMemoryLine, I will get an empty string. EventLevel will return the correct event level though? The line sent to AddInMemoryLine is retrieved via a TOnTextWriterEcho event.

Some code snippet:

function TTestLogger.LogEcho(Sender: TTextWriter; Level: TSynLogInfo;
  const Text: RawUTF8): boolean;
var
  P: PUTF8Char;
  Line: RawUTF8;
begin
      repeat
        Line := GetNextLine(P, P);
        if Length(Line) < 24 then
          Continue;
        fActiveLog.AddInMemoryLine(Line); 
      until P = nil;
end;

// and when I do this:

for index := fActiveLog.Count - 1 downto 0 do
  WriteLn('Level: ', EventLevelCaption[fActiveLog.EventLevel[index]], ' Text: ', fActiveLog.EventText[index]);

// I get an empty string for Event Text, but the Event Level is correct

Any ideas? Or did I do something wrongly?

Offline

#2 2014-10-04 10:10:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: TSynLogFile.GetEventText returns an Empty String?

Perhaps your Text does not have the expected format.
It should have the same layout as the default logging.

Does remote LogView work on your side?
It does use AddInMemoryLine in real time.

Offline

#3 2014-10-04 13:34:25

cheemeng
Member
From: Malaysia
Registered: 2011-08-09
Posts: 61

Re: TSynLogFile.GetEventText returns an Empty String?

Yup, weirdly LogView works. However, in my sample code, it's exactly the same thing, just without the remote connection.

The message was added via TSynLog.Add.Log(slxxx, 'xxx'); so I assume the format should be correct? When debugging, I see that it returns an empty text because strlen always return 0. However, when I restart the test app (it first works by loading an existing log file, before doing the in-memory append for subsequent logs), the same looping code above works, so I believe the format is correct.

I had this issue much earlier and managed to resolve it by appending a #13 to the Line I got from LogEcho, however that has stopped working since a few patches ago... no idea why yet though.

Offline

#4 2014-10-04 14:09:07

cheemeng
Member
From: Malaysia
Registered: 2011-08-09
Posts: 61

Re: TSynLogFile.GetEventText returns an Empty String?

Here's a sample console code:

program TestLogger;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SynCommons,
  System.SysUtils;

type
  TTestLogger = class(TSynLog)
    procedure ComputeFileName; override;
  end;

  TSynLogFileEcho = class(TSynLogFile)
  public
    function LogEcho(Sender: TTextWriter; Level: TSynLogInfo; const Text: RawUTF8): boolean;
  end;

var
  LogFile: TSynLogFileEcho;
  FEventCaption: array[TSynLogInfo] of string;
  E: TSynLogInfo;

{ TTestLogger }

procedure TTestLogger.ComputeFileName;
begin
  inherited;
  fFileName := 'abc.log';
end;

{ TSynLogFileEcho }

function TSynLogFileEcho.LogEcho(Sender: TTextWriter; Level: TSynLogInfo;
  const Text: RawUTF8): boolean;
var
  P: PUTF8Char;
  Line: RawUTF8;
begin
  P := Pointer(Text);
  repeat
    Line := GetNextLine(P, P);
    if Length(Line) < 24 then
      Continue;
    Self.AddInMemoryLine(Line);
  until P = nil;
  Result := True;
end;

procedure ShowLogContent;
var
  index: Integer;
begin
  for index := LogFile.Count - 1 downto 0 do
    WriteLn('Level: ', FEventCaption[LogFile.EventLevel[index]], ' Text: ', LogFile.EventText[index]);
end;

procedure WaitForEnter(const Msg: string);
begin
  WriteLn(Msg);
  Write('Press ENTER to continue...');
  ReadLn;
end;

begin
  try
    with TTestLogger.Family do
    begin
      Level := LOG_VERBOSE;
      FileExistsAction := acAppend;
    end;

    for E := succ(sllNone) to high(E) do
      FEventCaption[E] := GetCaptionFromEnum(TypeInfo(TSynLogInfo),ord(E));

    if FileExists('abc.log') then
      LogFile := TSynLogFileEcho.Create('abc.log')
    else LogFile := TSynLogFileEcho.Create;

    WaitForEnter('Listing initial content');
    ShowLogContent;

    TTestLogger.Add.Log(sllNewRun, 'Starting Up!');
    TTestLogger.Family.EchoRemoteStart(nil, LogFile.LogEcho, False);

    WaitForEnter('After first log');
    ShowLogContent;

    TTestLogger.Add.Log(sllInfo, 'Information, information, information');
    TTestLogger.Add.Log(sllDebug, 'Debug, debug, debug');
    TTestLogger.Add.Log(sllWarning, 'Warning, warning, warning');

    WaitForEnter('After sample logs');
    ShowLogContent;

    WaitForEnter('The End');

    LogFile.Free;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

The first time you run it, echo would be correct.

The second time you run it, the log text from the log file will be dumped correctly, but subsequent echoes will show blank text.

The third time you run it, log text from first and second run will be shown, but subsequent echoes will also show blank text.

Offline

#5 2014-10-06 09:52:46

cheemeng
Member
From: Malaysia
Registered: 2011-08-09
Posts: 61

Re: TSynLogFile.GetEventText returns an Empty String?

Just wondering if anyone has had time to test it out? Thank you.

Offline

Board footer

Powered by FluxBB