polyorb_hi-output.adb 7.22 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--                    P O L Y O R B _ H I . O U T P U T                     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
--                                                                          --
-- PolyORB HI is free software; you  can  redistribute  it and/or modify it --
-- under terms of the GNU General Public License as published by the Free   --
-- Software Foundation; either version 2, or (at your option) any later.    --
-- PolyORB HI is distributed  in the hope that it will be useful, but       --
-- WITHOUT ANY WARRANTY;  without even the implied warranty of              --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public  License  distributed with PolyORB HI; see file       --
-- COPYING. If not, write  to the Free  Software Foundation, 51 Franklin    --
-- Street, Fifth Floor, Boston, MA 02111-1301, USA.                         --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
jhugues's avatar
jhugues committed
29 30
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
31 32 33 34 35 36 37 38 39
--                                                                          --
------------------------------------------------------------------------------

with PolyORB_HI.Output_Low_Level;
with PolyORB_HI.Suspenders;
with Ada.Real_Time;
with System;

package body PolyORB_HI.Output is
40

41 42
   use Ada.Real_Time;

43 44 45 46 47 48
   procedure Unprotected_Put_Line (Text : in String);
   --  Not thread-safe Put_Line function

   procedure Unprotected_Put (Text : in String);
   --  Not thread-safe Put function

49 50 51 52 53 54 55 56 57 58
   --  This package encapsulates specific elements to protect against
   --  race condition on the output buffer. It is in a package to
   --  abide with SPARK restrictions.

   package Output_Lock is

      procedure Put_Line (Text : in String);
      --  As above but always displays the message

      procedure Put (Text : in String);
59

60 61 62 63 64 65
   end Output_Lock;

   package body Output_Lock is
      pragma SPARK_MOde (Off);

      protected Lock is
66 67 68
      --  This lock has been defined to guarantee thread-safe output
      --  display

69 70 71 72
      procedure Put (Text : in String);

      procedure Put_Line (Text : in String);

73 74 75 76 77 78 79 80 81
      private
         pragma Priority (System.Priority'Last);
      end Lock;

      protected body Lock is

         --------------
         -- Put_Line --
         --------------
82

83 84 85 86
         procedure Put_Line (Text : in String) is
         begin
            Unprotected_Put_Line (Text);
         end Put_Line;
87

88 89 90 91 92 93 94 95 96
         ---------
         -- Put --
         ---------

         procedure Put (Text : in String) is
         begin
            Unprotected_Put (Text);
         end Put;
      end Lock;
97 98 99

      procedure Put_Line (Text : in String) is
      begin
100
         Lock.Put_Line (Text);
101 102 103 104
      end Put_Line;

      procedure Put (Text : in String) is
      begin
105
         Lock.Put (Text);
106
      end Put;
107 108

   end Output_Lock;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

   --------------
   -- Put_Line --
   --------------

   procedure Put_Line (Mode : in Verbosity := Normal; Text : in String) is
   begin
      pragma Warnings (Off);
      --  Disable warnings on "Condition always true/false" because
      --  Current_Mode is a constant.

      if Mode >= Current_Mode then
         pragma Warnings (On);
         Put_Line (Text);
      end if;

   end Put_Line;

   procedure Put_Line (Text : in String) is
   begin
129
      Output_Lock.Put_Line (Text);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
   end Put_Line;

   ---------
   -- Put --
   ---------

   procedure Put (Mode : in Verbosity := Normal; Text : in String) is
   begin
      pragma Warnings (Off);
      --  Disable warnings on "Condition always true/false" because
      --  Current_Mode is a constant.

      if Mode >= Current_Mode then
         pragma Warnings (On);
         Put (Text);
      end if;

   end Put;

   procedure Put (Text : in String) is
   begin
151
      Output_Lock.Put (Text);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
   end Put;

   --------------------------
   -- Unprotected_Put_Line --
   --------------------------

   procedure Unprotected_Put_Line (Text : in String) is
   begin
      Unprotected_Put (Text);
      PolyORB_HI.Output_Low_Level.New_Line;
   end Unprotected_Put_Line;

   ---------------------
   -- Unprotected_Put --
   ---------------------

   procedure Unprotected_Put (Text : in String) is
      Start_Time : Time renames  PolyORB_HI.Suspenders.System_Startup_Time;
      Elapsed    : Time_Span;
   begin
      if Start_Time = Time_First then
         Elapsed := To_Time_Span (0.0);
      else
         Elapsed := Clock - Start_Time;
      end if;

      PolyORB_HI.Output_Low_Level.Put ("[");
179 180
      PolyORB_HI.Output_Low_Level.Put
        (Duration'Image (To_Duration (Elapsed * 1000)));
181 182 183 184 185 186 187 188
      PolyORB_HI.Output_Low_Level.Put ("] ");
      PolyORB_HI.Output_Low_Level.Put (Text);
   end Unprotected_Put;

   ----------
   -- Dump --
   ----------

189 190 191
   subtype Output_Position is Positive range 1 .. 48;

   subtype Output_Line is String (Output_Position);
192

193
   Hex : constant array (0 .. 15) of Character := "0123456789ABCDEF";
194 195 196 197 198 199
   Nil : constant Output_Line := (Output_Line'Range => ' ');

   procedure Dump
     (Stream : Stream_Element_Array;
      Mode   : Verbosity            := Verbose)
   is
200
      Index   : Output_Position := Output_Position'First;
201 202 203
      Output  : Output_Line := Nil;
   begin
      for J in Stream'Range loop
204
         if Index + 3 <= Output'Last then
205
            Output (Index)     := ' ';
206 207
            Output (Index + 1) := Hex (Natural (Stream (J) / 16));
            Output (Index + 2) := Hex (Natural (Stream (J) mod 16));
208 209
            Index := Index + 3;
         else
210 211 212 213 214 215
            Put_Line (Mode, Output);
            Index := 1;
            Output := Nil;
         end if;
      end loop;

216
      Put_Line (Mode,  Output);
217 218
   end Dump;

219 220 221 222 223
   ---------
   -- "+" --
   ---------

   function "+" (S1 : String; S2 : String) return String is
224
      S : String (1 .. S1'Length + S2'Length) := (others => ' ');
225 226 227 228 229 230 231
   begin
      S (1 .. S1'Length) := S1 (S1'First .. S1'Last);
      S (S1'Length + 1 .. S'Last) := S2 (S2'First .. S2'Last);

      return S;
   end "+";

232
end PolyORB_HI.Output;