adagsl_335d13f0/toolkit/examples/dglogs/src/dglogs.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 Ada.Text_Io; use Ada.Text_Io;
with Ada.Streams ; use Ada.Streams ;
with Ada.Calendar.Formatting ; use Ada.Calendar.Formatting ;
with Ada.Calendar.Time_Zones ; use Ada.Calendar.Time_Zones ;
with Ada.Strings.Unbounded ;  use Ada.Strings.Unbounded;
with GNAT.Sockets ; use GNAT.Sockets;

with cli ; use cli ;
with images ;
with logging ; 
with logging.file ;
with logging.socket ;


procedure Dglogs is
   package GS renames GNAT.Sockets ;
   s : GS.Socket_Type ;

   sa : GS.Sock_Addr_Type ;
   sender : GS.Sock_Addr_Type ;
   lm : aliased logging.socket.Message_Type ;
   logmsg : Stream_Element_Array( 1..lm'Size/8 );
   for logmsg'Address use lm'Address ;
   logmsgsize : Stream_Element_Offset ;
   logfile : logging.file.FileDestinationPtr_Type ;

   function Time_Stamp (t : Ada.Calendar.Time ) return String is
   begin

      return Ada.Calendar.Formatting.Image (t , Time_Zone => Local_Time_Offset ) ;

   end Time_Stamp;

begin
   ProcessCommandLine ;

   GS.Create_Socket( s , 
                     mode => GS.Socket_Datagram ,
                     level => GS.IP_Protocol_For_UDP_Level);
   if cli.Verbosity > 1
   then
      Put("Starting logging service"); New_Line ;
   end if ;
   declare
      he : GS.Host_Entry_Type := GS.Get_Host_By_Name("localhost");
   begin
      sa := GS.Network_Socket_Address( addr => GS.Addresses(he,1) ,
                                    port => GS.Port_Type(cli.port) );
      GS.bind_socket( s , sa );
   end ;
   logfile := logging.file.Create(To_String(cli.logname),rotate => 60.0 );
   logging.SetDestination(logfile);
   loop
      GS.Receive_Socket( s , logmsg , logmsgsize , sender );
      if cli.Verbosity > 10
      then
         Put("Received a message from "); Put( GS.Image(sender)); New_Line;
      end if ;
      declare
         mimg : constant String := logging.Image( lm.mt(1..Integer(lm.ml)) ,
                                         lm.l ,
                                         lm.s , 
                                         lm.c );
         mprefix : constant String := GS.Image(sender) &
                                      images.Image("> %06d : ",lm.seq ) ; 
      begin
         --Put_Line(mprefix);
         logging.file.SendMessage(logfile.all , mprefix , mimg );
      end ;
   end loop ;
end Dglogs;