Board index » delphi » Using {$IFDEF DEBUG} for debugging messages.

Using {$IFDEF DEBUG} for debugging messages.

Hello,

If I have the following code:

{$IFDEF DEBUG}
    OutputDebugString(PChar('Error getting URL: ' + url + ' error message: '
+ e.Message));
{$ENDIF}

Is this an acceptable coding usually? If now what do people usually use to
print out debug messages but only do that when debugging in the IDE?

Thanks,

Victor

 

Re:Using {$IFDEF DEBUG} for debugging messages.


Quote
Victor Hadianto wrote:
> Hello,

> If I have the following code:

> {$IFDEF DEBUG}
>     OutputDebugString(PChar('Error getting URL: ' + url + ' error message: '
> + e.Message));
> {$ENDIF}

> Is this an acceptable coding usually? If now what do people usually use to
> print out debug messages but only do that when debugging in the IDE?

OutputDebugString is useful, and I use a similar strategy. However I
infer from that line of code that you appear to be hiding exception
messages in release versions; this is generally not good practice.

--
jc

Remove the -not from email

Re:Using {$IFDEF DEBUG} for debugging messages.


Quote
> {$IFDEF DEBUG}
>     OutputDebugString(PChar('Error getting URL: ' + url + ' error message: '
> + e.Message));
> {$ENDIF}

I miss the condition. Should it read: if URL <> ExpectedURL then
OutputDebugString(....?

Quote
> Is this an acceptable coding usually? If now what do people usually use to
> print out debug messages but only do that when debugging in the IDE?

  assert( ExpectedURL, URL, 'Error getting URL: ' + url + ' error
message: ' + e.Message );

and compiler option assertions on if debugging and off otherwise.

  felix

Re:Using {$IFDEF DEBUG} for debugging messages.


Hi Victor,

you may find simpler to wrap your logging code in a single function and then
call this function (at least you will avoid writing the {$IFDEF DEBUG} and
{$ENDIF} everywhere).

ex:

procedure DebugMessage(const Fmt: string; Args: array of const);
{$IFDEF DEBUG}
var
    Msg: string;
{$ENDIF}
begin
{$IFDEF DEBUG}
    Msg := Format(Fmt, Args);
    OutputDebugString(PChar(Msg));
{$ENDIF}
end;

For a more professional way of logging messages from within your application
give a look to Raize CodeSite:

http://www.raize.com/DevTools/CodeSite/Default.asp

Cheers

Renato Mancuso
http://www.renatomancuso.com

Other Threads