adacl_5.15.1_e7c1515b/src/adacl-trace.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
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 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
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
-------------------------------------------------------------- {{{1 ----------
--: Copyright © 2007 … 2023 Martin Krischik «krischik@users.sourceforge.net»
-------------------------------------------------------------------------------
--: This library is free software; you can redistribute it and/or modify it
--: under the terms of the GNU Library General Public License as published by
--: the Free Software Foundation; either version 2 of the License, or (at your
--: option) any later version.
--:
--: This library 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 Library General Public
--: License for more details.
--:
--: You should have received a copy of the GNU Library General Public License
--: along with this library; if not, write to the Free Software Foundation,
--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--------------------------------------------------------------- }}}1 ----------

pragma License (Modified_Gpl);
pragma Ada_2022;

with Ada.Characters.Conversions;
with Ada.Characters.Handling;
with Ada.Characters.Latin_1;
with Ada.Containers.Hashed_Maps;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Task_Identification;
with Ada.Text_IO;
with AdaCL.Command_Line.GetOpt;
with AdaCL.Strings;
with AdaCL.Strings.Hex;

--
--  @summary
--  Ada Class Library
--  Trace
--
--  @description
--
package body AdaCL.Trace is

   ---------------------------------------------------------------------------

   use type Ada.Strings.Unbounded.Unbounded_String;

   ---------------------------------------------------------------------------

   package Unbounded renames Ada.Strings.Unbounded;
   package Latin_1 renames Ada.Characters.Latin_1;
   package Text_IO renames Ada.Text_IO;

   ---------------------------------------------------------------------------
   function Address_Image is new AdaCL.Strings.Hex.Generic_Image (System.Storage_Elements.Integer_Address);

   ---
   --  Remembers thread ids.
   --
   --: @field Thread_No  Each Thread has a number. A number is shorter then string.
   --: @field Indent     Function indeting is counded separate for every thread
   type Thread_ID is record
      Thread_No : Natural := Natural'First;
      Indent    : Natural := Natural'First;
   end record;

   ---
   --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two adjustments
   --  and two finals are ignored.
   --
   type Repeat_Count is range 0 .. 4;

   ---
   --  Some Container for Thread_IDs
   --
   package Thread_ID_Map is new Ada.Containers.Hashed_Maps
      (Key_Type        => Unbounded.Unbounded_String,
       Element_Type    => Thread_ID,
       Hash            => AdaCL.Strings.Hash,
       Equivalent_Keys => "=",
       "="             => "=");

   ---
   --  Protect all global data.
   --
   protected Cl is
      ---
      --  Initialize Trace.
      --
      procedure Initialize;

      ---
      --  Icrement Trace line counter by one
      --
      procedure Inc_Sequence;
      pragma Inline (Inc_Sequence);

      ---
      --  Get Trace line counter
      --
      function Get_Sequence return Natural;
      pragma Inline (Get_Sequence);

      procedure Set_Filename (New_Filename : in String);
      pragma Inline (Set_Filename);

      ---
      --  Determine the threadId of the current thread
      --
      procedure Get_Thread_ID (Retval : out Thread_ID);

      ---
      --  Determine the threadId of the current thread
      --
      procedure Set_Thread_ID (Element : in Thread_ID);

      ---
      --  Trace is On
      --
      function Get_On return Boolean;
      pragma Inline (Get_On);

      ---
      --  Trace is On
      --
      procedure Set_On (On : Boolean);
      pragma Inline (Set_On);

      ---
      --  Trace is On
      --
      function Get_Verbose return Boolean;
      pragma Inline (Get_Verbose);

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  reset the last function counter and function name.
      --
      procedure Reset_Last_Function (Function_Name : String);

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  Check if counter is greater 0
      --
      function Ignore_Function (Function_Name : String) return Boolean;

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  Decrement counter until it reaches 0
      --
      procedure Decrement_Last_Function;

      ---
      --  Trace is On
      --
      procedure Set_Verbose (Verbose : Boolean);
      pragma Inline (Set_Verbose);

      ---
      --  Trace with line numbers.
      --
      function Get_Write_Line_Number return Boolean;
      pragma Inline (Get_Write_Line_Number);

      ---
      --  Trace with line numbers.
      --
      procedure Set_Write_Line_Number (Write_Line_Number : Boolean);
      pragma Inline (Set_Write_Line_Number);

      ---
      --  Trace with thread profex and optional line numbers.
      --
      function Get_Write_Prefix return Boolean;
      pragma Inline (Get_Write_Prefix);

      ---
      --  Trace with thread profex and optional line numbers.
      --
      procedure Set_Write_Prefix (Write_Prefix : Boolean);
      pragma Inline (Set_Write_Prefix);

      ---
      --  Trace Destination
      --
      function Get_Trace_Location return Destination;
      pragma Inline (Get_Trace_Location);

      ---
      --  Trace Destination
      --
      procedure Set_Trace_Location (Location : in Destination);
      pragma Inline (Set_Trace_Location);

      ---
      --  Write Formated Text
      --
      --: @param Text    Text to be written
      --: @param Marker  Marker to be used
      procedure Write_Formatted_String (Text : in String; Marker : in Character);

      ---
      --  Write Text
      --
      --: @param Text  Text to be written
      procedure Write_String (Text : in String);

   private

      ---
      --  Trace line counter
      --
      Sequence : Natural := Natural'First;

      ---
      --  Filename of Trace if Destination ist File
      --
      Filename : Unbounded.Unbounded_String := Unbounded.To_Unbounded_String ("AdaCL-Trace.log");

      ---
      --  The original IBM design opened and closed the File all the time. However, Ada.Text_IO won't allow that and of
      --  course, it is slow.
      --
      Filehandle : Text_IO.File_Type;

      ---
      --  Last Thread ID used
      --
      Thread_No : Natural := Natural'First;

      ---
      --  Current Indenting Level for each thread
      --
      Threads : Thread_ID_Map.Map;

      ---
      --  Trace Destination
      --
      Location : Destination := Standard_Error;

      ---
      --  Trace is On
      --
      On : Boolean := False;

      ---
      --  Trace with line numbers.
      --
      Write_Line_Number : Boolean := True;

      ---
      --  Trace with thread profex and optional line numbers.
      Write_Prefix : Boolean := True;

      ---
      --   Verbose operation.
      Verbose : Boolean := False;

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  Counter to count how often Adjust was called.
      --
      Repeat_Counter : Repeat_Count := 0;

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  remembers the last function name traced
      --
      Last_Function : Unbounded.Unbounded_String := Unbounded.Null_Unbounded_String;

   end Cl;

   ---------------------------------------------------------------------------
   --
   --  Indent Level
   --
   Indent_Level : constant Natural := 2;
   --
   --  Commandline options
   --
   Trace_Verbose : constant String := "verbose";

   Trace_Opt          : constant String := "TRACE";
   Trace_Opt_On       : constant String := "ON";
   Trace_Opt_NoPrefix : constant String := "NOPREFIX";

   Trace_Opt_To        : constant String := "TRACETO";
   Trace_Opt_To_Err1   : constant String := "STDERR";
   Trace_Opt_To_Err2   : constant String := "ERR";
   Trace_Opt_To_Std1   : constant String := "STDOUT";
   Trace_Opt_To_Std2   : constant String := "OUT";
   Trace_Opt_To_File   : constant String := "FILE";
   Trace_Opt_To_Queue1 : constant String := "QUEUE";
   Trace_Opt_To_Queue2 : constant String := "PMPRINTF";

   Trace_Opt_File : constant String := "TRACEFILE";

   Marker_Std     : constant Character := Latin_1.Greater_Than_Sign;
   Marker_Special : constant Character := Latin_1.Exclamation;
   Marker_Outdent : constant Character := Latin_1.Minus_Sign;
   Marker_Indent  : constant Character := Latin_1.Plus_Sign;

   ---------------------------------------------------------------------------
   --
   --  Protect all global data.
   --
   protected body Cl is

      ---
      --  Get Trace line counter
      --
      function Get_Sequence return Natural is (Sequence);

      ---
      --  Trace is On
      --
      function Get_On return Boolean is (On);

      ---
      --
      --  Determine the threadId of the current thread
      --
      procedure Get_Thread_ID (Retval : out Thread_ID) is
         use Ada.Strings.Unbounded;
         use Thread_ID_Map;
         use Ada.Task_Identification;

         FixThread_ID : constant String           := Image (Current_Task);
         StrThread_ID : constant Unbounded_String := To_Unbounded_String (FixThread_ID);
      begin
         if Contains (Container => Threads, Key => StrThread_ID) then
            Retval := Element (Container => Threads, Key => StrThread_ID);
         else
            Retval :=
               Thread_ID'
                  (Thread_No => Thread_No,
                   Indent    => 0);
            Insert
               (Container => Threads,
                Key       => StrThread_ID,
                New_Item  => Retval);

            Thread_No := Natural'Succ (Thread_No);

            if On then
               Write_Formatted_String (Text => "New Thread : " & FixThread_ID, Marker => Marker_Special);
            end if;
         end if;
      end Get_Thread_ID;

      ---
      --  Trace Destination
      --
      function Get_Trace_Location return Destination is (Location);

      ---
      --  Trace with line numbers.
      --
      function Get_Write_Line_Number return Boolean is (Write_Line_Number);

      ---
      --  Trace with thread profex and optional line numbers.
      --
      function Get_Write_Prefix return Boolean is (Write_Prefix);

      ---
      --  Trace is On
      --
      function Get_Verbose return Boolean is (Verbose);

      ---
      --
      --  Icrement Sequence by one
      --
      procedure Inc_Sequence is
      begin
         Sequence := Natural'Succ (Sequence);
      end Inc_Sequence;

      ---
      --
      --  Initialize Trace.
      --
      procedure Initialize is
         use AdaCL.Command_Line.GetOpt;

         Options : AdaCL.Command_Line.GetOpt.Object;
         Found   : FoundFlag;
      begin
         Options.Set_Pattern (":" & Trace_Verbose (1));
         Options.Set_ExceptionOnError (False);
         Options.Set_ExtractGNU (True);

         ParseCL :
         loop
            Options.Next (Found);

            --  To trace the trace ;-)
            --
            --: Text_IO.Put_Line (Text_IO.Standard_Error, Options'Image);
            --: Text_IO.Put_Line (Text_IO.Standard_Error, Found'Image);

            exit ParseCL when Found = EndOfOptions;

            if Found = GNU_Style then
               Analyze_GNU :
               declare
                  Option   : constant String := Options.Get_GNUOption;
                  Argument : constant String := Options.Get_Argument;
               begin
                  if Option = Trace_Opt then
                     if Argument = Trace_Opt_On then
                        On := True;
                     elsif Argument = Trace_Opt_NoPrefix then
                        On                := True;
                        Write_Prefix      := False;
                        Write_Line_Number := False;
                     end if;
                  elsif Option = Trace_Opt_To then
                     if Argument = Trace_Opt_To_Err1 or else Argument = Trace_Opt_To_Err2 then
                        Location := Standard_Error;
                     elsif Argument = Trace_Opt_To_Std1 or else Argument = Trace_Opt_To_Std2 then
                        Location := Standard_Output;
                     elsif Argument = Trace_Opt_To_File then
                        Location := File;
                     elsif Argument = Trace_Opt_To_Queue1 or else Argument = Trace_Opt_To_Queue2 then
                        Location := Queue;
                     end if;
                  elsif Option = Trace_Opt_File then
                     if Argument'Length > 0 then
                        Set_Filename (Argument);
                     end if;
                  elsif Option = Trace_Verbose then
                     Verbose := True;
                  end if;
               end Analyze_GNU;
            elsif Found = WithoutArgument then
               Analyze_Without :
               declare
                  Option : constant Character := Options.Get_Option;
               begin
                  if Option = Trace_Verbose (1) then
                     Verbose := True;
                  end if;
               end Analyze_Without;
            end if;
         end loop ParseCL;
      end Initialize;

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  reset the last function counter and function name.
      --
      procedure Reset_Last_Function (Function_Name : String) is
      begin
         Repeat_Counter := Repeat_Count'Last;
         Last_Function  := Unbounded.To_Unbounded_String (Function_Name);
      end Reset_Last_Function;

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  Check if counter is greater 0
      --
      function Ignore_Function (Function_Name : String) return Boolean is
         (Repeat_Counter > 0 and then Last_Function = Function_Name);

      ---
      --  The controlled object used in Function_Trace is adjusted multiple times. To get a better output two
      --  adjustments are ignored.
      --
      --  Decrement counter until it reaches 0
      --
      procedure Decrement_Last_Function is
      begin
         if Repeat_Counter > 0 then
            Repeat_Counter := @ - 1;
         end if;

         if Repeat_Counter = 0 then
            Last_Function := Unbounded.Null_Unbounded_String;
         end if;
      end Decrement_Last_Function;

      ---
      --  Set Filename for Trace File
      --
      procedure Set_Filename (New_Filename : in String) is
      begin
         if Text_IO.Is_Open (Filehandle) then
            Text_IO.Close (Filehandle);
         end if;

         Filename := Unbounded.To_Unbounded_String (New_Filename);
      end Set_Filename;

      ---
      --  Trace is On
      --
      procedure Set_On (On : Boolean) is
      begin
         Cl.On := On;
      end Set_On;

      ---
      --  Determine the threadId of the current thread
      --
      procedure Set_Thread_ID (Element : in Thread_ID) is
         use Ada.Strings.Unbounded;
         use Thread_ID_Map;
         use Ada.Task_Identification;

         FixThread_ID : constant String           := Image (Current_Task);
         StrThread_ID : constant Unbounded_String := To_Unbounded_String (FixThread_ID);
      begin
         if Contains (Container => Threads, Key => StrThread_ID) then
            Replace
               (Container => Threads,
                Key       => StrThread_ID,
                New_Item  => Element);
         else
            Insert
               (Container => Threads,
                Key       => StrThread_ID,
                New_Item  => Element);
         end if;
      end Set_Thread_ID;

      ---
      --  Trace Destination
      --
      procedure Set_Trace_Location (Location : in Destination) is
      begin
         Cl.Location := Location;
      end Set_Trace_Location;

      ---
      --  Trace is On
      --
      procedure Set_Verbose (Verbose : Boolean) is
      begin
         Cl.Verbose := Verbose;
      end Set_Verbose;

      ---
      --  Trace with line numbers.
      --
      procedure Set_Write_Line_Number (Write_Line_Number : Boolean) is
      begin
         Cl.Write_Line_Number := Write_Line_Number;
      end Set_Write_Line_Number;

      ---
      --  Trace with thread profex and optional line numbers.
      --
      procedure Set_Write_Prefix (Write_Prefix : Boolean) is
      begin
         Cl.Write_Prefix := Write_Prefix;
      end Set_Write_Prefix;

      ---
      --  Write Formated Text
      --
      --: @param Text    Text to be written
      --: @param Marker  Marker to be used
      procedure Write_Formatted_String (Text : in String; Marker : in Character) is
         use Ada.Strings.Unbounded;

         Thread : Thread_ID;
      begin
         Get_Thread_ID (Thread);

         if Marker = Marker_Outdent and then Thread.Indent >= Indent_Level then
            Thread.Indent := Thread.Indent - Indent_Level;
         end if;

         Format :
         declare
            StrOut    : Unbounded_String := To_Unbounded_String (Marker & ' ' & Text);
            StrPrefix : Unbounded_String := Thread.Indent * ' ';
            StrLF     : constant String  := [1 => Latin_1.LF];
         begin
            if Write_Prefix then
               Prefix :
               declare
                  use Ada.Strings.Fixed;

                  StrThread_ID : constant String := Head (Thread.Thread_No'Image, 5);
                  StrLineNo    : constant String := Head (Get_Sequence'Image, 5);
               begin
                  StrPrefix := StrLineNo & ":" & StrThread_ID & ":" & StrPrefix;
               end Prefix;
            end if;

            AdaCL.Strings.Append_All
               (Source   => StrOut,
                Search   => StrLF,
                New_Item => To_String (StrPrefix),
                Mapping  => Ada.Strings.Maps.Identity);
            StrOut := StrPrefix & StrOut;

            Write_String (To_String (StrOut));
         end Format;

         Inc_Sequence;

         if Marker = Marker_Indent then
            Thread.Indent := Thread.Indent + Indent_Level;
         end if;

         Set_Thread_ID (Thread);

      end Write_Formatted_String;

      ---
      --  Write Text
      --
      --: @param Text to be written
      procedure Write_String (Text : in String) is
         use Ada.Text_IO;
      begin
         case Location is
            when Queue =>
               null;
            when Standard_Error =>
               Put_Line (Standard_Error, Text);
            when Standard_Output =>
               Put_Line (Standard_Output, Text);
            when File =>
               if not Is_Open (Filehandle) then
                  Create
                     (File => Filehandle,
                      Mode => Out_File,
                      Name => Unbounded.To_String (Filename),
                      Form => "shared=yes");
               end if;

               Put_Line (Filehandle, Text);
               Flush (Filehandle);
         end case;
      end Write_String;
   end Cl;

   ---
   --  Copy Instanz.
   --
   --: @param This   Object itself.
   overriding procedure Adjust (This : in out Object) is
   begin
      if Is_Trace_Enabled then
         if Cl.Ignore_Function (This.Trace_Name) then
            Cl.Decrement_Last_Function;
         else
            Cl.Write_Formatted_String (Text => This.Trace_Name, Marker => Marker_Indent);
         end if;
      end if;
   end Adjust;

   ---
   --  Report_Assertion a Condition. If the condition is not true create a trace entry describing the assertion and
   --  then raise an exception.
   --
   --: @param Condition  Condition which should be true
   --: @param Raising    Exeption which is raised
   --: @param Message    Free form Message
   --: @param Entity     Location destriptor.
   --: @param Source     Location destriptor.
   procedure Report_Assertion
      (Condition : in Boolean;
       Raising   : in Ada.Exceptions.Exception_Id := Ada.Assertions.Assertion_Error'Identity;
       Message   : in String                      := "No Message given.";
       Entity    : in String                      := GNAT.Source_Info.Enclosing_Entity;
       Source    : in String                      := GNAT.Source_Info.Source_Location)
   is
   begin
      if not Condition then
         Raise_Exception
            (Raising => Raising,
             Message => Message,
             Entity  => Entity,
             Source  => Source);
      end if;
   end Report_Assertion;

   ---
   --  Report_Assertion a Condition. If the condition is not true create a trace entry describing the assertion and
   --  then raise an exception.
   --
   --: @param Condition  Condition which should be true
   --: @param Message    Free form Message
   --: @param Source     Filename.
   --: @param Line       Line number.
   procedure Report_Assertion
      (Condition : Boolean;
       Message   : String;
       Source    : String  := GNAT.Source_Info.File;
       Line      : Natural := GNAT.Source_Info.Line)
   is
   begin
      if not Condition then
         Raise_Exception
            (Raising => Ada.Assertions.Assertion_Error'Identity,
             Message => Message,
             Source  => Source,
             Line    => Line);
      end if;
   end Report_Assertion;

   ---
   --  Enable Trace
   --
   procedure Disable_Trace is
   begin
      Cl.Set_On (False);
   end Disable_Trace;

   ---
   --  Enable Trace
   --
   procedure Disable_Verbose is
   begin
      Cl.Set_Verbose (False);
   end Disable_Verbose;

   ---
   --  Don't Write Line numbers
   --
   procedure Disable_Write_Line_Number is
   begin
      Cl.Set_Write_Line_Number (False);
   end Disable_Write_Line_Number;

   ---
   --  Disable the Write prefix
   --
   procedure Disable_Write_Prefix is
   begin
      Cl.Set_Write_Prefix (False);
   end Disable_Write_Prefix;

   ---
   --  Enable Trace
   --
   procedure Enable_Trace is
   begin
      Cl.Set_On (True);
   end Enable_Trace;

   ---
   --  Enable Trace
   --
   procedure Enable_Verbose is
   begin
      Cl.Set_Verbose (True);
   end Enable_Verbose;

   ---
   --  Write Line numbers
   --
   procedure Enable_Write_Line_Number is
   begin
      Cl.Set_Write_Line_Number (True);
   end Enable_Write_Line_Number;

   ---
   --  Enable the Write prefix
   --
   procedure Enable_Write_Prefix is
   begin
      Cl.Set_Write_Prefix (True);
   end Enable_Write_Prefix;

   ---
   --  Trace end of function
   --
   --: @param This  Object itself.
   overriding procedure Finalize (This : in out Object) is
   begin
      if Is_Trace_Enabled then
         if Cl.Ignore_Function (This.Trace_Name) then
            Cl.Decrement_Last_Function;
         else
            Cl.Write_Formatted_String (Text => This.Trace_Name, Marker => Marker_Outdent);
         end if;
      end if;
   end Finalize;

   ---
   --  Functrace is not quite as usefull as the C++ version. The reason are the missing constructors and destructors in
   --  Ada. With Controlled types you can't limit to just one call to Initialize and one to Finalize There are allways
   --  some extra Adjust with matching. Finalize.
   --
   --: @param Name  Name of the function calls to be traced.
   function Function_Trace (Name : String) return Object is
      Retval : constant Object (Name'Length) :=
         (Base.Object with Name_Length => Name'Length,
          Trace_Name                   => Name);
   begin
      --
      --  The Initialize method is not realy a replacement for a proper contructor.
      --
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Retval.Trace_Name, Marker => Marker_Indent);
         Cl.Reset_Last_Function (Name);
      end if;

      return Retval;
   end Function_Trace;

   ---
   --  check is trace is Enabled
   --
   function Is_Trace_Enabled return Boolean is (Cl.Get_On);

   ---
   --  check is trace is Enabled
   --
   function Is_Verbose_Enabled return Boolean is (Cl.Get_Verbose);

   ---
   --  check if Line numbers are written
   --
   function Is_Write_Line_Number_Enabled return Boolean is (Cl.Get_Write_Line_Number);

   ---
   --  Check the Write prefix flag
   --
   function Is_Write_Prefix_Enabled return Boolean is (Cl.Get_Write_Prefix);

   ---
   --  Trace the given exception details and then raise the exception.
   --
   --: @param Raising  Exception which is raised
   --: @param Message  Free form Message
   --: @param Entity   Location descriptor. Suggested content: AdaCL.Trace.Entity
   --: @param Source   Location descriptor. Suggested content: AdaCL.Trace.Source
   procedure Raise_Exception
      (Raising : in Ada.Exceptions.Exception_Id := Ada.Assertions.Assertion_Error'Identity;
       Message : in String                      := "No Message given";
       Entity  : in String                      := GNAT.Source_Info.Enclosing_Entity;
       Source  : in String                      := GNAT.Source_Info.Source_Location)
   is
      use Ada.Exceptions;
   begin
      Write ("Raise Exception " & Exception_Name (Raising));
      Write ("   with Message " & Message);
      Write ("   for Entity   " & Entity);
      Write ("   in Source    " & Source);

      Raise_Exception (E => Raising, Message => Message & " Entity :" & Entity & "." & " Source :" & Source & ".");
   end Raise_Exception;

   ---
   --  Trace the given exception details and then raise the exception.
   --
   --: @param Raising  Exception which is raised Message : Free form Message
   --: @param Message  Message to print to trace
   --: @param Source   Filename.
   --: @param Line     Line number.
   procedure Raise_Exception
      (Raising : in Ada.Exceptions.Exception_Id := Ada.Assertions.Assertion_Error'Identity;
       Message : in String                      := "No Message given";
       Source  : in String                      := GNAT.Source_Info.File;
       Line    : in Natural                     := GNAT.Source_Info.Line)
   is
      use Ada.Exceptions;
   begin
      Write ("Raise Exception " & Exception_Name (Raising));
      Write ("   with Message " & Message);
      Write ("   in File      " & Source);
      Write ("   in Line      " & Line'Image);

      Raise_Exception (E => Raising, Message => Message & "." & " Source :" & Source & " Line :" & Line'Image & ".");
   end Raise_Exception;

   ---
   --  Check the Trace Destination
   --
   function Trace_Destination return Destination is (Cl.Get_Trace_Location);

   ---
   --  Write an IString using Write_Formatted_String after adding the appropriate padding for indentation.
   --
   --: @param Text  String to be written
   procedure Write (Text : in String) is
   begin
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Text, Marker => Marker_Std);
      end if;
   end Write;

   ---
   --  Write an Address.
   --
   --: @param Text  String to be written
   procedure Write (Text : in String; An_Address : in System.Address) is
   begin
      if Is_Trace_Enabled then
         Write_Address :
         declare
            Address_Text : constant String := Address_Image (System.Storage_Elements.To_Integer (An_Address));
         begin
            Cl.Write_Formatted_String (Text => Text & ' ' & Address_Text, Marker => Marker_Std);
         end Write_Address;
      end if;
   end Write;

   ---
   --  Write an IString using Write_Formatted_String after adding the appropriate padding for indentation.
   --
   --: @param Text  String to be written
   procedure Write (Text : in Unbounded.Unbounded_String) is
      use Ada.Strings.Unbounded;
   begin
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => To_String (Text), Marker => Marker_Std);
      end if;
   end Write;

   ---
   --  Write an Exception to the Trace
   --
   --: @param An_Exception  String to be written
   procedure Write (An_Exception : in Ada.Exceptions.Exception_Occurrence) is
      use Ada.Exceptions;
   begin
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Exception_Information (An_Exception), Marker => Marker_Special);
         --              Cl.Write_Formatted_String (Text   =>
         --  G_TB.Symbolic_Traceback (An_Exception),
         --                                         Marker => Marker_Special);
      end if;
   end Write;

   ---
   --  Write an Exception to the Trace
   --
   --: @param An_Exception  String to be written
   --: @param Entity     Procedure in which the exception was caught
   --: @param Source      Source File in which Entity is located.
   procedure Write
      (An_Exception : in Ada.Exceptions.Exception_Occurrence;
       Entity       : in String;
       Source       : in String)
   is
      use Ada.Exceptions;
   begin
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Exception_Information (An_Exception), Marker => Marker_Special);
         Cl.Write_Formatted_String (Text => "Function: " & Entity, Marker => Marker_Special);
         Cl.Write_Formatted_String (Text => "Source: " & Source, Marker => Marker_Special);
         --       Cl.Write_Formatted_String (
         --          Text   => G_TB.Symbolic_Traceback (An_Exception),
         --          Marker => Marker_Special);
      end if;
   end Write;

   ---
   --  Write Help for Commandline Options parsed from Trace
   --
   procedure Write_Commandline_Help is
      use AdaCL.Command_Line.GetOpt;
   begin
      Text_IO.New_Line;
      Text_IO.Put_Line ("Trace options:");
      Text_IO.New_Line;
      Put_Help_Line (Trace_Verbose (1), Trace_Verbose, "verbose operation.");
      Put_Help_Line (Trace_Opt, Trace_Opt_On, "activate trace.");
      Put_Help_Line (Trace_Opt, Trace_Opt_NoPrefix, "activate trace without prefix.");
      Text_IO.New_Line;
      Put_Help_Line (Trace_Opt_To, Trace_Opt_To_Err1, "trace in stderr.");
      Put_Help_Line (Trace_Opt_To, Trace_Opt_To_Std1, "trace in stdout.");
      Put_Help_Line (Trace_Opt_To, Trace_Opt_To_File, "trace to file.");
      Text_IO.New_Line;
      Put_Help_Line (Trace_Opt_File, "Filename", "trace file.");
      Text_IO.New_Line;
   end Write_Commandline_Help;

   ---
   --  Create a memory dump
   --
   --  String to be written
   procedure Write_Dump (An_Address : in System.Address; Size_In_Byte : in System.Storage_Elements.Storage_Count) is
      use System.Storage_Elements;
   begin
      Write ("Address: ", An_Address);
      Write ("Lenght : " & Size_In_Byte'Image);

      if Is_Trace_Enabled then
         Dump :
         declare
            function Element_Image is new AdaCL.Strings.Hex.Generic_Image (System.Storage_Elements.Storage_Element);

            use Ada.Strings.Fixed;

            Data : Storage_Array (0 .. Size_In_Byte - 1);
            for Data'Address use An_Address;
            pragma Import (Ada, Data);

            Line_Len     : constant := 16;
            Byte_Len     : constant := 2;
            Address_Len  : constant := 16;
            Byte_Offset  : constant := 10 + Address_Len + 5;   --  «Dump   : [00007FF7B3740130] : »
            ASCII_Offset : constant := Byte_Offset + Line_Len * (Byte_Len + 1) + 1;
            Text_Len     : constant := ASCII_Offset + Line_Len;

            Text     : String (1 .. Text_Len);
            Line     : Storage_Offset := Data'First;
            Col      : Storage_Offset := Data'First;
            Char     : Character;
            Byte_Col : Integer;
         begin
            Dump_Line :
            while Line <= Data'Last loop
               declare
                  Address_Text : constant String := Address_Image (To_Integer (An_Address + Line));
               begin
                  Move (Source => "Dump   : [" & Address_Text & "] : ", Target => Text);
               end;

               Col      := 0;
               Byte_Col := Byte_Offset;

               Dump_Column :
               while Col < Line_Len and then Col + Line < Size_In_Byte loop
                  declare
                     Byte_Text : constant String := Element_Image (Data (Line + Col));
                  begin
                     Text (Byte_Col .. Byte_Col + 1) := Byte_Text;
                  end;

                  Char := Character'Val (Data (Line + Col));

                  if Ada.Characters.Handling.Is_Graphic (Char) then
                     Text (Natural (ASCII_Offset + Col)) := Char;
                  else
                     Text (Natural (ASCII_Offset + Col)) := '.';
                  end if;

                  Col      := Col + 1;
                  Byte_Col := Byte_Col + (Byte_Len + 1);
               end loop Dump_Column;

               Cl.Write_Formatted_String (Text => Text, Marker => Marker_Std);
               Line := Line + Line_Len;
            end loop Dump_Line;
         end Dump;
      end if;
   end Write_Dump;

   ---
   --  Create a memory dump. This Dump takes size in bits.
   --
   --: @param An_Address   String to be written
   --: @param Size_In_Bits Size in Storage_Elements.
   procedure Write_Dump (An_Address : in System.Address; Size_In_Bits : in Integer) is
      use System.Storage_Elements;

      Size : Storage_Count := Storage_Count (Size_In_Bits / System.Storage_Unit);
   begin
      if (Size_In_Bits mod System.Storage_Unit) /= 0 then
         Size := Size + 1;
      end if;

      Write_Dump (An_Address, Size);
   end Write_Dump;

   ---
   --  Write an IString using Write_Formatted_String after adding the appropriate padding for indentation.
   --
   --: @param Text  String to be written
   procedure Write_Error (Text : in String) is
   begin
      if not Is_Trace_Enabled or else Trace_Destination /= Standard_Error then
         Text_IO.Put_Line (Text_IO.Standard_Error, Text);
      end if;

      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Text, Marker => Marker_Std);
      end if;
   end Write_Error;

   ---
   --  Write an IString using Write_Formatted_String after adding the appropriate padding for indentation.
   --
   --: @param Text  String to be written
   procedure Write_Error (Text : in Unbounded.Unbounded_String) is
      use Ada.Strings.Unbounded;
   begin
      if not Is_Trace_Enabled or else Trace_Destination /= Standard_Error then
         Text_IO.Put_Line (Text_IO.Standard_Error, To_String (Text));
      end if;

      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => To_String (Text), Marker => Marker_Std);
      end if;
   end Write_Error;

   ---
   --  Write an Exception to the Trace
   --
   --: @param An_Exception   String to be written
   procedure Write_Error (An_Exception : in Ada.Exceptions.Exception_Occurrence) is
      use Ada.Exceptions;
   begin
      if not Is_Trace_Enabled or else Trace_Destination /= Standard_Error then
         Text_IO.Put_Line (Text_IO.Standard_Error, Exception_Information (An_Exception));
         --              Text_IO.Put_Line (Text_IO.Standard_Error,
         --  G_TB.Symbolic_Traceback (An_Exception));
      end if;

      Write (An_Exception);
   end Write_Error;

   ---
   --  Write an Exception to the Trace
   --
   --: @param An_Exception String to be written
   --: @param Entity       Procedure in which the exception was caught
   --: @param Source       Source File in which Entity is located.
   procedure Write_Error
      (An_Exception : in Ada.Exceptions.Exception_Occurrence;
       Entity       : in String;
       Source       : in String)
   is
      use Ada.Exceptions;
   begin
      if not Is_Trace_Enabled or else Trace_Destination /= Standard_Error then
         Text_IO.New_Line (Text_IO.Standard_Error);
         Text_IO.Put (Text_IO.Standard_Error, Exception_Information (An_Exception));
         Text_IO.Put_Line (Text_IO.Standard_Error, "Function: " & Entity);
         Text_IO.Put_Line (Text_IO.Standard_Error, "Source: " & Source);
         --              Text_IO.Put_Line (Text_IO.Standard_Error,
         --  G_TB.Symbolic_Traceback (An_Exception));
      end if;

      Write
         (An_Exception => An_Exception,
          Entity       => Entity,
          Source       => Source);
   end Write_Error;

   ---
   --  When verbose is aktivated then an empty line is written to Standart_Output
   --
   procedure Write_Info is
   begin
      if Is_Verbose_Enabled then
         Text_IO.New_Line (Text_IO.Standard_Output);
      end if;
   end Write_Info;

   ---
   --  Write an IString using writeFormattedString after adding the appropriate padding for indentation.
   --
   --  When verbose is aktivated then the string is written to Standart_Output as well.
   --
   --: @param Text  String to be written
   procedure Write_Info (Text : in String) is
   begin
      if Is_Verbose_Enabled and then (not Is_Trace_Enabled or else Trace_Destination /= Standard_Output) then
         Text_IO.Put_Line (Text_IO.Standard_Output, Text);
      end if;

      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Text, Marker => Marker_Std);
      end if;
   end Write_Info;

   ---
   --  When verbose is aktivated then the character is written to Standart_Output.
   --
   --: @param Text  character to be written
   procedure Write_Info (Text : in Character) is
   begin
      if Is_Verbose_Enabled then
         Text_IO.Put (Text_IO.Standard_Output, Text);
      end if;
   end Write_Info;

   ---
   --  Write an IString using writeFormattedString after adding the appropriate padding for indentation.
   --
   --  When verbose is aktivated then the string is written to Standart_Output as well.
   --
   --: @param Text  String to be written
   procedure Write_Info (Text : in Unbounded.Unbounded_String) is
      use Ada.Strings.Unbounded;
   begin
      if Is_Verbose_Enabled and then (not Is_Trace_Enabled or else Trace_Destination /= Standard_Output) then
         Text_IO.Put_Line (Text_IO.Standard_Output, To_String (Text));
      end if;

      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => To_String (Text), Marker => Marker_Std);
      end if;
   end Write_Info;

   ---
   --  Write to queue - not supported yet.
   --
   procedure Write_To_File is
   begin
      Cl.Set_Trace_Location (File);
   end Write_To_File;

   ---
   --  Set Filename for Trace File
   --
   procedure Write_To_File (New_Filename : in String) is
   begin
      Cl.Set_Filename (New_Filename);
      Cl.Set_Trace_Location (File);
   end Write_To_File;

   ---
   --  Write to queue - not supported yet.
   --
   procedure Write_To_Queue is
   begin
      Cl.Set_Trace_Location (Queue);
   end Write_To_Queue;

   ---
   --  Write to Standart Error
   --
   procedure Write_To_Standard_Error is
   begin
      Cl.Set_Trace_Location (Standard_Error);
   end Write_To_Standard_Error;

   ---
   --  Write to Standart Error
   --
   procedure Write_To_Standard_Output is
   begin
      Cl.Set_Trace_Location (Standard_Output);
   end Write_To_Standard_Output;

   ---
   --  Write an string using Write_Formatted_String after adding the appropriate padding for indentation.
   --
   --: @param Text  String to be written
   procedure Write_Wide (Text : in Wide_String) is
   begin
      if Is_Trace_Enabled then
         Cl.Write_Formatted_String (Text => Ada.Characters.Conversions.To_String (Text), Marker => Marker_Std);
      end if;
   end Write_Wide;

begin
   Cl.Initialize;
   pragma Debug (Enable_Trace);
   pragma Debug (Write_To_File);
end AdaCL.Trace;

---------------------------------------------------------------- {{{ ----------
--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
--: vim: set spell spelllang=en_gb