pas2js: json output: fixed error messages

git-svn-id: trunk@38040 -
This commit is contained in:
Mattias Gaertner 2018-01-25 17:53:13 +00:00
parent ade2779f93
commit e2d23ea70e
2 changed files with 113 additions and 95 deletions

View File

@ -978,8 +978,8 @@ begin
try
if ShowDebug then
begin
Log.LogRaw('Pas-Module:');
Log.LogRaw(PasModule.GetDeclaration(true));
Log.LogPlain('Pas-Module:');
Log.LogPlain(PasModule.GetDeclaration(true));
end;
// analyze
@ -1007,7 +1007,7 @@ end;
procedure TPas2jsCompilerFile.ParsePascal;
begin
if ShowDebug then
Log.LogRaw(['Debug: Parsing Pascal "',PasFilename,'"...']);
Log.LogPlain(['Debug: Parsing Pascal "',PasFilename,'"...']);
try
// parse Pascal
PascalResolver.InterfaceOnly:=IsForeign;
@ -1281,7 +1281,7 @@ begin
// known unit
if (aFile.PasUnitName<>'') and (CompareText(aFile.PasUnitName,UseUnitname)<>0) then
begin
Log.LogRaw(['Debug: TPas2jsPasTree.FindUnit unitname MISMATCH aFile.PasUnitname="',aFile.PasUnitName,'"',
Log.LogPlain(['Debug: TPas2jsPasTree.FindUnit unitname MISMATCH aFile.PasUnitname="',aFile.PasUnitName,'"',
' Self=',FileResolver.Cache.FormatPath(PasFilename),
' Uses=',UseUnitname,
' IsForeign=',IsForeign]);
@ -1302,7 +1302,7 @@ begin
UseJSFilename:='';
if (not IsForeign) then
UseJSFilename:=FileResolver.FindUnitJSFileName(UsePasFilename);
// Log.LogRaw(['Debug: TPas2jsPasTree.FindUnit Self=',FileResolver.Cache.FormatPath(PasFilename),
// Log.LogPlain(['Debug: TPas2jsPasTree.FindUnit Self=',FileResolver.Cache.FormatPath(PasFilename),
// ' Uses=',UseUnitname,' Found="',FileResolver.Cache.FormatPath(UsePasFilename),'"',
// ' IsForeign=',IsForeign,' JSFile="',FileResolver.Cache.FormatPath(useJSFilename),'"']);
@ -1795,7 +1795,7 @@ begin
end;
except
on E: Exception do begin
Log.LogRaw('Error: '+E.Message);
Log.LogPlain('Error: '+E.Message);
Log.LogMsg(nUnableToWriteFile,[FileCache.FormatPath(DestFilename)]);
Terminate(ExitCodeWriteError);
end;
@ -1819,7 +1819,7 @@ begin
end;
except
on E: Exception do begin
Log.LogRaw('Error: '+E.Message);
Log.LogPlain('Error: '+E.Message);
Log.LogMsg(nUnableToWriteFile,[FileCache.FormatPath(MapFilename)]);
Terminate(ExitCodeWriteError);
end;
@ -1878,7 +1878,7 @@ end;
procedure TPas2jsCompiler.RaiseInternalError(id: int64; Msg: string);
begin
Log.LogRaw('['+IntToStr(id)+'] '+Msg);
Log.LogPlain('['+IntToStr(id)+'] '+Msg);
raise Exception.Create(Msg);
end;
@ -2172,7 +2172,7 @@ end;
procedure TPas2jsCompiler.ParamFatal(Msg: string);
begin
Log.LogRaw(['Fatal: ',Msg]);
Log.LogPlain(['Fatal: ',Msg]);
Terminate(ExitCodeErrorInParams);
end;
@ -2273,7 +2273,7 @@ begin
end;
inc(p);
until false;
Log.LogRaw(Value);
Log.LogPlain(Value);
Terminate(0);
end;
'B','l','n':
@ -3288,12 +3288,12 @@ begin
if FHasShownLogo then exit;
FHasShownLogo:=true;
WriteVersionLine;
Log.LogRaw('Copyright (c) 2017 Mattias Gaertner and others');
Log.LogPlain('Copyright (c) 2017 Mattias Gaertner and others');
end;
procedure TPas2jsCompiler.WriteVersionLine;
begin
Log.LogRaw('Pas2JS Compiler version '+GetVersion(false));
Log.LogPlain('Pas2JS Compiler version '+GetVersion(false));
end;
procedure TPas2jsCompiler.WriteOptions;
@ -3367,37 +3367,37 @@ procedure TPas2jsCompiler.WriteInfo;
begin
WriteVersionLine;
Log.LogLn;
Log.LogRaw('Compiler date : '+GetCompiledDate);
Log.LogRaw('Compiler CPU target: '+GetCompiledTargetCPU);
Log.LogPlain('Compiler date : '+GetCompiledDate);
Log.LogPlain('Compiler CPU target: '+GetCompiledTargetCPU);
Log.LogLn;
Log.LogRaw('Supported targets (targets marked with ''{*}'' are under development):');
Log.LogRaw([' ',PasToJsPlatformNames[PlatformBrowser],': webbrowser']);
Log.LogRaw([' ',PasToJsPlatformNames[PlatformNodeJS],': Node.js']);
Log.LogPlain('Supported targets (targets marked with ''{*}'' are under development):');
Log.LogPlain([' ',PasToJsPlatformNames[PlatformBrowser],': webbrowser']);
Log.LogPlain([' ',PasToJsPlatformNames[PlatformNodeJS],': Node.js']);
Log.LogLn;
Log.LogRaw('Supported CPU instruction sets:');
Log.LogRaw(' ECMAScript5, ECMAScript6');
Log.LogPlain('Supported CPU instruction sets:');
Log.LogPlain(' ECMAScript5, ECMAScript6');
Log.LogLn;
Log.LogRaw('Recognized compiler and RTL features:');
Log.LogRaw(' RTTI,CLASSES,EXCEPTIONS,EXITCODE,RANDOM,DYNARRAYS,COMMANDARGS,');
Log.LogRaw(' UNICODESTRINGS');
Log.LogPlain('Recognized compiler and RTL features:');
Log.LogPlain(' RTTI,CLASSES,EXCEPTIONS,EXITCODE,RANDOM,DYNARRAYS,COMMANDARGS,');
Log.LogPlain(' UNICODESTRINGS');
Log.LogLn;
Log.LogRaw('Supported Optimizations:');
Log.LogRaw(' EnumNumbers');
Log.LogRaw(' RemoveNotUsedPrivates');
Log.LogPlain('Supported Optimizations:');
Log.LogPlain(' EnumNumbers');
Log.LogPlain(' RemoveNotUsedPrivates');
Log.LogLn;
Log.LogRaw('Supported Whole Program Optimizations:');
Log.LogRaw(' RemoveNotUsedDeclarations');
Log.LogPlain('Supported Whole Program Optimizations:');
Log.LogPlain(' RemoveNotUsedDeclarations');
Log.LogLn;
Log.LogRaw('This program comes under the Library GNU General Public License');
Log.LogRaw('For more information read COPYING.FPC, included in this distribution');
Log.LogPlain('This program comes under the Library GNU General Public License');
Log.LogPlain('For more information read COPYING.FPC, included in this distribution');
Log.LogLn;
Log.LogRaw('Please report bugs in our bug tracker on:');
Log.LogRaw(' http://bugs.freepascal.org');
Log.LogPlain('Please report bugs in our bug tracker on:');
Log.LogPlain(' http://bugs.freepascal.org');
Log.LogLn;
Log.LogRaw('More information may be found on our WWW pages (including directions');
Log.LogRaw('for mailing lists useful for asking questions or discussing potential');
Log.LogRaw('new features, etc.):');
Log.LogRaw(' http://www.freepascal.org');
Log.LogPlain('More information may be found on our WWW pages (including directions');
Log.LogPlain('for mailing lists useful for asking questions or discussing potential');
Log.LogPlain('new features, etc.):');
Log.LogPlain(' http://www.freepascal.org');
end;
function TPas2jsCompiler.GetShownMsgTypes: TMessageTypes;
@ -3480,7 +3480,7 @@ begin
aFile.CreateScannerAndParser(FileCache.CreateResolver);
if ShowDebug then
Log.LogRaw(['Debug: Opening file "',PasFilename,'"...']);
Log.LogPlain(['Debug: Opening file "',PasFilename,'"...']);
// open file (beware: this changes aPasTree.FileResolver.BaseDirectory)
aFile.OpenFile(PasFilename);
end;

View File

@ -65,6 +65,7 @@ type
procedure SetOutputFilename(AValue: string);
procedure SetSorted(AValue: boolean);
procedure DoLogRaw(const Msg: string; SkipEncoding : Boolean);
function Concatenate(Args: array of const): string;
public
constructor Create;
destructor Destroy; override;
@ -74,14 +75,16 @@ type
procedure LogRaw(const Msg: string); overload;
procedure LogRaw(Args: array of const); overload;
procedure LogLn;
procedure LogPlain(const Msg: string); overload;
procedure LogPlain(Args: array of const); overload;
procedure LogMsg(MsgNumber: integer; Args: array of const;
const Filename: string = ''; Line: integer = 0; Col: integer = 0;
UseFilter: boolean = true);
procedure Log(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
const Filename: string = ''; Line: integer = 0; Col: integer = 0;
UseFilter: boolean = true);
procedure LogMsgIgnoreFilter(MsgNumber: integer; Args: array of const);
function MsgTypeToStr(MsgType: TMessageType): string;
procedure Log(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
const Filename: string = ''; Line: integer = 0; Col: integer = 0;
UseFilter: boolean = true);
function GetMsgText(MsgNumber: integer; Args: array of const): string;
function FormatMsg(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
const Filename: string = ''; Line: integer = 0; Col: integer = 0): string;
@ -423,7 +426,7 @@ var
begin
NewValue:=NormalizeEncoding(AValue);
if FEncoding=NewValue then Exit;
//LogRaw(ClassName+': Encoding changed from "'+FEncoding+'" to "'+NewValue+'"');
//LogPlain(ClassName+': Encoding changed from "'+FEncoding+'" to "'+NewValue+'"');
FEncoding:=NewValue;
end;
@ -488,7 +491,8 @@ begin
S:=Msg
else
begin
if Encoding='utf8' then
if (Encoding='utf8') or (Encoding='json') then
S:=Msg
else if Encoding='console' then
S:=UTF8ToConsole(Msg)
else if Encoding='system' then
@ -500,7 +504,7 @@ begin
S:=UTF8ToConsole(Msg);
end;
end;
//writeln('TPas2jsLogger.LogRaw "',Encoding,'" "',DbgStr(S),'"');
//writeln('TPas2jsLogger.LogPlain "',Encoding,'" "',DbgStr(S),'"');
if FOnLog<>Nil then
FOnLog(Self,S)
else if FOutputFile<>nil then
@ -513,6 +517,41 @@ begin
end;
end;
function TPas2jsLogger.Concatenate(Args: array of const): string;
var
s: String;
i: Integer;
begin
s:='';
for i:=Low(Args) to High(Args) do
begin
case Args[i].VType of
vtInteger: s += IntToStr(Args[i].VInteger);
vtBoolean: s += BoolToStr(Args[i].VBoolean);
vtChar: s += Args[i].VChar;
{$ifndef FPUNONE}
vtExtended: ; // Args[i].VExtended^;
{$ENDIF}
vtString: s += Args[i].VString^;
vtPointer: ; // Args[i].VPointer;
vtPChar: s += Args[i].VPChar;
vtObject: ; // Args[i].VObject;
vtClass: ; // Args[i].VClass;
vtWideChar: s += AnsiString(Args[i].VWideChar);
vtPWideChar: s += AnsiString(Args[i].VPWideChar);
vtAnsiString: s += AnsiString(Args[i].VAnsiString);
vtCurrency: ; // Args[i].VCurrency^);
vtVariant: ; // Args[i].VVariant^);
vtInterface: ; // Args[i].VInterface^);
vtWidestring: s += AnsiString(WideString(Args[i].VWideString));
vtInt64: s += IntToStr(Args[i].VInt64^);
vtQWord: s += IntToStr(Args[i].VQWord^);
vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString));
end;
end;
Result:=s;
end;
constructor TPas2jsLogger.Create;
begin
FMsg:=TFPList.Create;
@ -612,38 +651,8 @@ begin
end;
procedure TPas2jsLogger.LogRaw(Args: array of const);
var
s: String;
i: Integer;
begin
s:='';
for i:=Low(Args) to High(Args) do
begin
case Args[i].VType of
vtInteger: s += IntToStr(Args[i].VInteger);
vtBoolean: s += BoolToStr(Args[i].VBoolean);
vtChar: s += Args[i].VChar;
{$ifndef FPUNONE}
vtExtended: ; // Args[i].VExtended^;
{$ENDIF}
vtString: s += Args[i].VString^;
vtPointer: ; // Args[i].VPointer;
vtPChar: s += Args[i].VPChar;
vtObject: ; // Args[i].VObject;
vtClass: ; // Args[i].VClass;
vtWideChar: s += AnsiString(Args[i].VWideChar);
vtPWideChar: s += AnsiString(Args[i].VPWideChar);
vtAnsiString: s += AnsiString(Args[i].VAnsiString);
vtCurrency: ; // Args[i].VCurrency^);
vtVariant: ; // Args[i].VVariant^);
vtInterface: ; // Args[i].VInterface^);
vtWidestring: s += AnsiString(WideString(Args[i].VWideString));
vtInt64: s += IntToStr(Args[i].VInt64^);
vtQWord: s += IntToStr(Args[i].VQWord^);
vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString));
end;
end;
LogRaw(s);
LogRaw(Concatenate(Args));
end;
procedure TPas2jsLogger.LogLn;
@ -651,25 +660,46 @@ begin
LogRaw('');
end;
procedure TPas2jsLogger.LogPlain(const Msg: string);
var
s: String;
begin
if encoding='json' then
begin
s:=FormatJSONMsg(mtInfo,Msg,0,'',0,0);
DoLogRaw(s,True);
end
else
DoLogRaw(Msg,False);
end;
procedure TPas2jsLogger.LogPlain(Args: array of const);
begin
LogPlain(Concatenate(Args));
end;
procedure TPas2jsLogger.LogMsg(MsgNumber: integer; Args: array of const;
const Filename: string; Line: integer; Col: integer; UseFilter: boolean);
var
s: String;
Msg: TPas2jsMessage;
begin
Msg:=FindMsg(MsgNumber,true);
if UseFilter and not (Msg.Typ in FShowMsgTypes) then exit;
Log(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col,UseFilter);
end;
procedure TPas2jsLogger.Log(MsgType: TMessageType; Msg: string;
MsgNumber: integer; const Filename: string; Line: integer; Col: integer;
UseFilter: boolean);
var
s: String;
begin
if UseFilter and not (MsgType in FShowMsgTypes) then exit;
if MsgNumberDisabled[MsgNumber] then exit;
if encoding='json' then
begin
s:=FormatJSONMsg(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col);
DoLogRaw(S,True);
end
s:=FormatJSONMsg(MsgType,Msg,MsgNumber,Filename,Line,Col)
else
begin
s:=FormatMsg(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col);
DoLogRaw(S,False);
end;
s:=FormatMsg(MsgType,Msg,MsgNumber,Filename,Line,Col);
LogRaw(s);
end;
procedure TPas2jsLogger.LogMsgIgnoreFilter(MsgNumber: integer;
@ -692,18 +722,6 @@ begin
end;
end;
procedure TPas2jsLogger.Log(MsgType: TMessageType; Msg: string;
MsgNumber: integer; const Filename: string; Line: integer; Col: integer;
UseFilter: boolean);
var
s: String;
begin
if UseFilter and not (MsgType in FShowMsgTypes) then exit;
if MsgNumberDisabled[MsgNumber] then exit;
s:=FormatMsg(MsgType,Msg,MsgNumber,Filename,Line,Col);
LogRaw(s);
end;
function TPas2jsLogger.FormatMsg(MsgType: TMessageType; Msg: string;
MsgNumber: integer; const Filename: string; Line: integer; Col: integer
): string;