orka_b455160b/orka/src/orka/implementation/orka-loggers-formatting.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
--  SPDX-License-Identifier: Apache-2.0
--
--  Copyright (c) 2019 onox <denkpadje@gmail.com>
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.

with Ada.Strings.Fixed;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;

with Orka.Terminals;

package body Orka.Loggers.Formatting is

   package SF renames Ada.Strings.Fixed;

   package UTF renames Ada.Strings.UTF_Encoding;

   function Unicode (Number : Long_Integer) return UTF.UTF_8_String is
     (UTF.Wide_Wide_Strings.Encode ("" & Wide_Wide_Character'Val (Number)));

   Length_Level  : constant := 7;

   function Format_Message
     (From     : String;
      Level    : Severity;
      Message  : String;
      Colorize : Boolean) return String
   is
      Level_Color : constant Terminals.Color
        := (case Level is
              when Error   => Terminals.Red,
              when Warning => Terminals.Yellow,
              when Failure => Terminals.Red,
              when Success => Terminals.Green,
              when Info    => Terminals.Blue,
              when Debug   => Terminals.Default);

      Level_Icon : constant UTF.UTF_8_String
        := (case Level is
              when Error   => Unicode (16#F06A#),  --  
              when Warning => Unicode (16#F071#),  --  
              when Failure => Unicode (16#F05C#),  --  
              when Success => Unicode (16#F05D#),  --  
              when Info    => Unicode (16#F05A#),  --  
              when Debug   => " ");
              --  Symbols from font FontAwesome

      Level_Image  : String (1 .. Length_Level);

      function Colorize_Text (Text : String; Color : Terminals.Color) return String is
        (if Colorize then Terminals.Colorize (Text, Color) else Text);
   begin
      SF.Move (Level'Image, Level_Image, Justify => Ada.Strings.Center);

      return
        Colorize_Text (Terminals.Time_Image, Terminals.Grey) &
        " " &
        Colorize_Text ("[" & Level_Icon & " " & Level_Image & "]", Level_Color) &
        " " &
        Colorize_Text ("[" & From & "]", Terminals.Magenta) &
        " " & Terminals.Strip_Line_Term (Message);
   end Format_Message;

end Orka.Loggers.Formatting;