utilada_2.6.0_99ca46a1/samples/syslog_appenders.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
with System;
with Interfaces.C;
with Ada.Finalization;
with Util.Systems.Constants;
package body Syslog_Appenders is

   use Util.Systems.Constants;
   use type System.Address;

   type Syslog_Appender_Access is access all Syslog_Appender'Class;

   procedure Sys_Openlog (Ident    : System.Address;
                          Options  : Interfaces.C.int;
                          Facility : Interfaces.C.int)
     with Import => True, Convention => C,
     Link_Name => SYMBOL_PREFIX & "openlog";

   procedure Sys_Syslog (Priority : Interfaces.C.int;
                         Format   : System.Address;
                         Param1   : System.Address)
     with Import => True, Convention => C,
     Link_Name => SYMBOL_PREFIX & "syslog";

   --  Use weak symbols to avoid compilation issues when this is not available.
   pragma Weak_External (Sys_Openlog);
   pragma Weak_External (Sys_Syslog);

   LOG_PID    : constant Interfaces.C.int := 1;
   LOG_ERROR  : constant Interfaces.C.int := 3;
   LOG_LOCAL1 : constant Interfaces.C.int := 136;

   function Create (Name       : in String;
                    Properties : in Util.Properties.Manager;
                    Default    : in Util.Log.Level_Type)
                    return Util.Log.Appenders.Appender_Access is
      Result : constant Syslog_Appender_Access
        := new Syslog_Appender '(Ada.Finalization.Limited_Controlled
                                   with Length => Name'Length,
                                 Name => Name,
                                 others => <>);
   begin
      Result.Set_Level (Name, Properties, Default);
      Result.Set_Layout (Name, Properties, Util.Log.Appenders.FULL);
      if Sys_Openlog'Address /= System.Null_Address then
         Sys_Openlog (Result.Name'Address, LOG_PID, LOG_LOCAL1);
      end if;
      return Result.all'Access;
   end Create;

   overriding
   procedure Append (Self    : in out Syslog_Appender;
                     Message : in Util.Strings.Builders.Builder;
                     Date    : in Ada.Calendar.Time;
                     Level   : in Util.Log.Level_Type;
                     Logger  : in String) is
      pragma Unreferenced (Date, Logger);

      Def_Format : aliased constant String := "%s" & ASCII.NUL;
   begin
      if Self.Level >= Level and then Sys_Syslog'Address /= System.Null_Address
      then
         declare
            Msg : constant String
              := Util.Strings.Builders.To_Array (Message) & ASCII.NUL;
         begin
            Sys_Syslog (LOG_ERROR, Def_Format'Address, Msg'Address);
         end;
      end if;
   end Append;

end Syslog_Appenders;