Browse Source

* Add JSON output encoding of messages

git-svn-id: trunk@38035 -
michael 7 years ago
parent
commit
a834444cc0

+ 2 - 1
packages/pastojs/src/pas2jscompiler.pp

@@ -2350,7 +2350,7 @@ begin
             begin
             begin
             Identifier:=NormalizeEncoding(String(p));
             Identifier:=NormalizeEncoding(String(p));
             case Identifier of
             case Identifier of
-            'console','system','utf8': Log.Encoding:=Identifier;
+            'console','system','utf8', 'json': Log.Encoding:=Identifier;
             else ParamFatal('invalid encoding "'+String(p)+'"');
             else ParamFatal('invalid encoding "'+String(p)+'"');
             end;
             end;
             end;
             end;
@@ -3215,6 +3215,7 @@ begin
   l('     -Jeconsole : Console codepage. This is the default.');
   l('     -Jeconsole : Console codepage. This is the default.');
   l('     -Jesystem  : System codepage. On non Windows console and system are the same.');
   l('     -Jesystem  : System codepage. On non Windows console and system are the same.');
   l('     -Jeutf-8   : Unicode UTF-8. Default when using -Fe.');
   l('     -Jeutf-8   : Unicode UTF-8. Default when using -Fe.');
+  l('     -JeJSON    : Output compiler messages as JSON. Logo etc are outputted as-is.');
   l('   -Ji<x> : Insert JS file <x> into main JS file. E.g. -Jirtl.js. Can be given multiple times. To remove a file name append a minus, e.g. -Jirtl.js-.');
   l('   -Ji<x> : Insert JS file <x> into main JS file. E.g. -Jirtl.js. Can be given multiple times. To remove a file name append a minus, e.g. -Jirtl.js-.');
   l('   -Jl    : lower case identifiers');
   l('   -Jl    : lower case identifiers');
   l('   -Jm    : generate source maps');
   l('   -Jm    : generate source maps');

+ 0 - 1
packages/pastojs/src/pas2jslibcompiler.pp

@@ -67,7 +67,6 @@ Type
 
 
 Type
 Type
   PPas2JSCompiler = Pointer;
   PPas2JSCompiler = Pointer;
-  PStubCreator = Pointer;
 
 
 Procedure SetPas2JSWriteJSCallBack(P : PPas2JSCompiler; ACallBack : TWriteJSCallBack; CallBackData : Pointer); stdcall;
 Procedure SetPas2JSWriteJSCallBack(P : PPas2JSCompiler; ACallBack : TWriteJSCallBack; CallBackData : Pointer); stdcall;
 Procedure SetPas2JSCompilerLogCallBack(P : PPas2JSCompiler; ACallBack : TLibLogCallBack; CallBackData : Pointer); stdcall;
 Procedure SetPas2JSCompilerLogCallBack(P : PPas2JSCompiler; ACallBack : TLibLogCallBack; CallBackData : Pointer); stdcall;

+ 78 - 27
packages/pastojs/src/pas2jslogger.pp

@@ -13,7 +13,7 @@ unit Pas2jsLogger;
 interface
 interface
 
 
 uses
 uses
-  Classes, SysUtils, PasTree, PScanner, jstree, jsbase, jswriter,
+  Classes, SysUtils, PasTree, PScanner, jstree, jsbase, jswriter, fpjson,
   Pas2jsFileUtils;
   Pas2jsFileUtils;
 
 
 const
 const
@@ -64,6 +64,7 @@ type
     procedure SetMsgNumberDisabled(MsgNumber: integer; AValue: boolean);
     procedure SetMsgNumberDisabled(MsgNumber: integer; AValue: boolean);
     procedure SetOutputFilename(AValue: string);
     procedure SetOutputFilename(AValue: string);
     procedure SetSorted(AValue: boolean);
     procedure SetSorted(AValue: boolean);
+    procedure DoLogRaw(const Msg: string; SkipEncoding : Boolean);
   public
   public
     constructor Create;
     constructor Create;
     destructor Destroy; override;
     destructor Destroy; override;
@@ -84,6 +85,8 @@ type
     function GetMsgText(MsgNumber: integer; Args: array of const): string;
     function GetMsgText(MsgNumber: integer; Args: array of const): string;
     function FormatMsg(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
     function FormatMsg(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
       const Filename: string = ''; Line: integer = 0; Col: integer = 0): string;
       const Filename: string = ''; Line: integer = 0; Col: integer = 0): string;
+    function FormatJSONMsg(MsgType: TMessageType; Msg: string; MsgNumber: integer = 0;
+      const Filename: string = ''; Line: integer = 0; Col: integer = 0): string;
     procedure OpenOutputFile;
     procedure OpenOutputFile;
     procedure Flush;
     procedure Flush;
     procedure CloseOutputFile;
     procedure CloseOutputFile;
