polyorb_hi-output.adb 7.24 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-2017 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                                                                          --
-- 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
pragma SPARK_MOde (Off);

34 35
with PolyORB_HI.Output_Low_Level;
with PolyORB_HI.Suspenders;
36 37
pragma Elaborate_All (PolyORB_HI.Suspenders);

38 39 40 41
with Ada.Real_Time;
with System;

package body PolyORB_HI.Output is
42

43 44
   use Ada.Real_Time;

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

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

51 52 53 54 55 56 57 58 59 60
   --  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);
61

62 63 64 65 66
   end Output_Lock;

   package body Output_Lock is

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

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

      procedure Put_Line (Text : in String);

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

      protected body Lock is

         --------------
         -- Put_Line --
         --------------
83

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

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

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

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

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

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

   --------------
   -- 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
130
      Output_Lock.Put_Line (Text);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
   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
152
      Output_Lock.Put (Text);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
   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
170
      Start_Time : Time renames PolyORB_HI.Suspenders.System_Startup_Time;
171 172 173 174 175 176 177 178 179
      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 ("[");
180 181 182 183
      --  XXX The following is disabled as some cross-runtime do not have
      --  the capability to build Duration'Image
      --      PolyORB_HI.Output_Low_Level.Put
      --        (Duration'Image (To_Duration (Elapsed * 1000)));
184 185 186 187 188 189 190 191
      PolyORB_HI.Output_Low_Level.Put ("] ");
      PolyORB_HI.Output_Low_Level.Put (Text);
   end Unprotected_Put;

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

192 193 194
   subtype Output_Position is Positive range 1 .. 48;

   subtype Output_Line is String (Output_Position);
195

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

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

219
      Put_Line (Mode,  Output);
220 221
   end Dump;

222 223 224 225 226
   ---------
   -- "+" --
   ---------

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

      return S;
   end "+";

235
end PolyORB_HI.Output;