diff --git a/components/lazutils/lazlogger.pas b/components/lazutils/lazlogger.pas index 4b112f7212..5320f150cc 100644 --- a/components/lazutils/lazlogger.pas +++ b/components/lazutils/lazlogger.pas @@ -15,7 +15,7 @@ interface uses Classes, SysUtils, types, math, // LazUtils - LazLoggerBase, LazClasses, LazFileUtils, LazStringUtils, LazUTF8; + LazLoggerBase, LazClasses, LazFileUtils, LazStringUtils, LazUTF8, laztty; type @@ -536,7 +536,13 @@ begin DoOpenFile; if FActiveLogText = nil then exit; - WriteLn(FActiveLogText^, s); + + //WriteLn(FActiveLogText^, s); + if TTYCheckSupported and IsATTY(FActiveLogText^) then + WriteLn(FActiveLogText^, Colorize(s)) + else + WriteLn(FActiveLogText^, s); + if FCloseLogFileBetweenWrites then DoCloseFile; diff --git a/ide/laztty.pas b/components/lazutils/laztty.pas similarity index 80% rename from ide/laztty.pas rename to components/lazutils/laztty.pas index cf95226914..441dcc1c16 100644 --- a/ide/laztty.pas +++ b/components/lazutils/laztty.pas @@ -41,6 +41,7 @@ type TOutputColor = (oc_black,oc_red,oc_green,oc_orange,og_blue,oc_magenta,oc_cyan,oc_lightgray); procedure WriteColoredOutput(var t: Text;color: TOutputColor;const s : AnsiString); +function Colorize(const s : AnsiString):AnsiString; implementation @@ -174,29 +175,70 @@ begin Result:=IsATTYValue; end; +function open_esc(color: TOutputColor):ansistring; +begin + case color of + oc_black: + Result:=#27'[1m'#27'[30m'; + oc_red: + Result:=#27'[1m'#27'[31m'; + oc_green: + Result:=#27'[1m'#27'[32m'; + oc_orange: + Result:=#27'[1m'#27'[33m'; + og_blue: + Result:=#27'[1m'#27'[34m'; + oc_magenta: + Result:=#27'[1m'#27'[35m'; + oc_cyan: + Result:=#27'[1m'#27'[36m'; + oc_lightgray: + Result:=#27'[1m'#27'[37m'; + end; +end; + +type tkeyword=record + t:pchar; + c:TOutputColor; + end; + +const terms:array[0..6] of tkeyword = +( + (t:'Note:';c:oc_orange), + (t:'Hint:';c:oc_lightgray), + (t:'Warning:';c:oc_magenta), + (t:'Error:';c:oc_red), + (t:'(lazbuild)';c:oc_lightgray), + (t:'(lazarus)';c:oc_cyan), + (t:'gtk2';c:oc_green) +); + +function Colorize(const s : AnsiString):AnsiString; +var + i,p,ll:integer; + color:ToutputColor; + p1,p2,p3:ansistring; +begin + for i:=0 to high(terms) do + begin + p:=pos(terms[i].t,s); + if p<=0 then continue; + ll:=length(terms[i].t); + p1:=copy(s,1,p-1); + p2:=copy(s,p,ll); + p3:=copy(s,p+ll,length(s)); + Result:=p1+ + open_esc(terms[i].c)+p2+#27'[0m'+ + p3; + exit; + end; + Result:=s; +end; + procedure WriteColoredOutput(var t: Text;color: TOutputColor;const s : AnsiString); begin if TTYCheckSupported and IsATTY(t) then - begin - case color of - oc_black: - write(t,#27'[1m'#27'[30m'); - oc_red: - write(t,#27'[1m'#27'[31m'); - oc_green: - write(t,#27'[1m'#27'[32m'); - oc_orange: - write(t,#27'[1m'#27'[33m'); - og_blue: - write(t,#27'[1m'#27'[34m'); - oc_magenta: - write(t,#27'[1m'#27'[35m'); - oc_cyan: - write(t,#27'[1m'#27'[36m'); - oc_lightgray: - write(t,#27'[1m'#27'[37m'); - end; - end; + write(t,open_esc(color)); write(t,s); if TTYCheckSupported and IsATTY(t) then write(t,#27'[0m');