Recent

Author Topic: Debug logging  (Read 7942 times)

adamski

  • New Member
  • *
  • Posts: 16
Debug logging
« on: November 07, 2009, 12:14:50 am »
HI;

I'm about to write a new app, and wanted to understand what options I have to output debug logs.
I'd like the ability to turn on / off  the debug logging, the debug code to be kept to a minium, and most importantly, when turned off - I don't want the debug output commands to impact on performance.

The simplest solution appears to be LCLProc and DebugLn. But I was wondering what effect calling that function has, if an output file hasn't been defined, and it's a GUI app (ie debug logging essentially turned off)? How much will it effect performance?

What are the alternatives?

Many thanks,
Marc

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Debug logging
« Reply #1 on: November 11, 2009, 06:51:55 pm »
The standard way to do this is adding all debug code inside ifdefs, like:

{$ifdef Debug}
DbgAppendToFile(ExtractFilePath(ParamStr(0)) + '1.log', 'Some text');
{$endif}

This way nothing from your debug code will go into the final executable. And you can set debugging on/off by compiling with the option -dDebug or not.

Mike James

  • New Member
  • *
  • Posts: 23
Re: Debug logging
« Reply #2 on: November 12, 2009, 11:43:07 am »
It might be better to add {$ifdef DEBUG_MY_APP} to customize the DEBUG - so that you don't activate some debug code in another module that you don't want  :D

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1226
    • Burdjia
Re: Debug logging
« Reply #3 on: November 12, 2009, 12:05:14 pm »
I did wrote a Log class some time ago. It was something like this (simplified):
Code: [Select]
TYPE
  TLogClass = CLASS
  PRIVATE
  { The higher the most "log" will write }
     fLogLevel: INTEGER;
  PUBLIC
     CONSTRUCTOR Create (aLogLevel: INTEGER = -1);
     PROCEDURE Out (Text: STRING; Level: INTEGER);

     PROPERTY Level: INTEGER READ fLogLevel WRITE fLogLevel;
  END;

  CONSTRUCTOR TLogClass.Create (aLogLevel: INTEGER);
  BEGIN
     fLogLevel := aLogLevel;
  END;
 
  PROCEDURE TLogClass.Out (Text: STRING; Level: INTEGER);
  BEGIN
     IF Level <= fLogLevel THEN
     BEGIN
        WriteInLogFile (Text);
     END;
  END;

The use was as this (also simplified):
Code: [Select]
VAR
    LogFile: TLogClass;
    LogLevel: INTEGER;
BEGIN
    IF (ParamCount > 1) AND (ParamStr (1) = '-DEBUG') THEN
       LogLevel := 1
    ELSE
       LogLevel := 0;
    LogFile := TLogClass.Create (LogLevel);
{ At some point of the program... }
    LogFile.Out ('We will do something', 1); { This will write the log if "LogLevel" is 1 or bigger }
    LogFile.Out ('This message will not be written', 3);  { This will write the log if "LogLevel" is 3 or bigger }
{ An finally }
    LogFile.Free;
END.

The actual class defined some constants to name the log level (NONE, INFO, ERROR, ALL, etc) and manages the file, creating it if it doesn't exists, flushing the output, closing it, etc.
« Last Edit: November 12, 2009, 12:07:04 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

EMTR

  • New Member
  • *
  • Posts: 20
Re: Debug logging
« Reply #4 on: November 12, 2009, 04:13:30 pm »
OutputDebugString('hello'); - If you want to write to the output/messages window

use conditional defines if its gonna go live

otherwise use a logging framework? Log4Delphi is OK I think

javivf

  • New Member
  • *
  • Posts: 14
    • http://javivf.alasombra.net/blog
Re: Debug logging
« Reply #5 on: November 14, 2009, 12:17:04 pm »
Probably also lazarus's debugserver could help you. You can find it on tools/debugserver  :)

 

TinyPortal © 2005-2018