@@ -475,6 +478,41 @@ begin
   if FSorted then Sort;
   if FSorted then Sort;
 end;
 end;
 
 
+procedure TPas2jsLogger.DoLogRaw(const Msg: string; SkipEncoding : Boolean);
+
+Var
+  S : String;
+
+begin
+  if SkipEncoding then
+    S:=Msg
+  else
+    begin
+    if Encoding='utf8' then
+    else if Encoding='console' then
+      S:=UTF8ToConsole(Msg)
+    else if Encoding='system' then
+      S:=UTF8ToSystemCP(Msg)
+    else
+      begin
+      // default: write UTF-8 to outputfile and console codepage to console
+      if FOutputFile=nil then
+        S:=UTF8ToConsole(Msg);
+      end;
+    end;
+  //writeln('TPas2jsLogger.LogRaw "',Encoding,'" "',DbgStr(S),'"');
+  if FOnLog<>Nil then
+    FOnLog(Self,S)
+  else if FOutputFile<>nil then
+    FOutputFile.Write(S+LineEnding)
+  else
+    begin
+    // prevent codepage conversion magic
+    SetCodePage(RawByteString(S), CP_OEMCP, False);
+    writeln(S);
+    end;
+end;
+
 constructor TPas2jsLogger.Create;
 constructor TPas2jsLogger.Create;
 begin
 begin
   FMsg:=TFPList.Create;
   FMsg:=TFPList.Create;
@@ -569,30 +607,8 @@ begin
 end;
 end;
 
 
 procedure TPas2jsLogger.LogRaw(const Msg: string);
 procedure TPas2jsLogger.LogRaw(const Msg: string);
-var
-  S: String;
-begin
-  S:=Msg;
-  if Encoding='utf8' then
-  else if Encoding='console' then
-    S:=UTF8ToConsole(S)
-  else if Encoding='system' then
-    S:=UTF8ToSystemCP(S)
-  else begin
-    // default: write UTF-8 to outputfile and console codepage to console
-    if FOutputFile=nil then
-      S:=UTF8ToConsole(S);
-  end;
-  //writeln('TPas2jsLogger.LogRaw "',Encoding,'" "',DbgStr(S),'"');
-  if FOnLog<>Nil then
-    FOnLog(Self,S)
-  else if FOutputFile<>nil then
-    FOutputFile.Write(S+LineEnding)
-  else begin
-    // prevent codepage conversion magic
-    SetCodePage(RawByteString(S), CP_OEMCP, False);
-    writeln(S);
-  end;
+begin
+  DoLogRaw(Msg,False);
 end;
 end;
 
 
 procedure TPas2jsLogger.LogRaw(Args: array of const);
 procedure TPas2jsLogger.LogRaw(Args: array of const);
@@ -644,8 +660,16 @@ begin
   Msg:=FindMsg(MsgNumber,true);
   Msg:=FindMsg(MsgNumber,true);
   if UseFilter and not (Msg.Typ in FShowMsgTypes) then exit;
   if UseFilter and not (Msg.Typ in FShowMsgTypes) then exit;
   if MsgNumberDisabled[MsgNumber] then exit;
   if MsgNumberDisabled[MsgNumber] then exit;
-  s:=FormatMsg(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col);
-  LogRaw(s);
+  if encoding='json' then
+    begin
+    s:=FormatJSONMsg(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col);
+    DoLogRaw(S,True);
+    end
+  else
+    begin
+    s:=FormatMsg(Msg.Typ,SafeFormat(Msg.Pattern,Args),MsgNumber,Filename,Line,Col);
+    DoLogRaw(S,False);
+    end;
 end;
 end;
 
 
 procedure TPas2jsLogger.LogMsgIgnoreFilter(MsgNumber: integer;
 procedure TPas2jsLogger.LogMsgIgnoreFilter(MsgNumber: integer;
@@ -709,6 +733,33 @@ begin
   Result:=s;
   Result:=s;
 end;
 end;
 
 
+function TPas2jsLogger.FormatJSONMsg(MsgType: TMessageType; Msg: string; MsgNumber: integer; const Filename: string; Line: integer;
+  Col: integer): string;
+
+Var
+  J : TJSONObject;
+  FN : String;
+
+begin
+  if Assigned(OnFormatPath) then
+    FN:=OnFormatPath(Filename)
+  else
+    FN:=Filename;
+  J:=TJSONObject.Create([
+    'message',Msg,
+    'line',Line,
+    'col',Col,
+    'number',MsgNumber,
+    'filename',FN,
+    'type',MsgTypeToStr(MsgType)
+    ]);
+  try
+    Result:=J.AsJSON;
+  finally
+    J.Free;
+  end;
+end;
+
 procedure TPas2jsLogger.OpenOutputFile;
 procedure TPas2jsLogger.OpenOutputFile;
 begin
 begin
   if FOutputFile<>nil then exit;
   if FOutputFile<>nil then exit;