1. -- 
  2. --  Copyright (c) 2008, 
  3. --  Reto Buerki, Adrian-Ken Rueegsegger 
  4. --  secunet SwissIT AG 
  5. -- 
  6. --  This file is part of Alog. 
  7. -- 
  8. --  Alog is free software; you can redistribute it and/or modify 
  9. --  it under the terms of the GNU Lesser General Public License as published 
  10. --  by the Free Software Foundation; either version 2.1 of the License, or 
  11. --  (at your option) any later version. 
  12. -- 
  13. --  Alog is distributed in the hope that it will be useful, 
  14. --  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  16. --  GNU Lesser General Public License for more details. 
  17. -- 
  18. --  You should have received a copy of the GNU Lesser General Public License 
  19. --  along with Alog; if not, write to the Free Software 
  20. --  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
  21. --  MA  02110-1301  USA 
  22. -- 
  23.  
  24. with Ada.Finalization; 
  25. with Ada.Unchecked_Deallocation; 
  26. with Ada.Strings.Unbounded; 
  27.  
  28. with Alog.Facilities; 
  29. with Alog.Transforms; 
  30. with Alog.Controlled_Map; 
  31.  
  32. --  Logger instance. Facilities can be attached to a logger instance in order to 
  33. --  log to different targets simultaneously. A logger provides different helper 
  34. --  functions for logging facilities configuration. 
  35. package Alog.Logger is 
  36.  
  37.    type Instance (Init : Boolean) is tagged limited private; 
  38.    --  Logger instance. The Init discriminant defines whether or not a default 
  39.    --  'stdout' (FD facility without logfile set) is attached automatically. Set 
  40.    --  Init to 'True' if you want to make sure minimal stdout logging is 
  41.    --  possible as soon as a new logger is instantiated. 
  42.  
  43.    type Handle is access all Instance; 
  44.    --  Handle to logger type. 
  45.  
  46.    procedure Attach_Facility 
  47.      (Logger   : in out Instance; 
  48.       Facility :        Facilities.Handle); 
  49.    --  Attach a facility to logger instance. 
  50.  
  51.    procedure Attach_Default_Facility (Logger : in out Instance); 
  52.    --  Attach default facility with name Default_Facility_Name to logger 
  53.    --  instance. If the default facility is already attached do nothing. 
  54.  
  55.    procedure Detach_Facility 
  56.      (Logger : in out Instance; 
  57.       Name   :        String); 
  58.    --  Detach a facility with name 'Name' from logger instance. If the facility 
  59.    --  is not found a Facility_Not_Found exception is raised. 
  60.  
  61.    procedure Detach_Default_Facility (Logger : in out Instance); 
  62.    --  Detach default facility with name Default_Facility_Name from logger 
  63.    --  instance. If the default facility is not attached do nothing. 
  64.  
  65.    function Facility_Count (Logger : Instance) return Natural; 
  66.    --  Return number of attached facilites. 
  67.  
  68.    procedure Update 
  69.      (Logger  : Instance; 
  70.       Name    : String; 
  71.       Process : not null access 
  72.         procedure (Facility_Handle : Facilities.Handle)); 
  73.    --  Update a specific Facility identified by 'Name'. Call the 'Process' 
  74.    --  procedure to perform the update operation. 
  75.  
  76.    procedure Iterate 
  77.      (Logger  : Instance; 
  78.       Process : not null access 
  79.         procedure (Facility_Handle : Facilities.Handle)); 
  80.    --  Call 'Process' for all attached facilities. 
  81.  
  82.    procedure Attach_Transform 
  83.      (Logger    : in out Instance; 
  84.       Transform :        Transforms.Handle); 
  85.    --  Attach a transform to logger instance. 
  86.  
  87.    procedure Detach_Transform 
  88.      (Logger : in out Instance; 
  89.       Name   :        String); 
  90.    --  Detach a transform with name 'Name' from logger instance. If the 
  91.    --  transform is not found a Transform_Not_Found exception is raised. 
  92.  
  93.    function Transform_Count (Logger : Instance) return Natural; 
  94.    --  Return number of attached transforms. 
  95.  
  96.    procedure Update 
  97.      (Logger  : Instance; 
  98.       Name    : String; 
  99.       Process : not null access 
  100.         procedure (Transform_Handle : Transforms.Handle)); 
  101.    --  Update a specific Transform identified by 'Name'. Call the 'Process' 
  102.    --  procedure to perform the update operation. 
  103.  
  104.    procedure Iterate 
  105.      (Logger  : Instance; 
  106.       Process : not null access 
  107.         procedure (Transform_Handle : Transforms.Handle)); 
  108.    --  Call 'Process' for all attached transforms. 
  109.  
  110.    procedure Clear (L : in out Instance); 
  111.    --  Clear logger instance. Detach and teardown all attached facilities and 
  112.    --  transforms. 
  113.  
  114.    procedure Log_Message 
  115.      (Logger : Instance; 
  116.       Source : String := ""; 
  117.       Level  : Log_Level; 
  118.       Msg    : String); 
  119.    --  Log a message. The Write_Message() procedure of all attached facilities 
  120.    --  is called. Depending on the Log-Threshold set, the message is logged to 
  121.    --  different targets (depending on the facilites) automatically. 
  122.    -- 
  123.    --  Prior to actually processing the given log message the policy database is 
  124.    --  inquired if the log message with given source and level should be logged. 
  125.  
  126.    procedure Free is new Ada.Unchecked_Deallocation 
  127.      (Object => Facilities.Class, 
  128.       Name   => Facilities.Handle); 
  129.    --  Free memory allocated by a facility. 
  130.  
  131.    procedure Free is new Ada.Unchecked_Deallocation 
  132.      (Object => Transforms.Class, 
  133.       Name   => Transforms.Handle); 
  134.    --  Free memory allocated by a transform. 
  135.  
  136.    Facility_Not_Found        : exception; 
  137.    --  Will be raised if a requested facility is not found. 
  138.    Facility_Already_Present  : exception; 
  139.    --  Will be raised if a facility is already present. 
  140.    Transform_Not_Found       : exception; 
  141.    --  Will be raised if a requested transform is not found. 
  142.    Transform_Already_Present : exception; 
  143.    --  Will be raised if a facility is already present. . 
  144.  
  145.    Default_Facility_Name : constant String := "__Default_Facility"; 
  146.  
  147. private 
  148.  
  149.    use Ada.Strings.Unbounded; 
  150.    use Alog.Facilities; 
  151.    use Alog.Transforms; 
  152.  
  153.    procedure Initialize (Logger : in out Instance); 
  154.    --  Initialize the logger instance. 
  155.  
  156.    procedure Finalize (Logger : in out Instance); 
  157.    --  Finalize procedure used to cleanup. 
  158.  
  159.    package Map_Of_Transforms_Package is new Alog.Controlled_Map 
  160.      (Key_Type       => Unbounded_String, 
  161.       Element_Type   => Transforms.Class, 
  162.       Element_Handle => Transforms.Handle); 
  163.  
  164.    package MOTP renames Map_Of_Transforms_Package; 
  165.  
  166.    package Map_Of_Facilities_Package is new Alog.Controlled_Map 
  167.      (Key_Type       => Unbounded_String, 
  168.       Element_Type   => Facilities.Class, 
  169.       Element_Handle => Facilities.Handle); 
  170.  
  171.    package MOFP renames Map_Of_Facilities_Package; 
  172.  
  173.    type Instance (Init : Boolean) is new 
  174.      Ada.Finalization.Limited_Controlled with record 
  175.       Facilities   : MOFP.Map; 
  176.       --  Attached facilities. 
  177.  
  178.       Transforms   : MOTP.Map; 
  179.       --  Attached transforms. 
  180.    end record; 
  181.  
  182. end Alog.Logger;