adagsl_335d13f0/toolkit/adalib/src/logging-file.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
75
76
77
78
79
80
81
82
pragma Ada_2020;
with Images ;
package body logging.file is

   procedure Create(fd : in out FileDestination_Type) is
      filename : Unbounded_String ;
   begin
      if fd.cadence > 0.0
      then
         filename := To_Unbounded_String(To_String(fd.basename) & 
                                         images.Image(".%04d",fd.filenumber) & 
                                         To_String(fd.basetype) );
      else
         filename := To_Unbounded_String( To_String(fd.basename) & 
                                          To_String(fd.basetype) );
      end if ;
      Ada.Text_Io.Create( fd.f.all , Ada.Text_Io.Out_File , To_String(filename) ) ;
      fd.filestarted := Clock ;
   end Create ;

   function Create (name : String; 
                    filetype : String := ".log" ;
                    number : Integer := 0 ;
                    rotate : duration := 0.0 ) return FileDestinationPtr_Type is
      result : FileDestinationPtr_Type ;
   begin
      result := new FileDestination_Type ;
      result.basename := To_Unbounded_String(name) ;
      result.basetype := To_Unbounded_String(filetype) ;
      result.cadence := rotate ;
      result.filenumber := number ;

      result.f := new Ada.Text_Io.File_Type ;
      Create(result.all) ;

      return result ;
   end Create ;

   overriding
   procedure SendMessage
     ( dest : in out FileDestination_Type ;
       message : String ;
       level : message_level_type := INFORMATIONAL ;
       source : String := Default_Source_Name ;
       class : String := Default_Message_Class ) is
   begin
      if dest.cadence > 0.0 and then Clock - dest.filestarted > dest.cadence
      then
         Close(dest);
         dest.filenumber := @ + 1 ;
         Create(dest);
      end if ;
      Ada.Text_Io.Put_Line(dest.f.all , Image(message,level,source,class));
   end SendMessage;

   procedure SendMessage
     ( dest : in out FileDestination_Type ;
       prefix : String ;
       message : String ) is
   begin
       if dest.cadence > 0.0 and then Clock - dest.filestarted > dest.cadence
      then
         Close(dest);
         dest.filenumber := @ + 1 ;
         Create(dest);
      end if ;
      Ada.Text_Io.Put(dest.f.all, prefix);
      Ada.Text_Io.Put(dest.f.all," ");
      Ada.Text_Io.Put_Line(dest.f.all , message );
      Ada.Text_Io.Flush(dest.f.all);
      --Put_Line(message);
   end SendMessage ;

   overriding
   procedure Close(dest : FileDestination_Type) is
   begin
      Ada.Text_Io.Put_Line(dest.f.all,"************End of File*****************");
      Ada.Text_Io.Close(dest.f.all);
   end Close ;


end logging.file ;