| 1 |
with Interfaces.C_Streams; |
| 2 |
with Simple_Logging; use Simple_Logging; |
| 3 |
with Simple_Logging.Decorators; |
| 4 |
|
| 5 |
package body CLIC.TTY is |
| 6 |
|
| 7 |
use all type ANSI.Colors; |
| 8 |
use all type ANSI.Styles; |
| 9 |
|
| 10 |
Use_Color : Boolean := False; -- Err on the safe side |
| 11 |
|
| 12 |
function Regular_Decorator (Level : Simple_Logging.Levels; |
| 13 |
Message : String) return String; |
| 14 |
|
| 15 |
function Verbose_Decorator (Level : Simple_Logging.Levels; |
| 16 |
Message : String) return String; |
| 17 |
|
| 18 |
Disabled_By_User : Boolean := False; |
| 19 |
|
| 20 |
----------------------- |
| 21 |
-- Force_Disable_TTY -- |
| 22 |
----------------------- |
| 23 |
|
| 24 |
procedure Force_Disable_TTY is |
| 25 |
begin |
| 26 |
Disabled_By_User := True; |
| 27 |
end Force_Disable_TTY; |
| 28 |
|
| 29 |
--------------- |
| 30 |
-- Check_TTY -- |
| 31 |
--------------- |
| 32 |
|
| 33 |
function Check_TTY return Boolean is |
| 34 |
use Interfaces.C_Streams; |
| 35 |
begin |
| 36 |
return isatty (fileno (stdout)) /= 0; |
| 37 |
end Check_TTY; |
| 38 |
|
| 39 |
------------ |
| 40 |
-- Is_TTY -- |
| 41 |
------------ |
| 42 |
|
| 43 |
function Is_TTY return Boolean |
| 44 |
is (if Disabled_By_User then False else Check_TTY); |
| 45 |
|
| 46 |
------------------- |
| 47 |
-- Color_Enabled -- |
| 48 |
------------------- |
| 49 |
|
| 50 |
function Color_Enabled return Boolean is (Use_Color); |
| 51 |
|
| 52 |
------------------- |
| 53 |
-- Disable_Color -- |
| 54 |
------------------- |
| 55 |
|
| 56 |
procedure Disable_Color is |
| 57 |
begin |
| 58 |
Use_Color := False; |
| 59 |
end Disable_Color; |
| 60 |
|
| 61 |
------------------ |
| 62 |
-- Enable_Color -- |
| 63 |
------------------ |
| 64 |
|
| 65 |
procedure Enable_Color (Force : Boolean := False) is |
| 66 |
begin |
| 67 |
|
| 68 |
-- Enable when appropriate |
| 69 |
|
| 70 |
if Force or else Is_TTY then |
| 71 |
Use_Color := True; |
| 72 |
Simple_Logging.Debug ("Color output enabled"); |
| 73 |
else |
| 74 |
Simple_Logging.Debug ("Color output was requested but not enabled:" |
| 75 |
& " force=" & Force'Img |
| 76 |
& "; Is_TTY=" & Is_TTY'Img); |
| 77 |
end if; |
| 78 |
|
| 79 |
-- Set debug colors. When Detail/Debug are also enabled, we add the |
| 80 |
-- "info:" prefix to otherwise normal output, so it's seen more easily. |
| 81 |
|
| 82 |
if Use_Color then |
| 83 |
case Simple_Logging.Level is |
| 84 |
when Always .. Info => |
| 85 |
Decorators.Level_Decorator := |
| 86 |
Regular_Decorator'Access; |
| 87 |
when others => |
| 88 |
Decorators.Level_Decorator := |
| 89 |
Verbose_Decorator'Access; |
| 90 |
end case; |
| 91 |
end if; |
| 92 |
end Enable_Color; |
| 93 |
|
| 94 |
------------ |
| 95 |
-- Format -- |
| 96 |
------------ |
| 97 |
|
| 98 |
function Format (Text : String; |
| 99 |
Fore : ANSI.Colors := ANSI.Default; |
| 100 |
Back : ANSI.Colors := ANSI.Default; |
| 101 |
Style : ANSI.Styles := ANSI.Default) |
| 102 |
return String |
| 103 |
is |
| 104 |
use ANSI; |
| 105 |
begin |
| 106 |
if not Use_Color then |
| 107 |
return Text; |
| 108 |
end if; |
| 109 |
|
| 110 |
return |
| 111 |
((if Fore /= Default then Foreground (Fore) else "") |
| 112 |
& (if Back /= Default then Background (Fore) else "") |
| 113 |
& (if Style /= Default then ANSI.Style (Style, On) else "") |
| 114 |
& Text |
| 115 |
& (if Fore /= Default then Foreground (Default) else "") |
| 116 |
& (if Back /= Default then Background (Default) else "") |
| 117 |
& (if Style /= Default then ANSI.Style (Style, Off) else "")); |
| 118 |
end Format; |
| 119 |
|
| 120 |
----------------------- |
| 121 |
-- Regular_Decorator -- |
| 122 |
----------------------- |
| 123 |
|
| 124 |
function Regular_Decorator (Level : Simple_Logging.Levels; |
| 125 |
Message : String) return String is |
| 126 |
(case Level is |
| 127 |
when Info => Message, |
| 128 |
when others => Verbose_Decorator (Level, Message)); |
| 129 |
|
| 130 |
----------------------- |
| 131 |
-- Verbose_Decorator -- |
| 132 |
----------------------- |
| 133 |
|
| 134 |
function Verbose_Decorator (Level : Simple_Logging.Levels; |
| 135 |
Message : String) return String |
| 136 |
is |
| 137 |
use ANSI; |
| 138 |
begin |
| 139 |
return |
| 140 |
(case Level is |
| 141 |
when Always => Message, |
| 142 |
when Error => |
| 143 |
ANSI.Wrap (Text => "error:", |
| 144 |
Style => Bright, |
| 145 |
Foreground => ANSI.Foreground (Red)) & " " & Message, |
| 146 |
when Warning => |
| 147 |
ANSI.Wrap (Text => "warn:", |
| 148 |
Style => Bright, |
| 149 |
Foreground => ANSI.Foreground (Yellow)) & " " & Message, |
| 150 |
when Info => |
| 151 |
ANSI.Wrap (Text => "info:", |
| 152 |
Style => Bright, |
| 153 |
Foreground => ANSI.Foreground (Green)) & " " & Message, |
| 154 |
when Detail => |
| 155 |
ANSI.Wrap (Text => "detail:", |
| 156 |
Style => Bright, |
| 157 |
Foreground => ANSI.Foreground (Cyan)) & " " & Message, |
| 158 |
when Debug => |
| 159 |
ANSI.Wrap (Text => "debug:", |
| 160 |
Style => Default, |
| 161 |
Foreground => ANSI.Foreground (Grey)) & " " |
| 162 |
& ANSI.Wrap (Text => Message, |
| 163 |
Style => Dim)); |
| 164 |
end Verbose_Decorator; |
| 165 |
|
| 166 |
end CLIC.TTY; |