You are not logged in.
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 ? 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.
Last edited by ComingNine (2017-11-10 10:28:03)
Offline
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
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 ?
Is it documented that hard-cast of a string variable as Pointer returns the pointer to the string content ?
Offline
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
Many thanks for your kind help !
Offline