polyorb_hi-output.adb 7.06 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
--                                                                          --
-- PolyORB-HI is free software; you can redistribute it and/or modify under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. 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.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31 32 33 34 35 36 37
--                                                                          --
------------------------------------------------------------------------------

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

package body PolyORB_HI.Output is
38

39 40
   use Ada.Real_Time;

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

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

47 48 49 50 51 52 53 54 55 56
   --  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);
57

58 59 60 61 62 63
   end Output_Lock;

   package body Output_Lock is
      pragma SPARK_MOde (Off);

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

67 68 69 70
      procedure Put (Text : in String);

      procedure Put_Line (Text : in String);

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

      protected body Lock is

         --------------
         -- Put_Line --
         --------------
80

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

86 87 88 89 90 91 92 93 94
         ---------
         -- Put --
         ---------

         procedure Put (Text : in String) is
         begin
            Unprotected_Put (Text);
         end Put;
      end Lock;
95 96 97

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

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

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

   --------------
   -- 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
127
      Output_Lock.Put_Line (Text);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
   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
149
      Output_Lock.Put (Text);
150 151 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
   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 ("[");
177 178
      PolyORB_HI.Output_Low_Level.Put
        (Duration'Image (To_Duration (Elapsed * 1000)));
179 180 181 182 183 184 185 186
      PolyORB_HI.Output_Low_Level.Put ("] ");
      PolyORB_HI.Output_Low_Level.Put (Text);
   end Unprotected_Put;

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

187 188 189
   subtype Output_Position is Positive range 1 .. 48;

   subtype Output_Line is String (Output_Position);
190

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

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

214
      Put_Line (Mode,  Output);
215 216
   end Dump;

217 218 219 220 221
   ---------
   -- "+" --
   ---------

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

      return S;
   end "+";

230
end PolyORB_HI.Output;