#1 2017-11-10 10:26:22

ComingNine
Member
Registered: 2010-07-29
Posts: 294

How to use the version of TSynLog.Enter with PUTF8Char parameter ?

I try to provide customized string to TSynLog.Enter. However, the following code gives unreadable characters as shown in the picture. The same problem can be seen with Delphi/Win, FPC/Win and FPC/Linux.

Could you help to show the correct way to use the version of TSynLog.Enter with PUTF8Char parameter ? big_smile Many thanks !

program Project1;

{$IFNDEF FPC}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  SysUtils,
  SynCommons, SynLog;

procedure xx;
var
 ILog: ISynLog;
 SMethodName: string;
 SMethodNameUTF8: RawUTF8;
begin
  SMethodName := 'enter xx';
  SMethodNameUTF8 := StringToUTF8(SMethodName);
  ILog := TSynLog.Enter(nil, @SMethodNameUTF8);  // <--- Will show unreadable character
  ILog.Log(sllWarning, 'hi xx');
end;

procedure yy;
var
 ILog: ISynLog;
 SMethodName: string;
 SMethodNameUTF8: RawUTF8;
begin
  SMethodName := 'enter yy';
  SMethodNameUTF8 := StringToUTF8(SMethodName);
  ILog := TSynLog.Enter(nil, @SMethodNameUTF8);  // <--- Will show unreadable character
  ILog.Log(sllWarning, 'hi yy');
end;

begin
  with TSynLog.Family do
  begin
    {$IFDEF MSWINDOWS}
      AutoFlushTimeOut := 1;
    {$ENDIF ~MSWINDOWS}
    Level := LOG_VERBOSE;
    EchoToConsole := LOG_VERBOSE;
    PerThreadLog := ptIdentifiedInOnFile;
    RotateFileCount := 50;
    RotateFileSizeKB := 20 * 1024; // rotate by 20 MB logs
  end;

  xx;
  yy;
end.

30auyo3.png

Last edited by ComingNine (2017-11-10 10:28:03)

Offline

#2 2017-11-10 13:14:56

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

Re: How to use the version of TSynLog.Enter with PUTF8Char parameter ?

Using @SMethodName is wrong: it is a pointer to the string pointer, not to the string content.
Try to use pointer(SMethodNmeUTF8) or @SMethodNameUTF8[1].

Or, even better in your case, use the overloaded TSynLog.Enter method, which uses TextFmt/TextArgs input parameters.

Offline

#3 2017-11-10 14:01:00

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: How to use the version of TSynLog.Enter with PUTF8Char parameter ?

Thank you very much for your helpful explanation !

@SMethodNameUTF8 is in fact what I have used, and does not work as shown.

However, Pointer(SMethodNmeUTF8) does work. The overloaded TSynLog.Enter method, which uses TextFmt/TextArgs input parameters, is even better.

Could you help to comment why Pointer(SMethodNmeUTF8) works but @SMethodNameUTF8 does not ? yikes
Is it documented that hard-cast of a string variable as Pointer returns the pointer to the string content ? yikes

Offline

#4 2017-11-10 14:56:44

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

Re: How to use the version of TSynLog.Enter with PUTF8Char parameter ?

It is standard pascal syntax.
@ returns a pointer to a variable, whereas pointer() type-cast the variable to another type, and @str[1] which returns a pointer to the first char.

Offline

#5 2017-11-10 16:20:52

ComingNine
Member
Registered: 2010-07-29
Posts: 294

Re: How to use the version of TSynLog.Enter with PUTF8Char parameter ?

Many thanks for your kind help ! smile

Offline

Board footer

Powered by FluxBB