LMD LogTools tutorial

From LMD
Revision as of 14:30, 17 July 2007 by Fduch (talk | contribs) (LMDLogMessage unit)

Jump to: navigation, search

LMDLogMessage unit

Main purpose of this tool package is logging and monitoring actions of application. It should be used when you can't run regular debugger or when it useless - multi-thread application, system services, applications on client site etc.

Сurrent pre-beta version provides basic features only. To start use LogTools in your application you should add LMDLogMessage.pas unit to uses section of your unit, then you'll be able to use LMDLog instance of TLMDLogSession class.

<delphi> uses

 ..., LMDLogMessage, ...;

...

 LMDLog.SendString('Test', 'Hello, World!');

... </delphi>


TLMDLogSession class has methods to send some info into main log - TLMDLogSession.Send*, to watch data in Watch area - TLMDLogSession.Watch* (main different with Send* function that Watch* has just changed value of watched variables, but not add new record)

<delphi> uses

 ..., LMDLogMessage, ...;

...

 while i < 200 do
   LMDLog.WatchInteger('i', i);

... </delphi>

To logging flow of application, below methods available: TLMDLogSession.Enter*, TLMDLogSession.Leave*. Sample:

<delphi> uses

 ..., LMDLogMessage, ...;

... function TestFunction: HResult; begin

 LMDLog.EnterMethod('TestFunction');
 try
   ...
 finally
   LMDLog.SendHResult('Result', Result);
   LMDLog.LeaveMethod('TestFunction');
 end;

end; ... </delphi>

and methods that can show trace of application

<delphi> uses

 ..., LMDLogMessage, ...;

...

 for i := 0 to Count - 1 do
 begin
   LMDLog.SendInteger('Iteration', i);
   b := 20 + i;
   LMDLog.TouchCounter('Checkpoint');
   a := CalcA(b);
   LMDLog.TouchCounter('Checkpoint');
   c := a div 34;
   LMDLog.ReleaseCounter('Checkpoint');
 end;

... </delphi>


To control log storage TLMDLogSession has next methods - TLMDLogSession.ClearLog, TLMDLogSession.ClearWatch and TLMDLogSession.ClearCallStack


TLMDLogSession allow you to control level of you logging messages. It's two properties for it - TLMDLogSession.AllowLogLevel - set minimum log level for messages, TLMDLogSession.DefaultLogLevel - set default log level for messages where level isn't set as parameter.

<delphi> uses

 ..., LMDLogMessage, ...;

...

 {$ifdef DEBUG}
 LMDLog.AllowLogLevel := LogAll;
 LMDLog.AllowLogLevel := LogAll;
 {$ELSE}
 LMDLog.AllowLogLevel := LogFatal; 
 LMDLog.DefaultLogLevel := LogOff;
 {$ENDIF}
 
 for i := 0 to 4 do
 begin
   try 
     LMDLog.SendInteger('i', i);
     a := 5 / i;
     LMDLog.SendReal('a', a);
   except
     on E: EDivideByZero do
       LMDLog.SendString(LogFatal, 'Error', 'Divide by zero');
   end;
 end;

... </delphi> Logging for 'a' and 'i' will be turned off when DEBUG undefined, but 'Error' will be sent to log in any case.

LMDLogConsole

This logging and monitoring console for LMD LogTools. All data that you send from your application are collected here.

LMD Logger Console.png