comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: Any Suggestion How To Accomplish A Debug Macro?
Date: Tue, 30 Dec 2014 13:07:41 -0700
Date: 2014-12-30T13:07:41-07:00	[thread overview]
Message-ID: <gCDow.832901$No4.618024@fx19.iad> (raw)
In-Reply-To: <ly61ctqthv.fsf@pushface.org>

On 14-12-30 04:14 AM, Simon Wright wrote:
> Hubert <herrdoktor@fumanchu.com> writes:
>
>> I know there is no such thing as C #define's in Ada, but isn't there a
>> way to make some sort of debug macro?
>> In my C++ code I use a lot of statements like
>>
>> PRINT(DEBUG_CHANNEL, "TEXT" );
>>
>> to print out debug messages and in release code, these defines aren't
>> compiled. Is there any way to achieve something like this in Ada
>> without surrounding it with an IF statement and a boolean flag?
>
> GNAT has pragma Debug[1]. I was always annoyed that this is controlled
> by -gnata, which also controls assertions, but I see there's a pragma
> Debug_Policy - stated to be equivalent to pragma Check_Policy (Debug).
>
> [1] https://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Debugging---A-Special-Case.html
>


Another more portable solution would be to declare a static constant 
somewhere, and use the value of that constant to decide if logging 
should occur. If the compiler/linker supports dead code elimination, 
then the debug code can be eliminated if that variable is set to False.

Eg.

package Debug_Logging is
    Debug_Enabled : constant Boolean := False;  -- Edit this line
    procedure Log(Message : String);
end Debug_Logging;


with Debug_Logging;

procedure Foo is
begin
    -- if statement removed if Debug_Enabled is false
    if Debug_Enabled then
      Log("Entered Foo");
    end if;
end Foo;

This works in GNAT, and might work in other compilers as well. Worst 
case is that the Debug_Enabled boolean get evaluated in multiple places, 
but that overhead of evaluating a Boolean might still be acceptable for 
a compiler that doesn't do dead code elimination.

Brad

  parent reply	other threads:[~2014-12-30 20:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-30  3:12 Any Suggestion How To Accomplish A Debug Macro? Hubert
2014-12-30 11:14 ` Simon Wright
2014-12-30 11:45   ` Hubert
2014-12-30 20:07   ` Brad Moore [this message]
2014-12-30 22:11     ` Randy Brukardt
2015-01-01 12:28       ` Georg Bauhaus
2015-01-02 20:37       ` J-P. Rosen
2015-01-02 21:13         ` Dmitry A. Kazakov
2015-01-02 21:52           ` Randy Brukardt
2015-01-02 22:17             ` Niklas Holsti
2014-12-31  0:15     ` Luke A. Guest
2014-12-30 16:24 ` sbelmont700
2014-12-30 18:21   ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox