THotLog |
[ Home ] |
Logging data |
THotLog provides 2 methodes to log strings, and 2 more to log errors
and exceptions.
The first one and the last two have overloaded definitions, resulting
in a total of height procedure available to log anything.
1- Logging variables |
Procedure AddStr (aString: String);
procedure Add(aString: String); overload;
Procedure Add (aStringList: TStringList); overload;
Self.header := TstringList.Create; With Self.Header Do Begin Add('{/}{LNumOff}{*80*}'); Add('>>>> Start {App_name} v {App_ver}{80@}{&}{dte} {hms}{&}'); Add('{@12}From : {App_path}'); Add('{@12}Prms : {App_prm-}{/}'); End; Self.footer := TStringList.Create; With self.Footer Do Begin Add('{LNumOff}'); Add('<<<< Stop {App_name}{80@}{&}{dte} {hms}{&}'); Add('{*80*}{/}'); End;They are added to the log file that way:
hLog.Add (header); ... hLog.Add (footer);and the result will look like this:
******************************************************************************** >>>> Start HotLogTest.exe v 1.0.0.4 2004-03-09 23:38:54 From : C:\Program Files\Borland\Delphi6\Projects\HotLog\ Prms : (No params) ... <<<< Stop HotLogTest.exe 2004-03-09 23:38:55 ********************************************************************************
Procedure Add(style: TVarRecStyle; aConstArray: Array of Const); overload;
1: Using aStyle := vsNone;
var i: real; begin Randomize; i := Random; hLog.Add(vsNone,['aString','{/}another one, followed by integers',1,2,3,'{/} and now, the "i" value :',i]); end;Result:
aString another one, followed by integers123 and now, the "i" value :0,502231817925349
hLog.Add(vsNone,['aString','another one, followed by integers',1,2,3,'and now, the "i" value :',i]);will result in
aStringanother one, followed by integers123and now, the "i" value :0,502231817925349The values are restituted like you sent them, without spaces or anything like that.
2: Using aStyle := vsBasic;
The elements in the array will be separated by colons and surrounded by parenthesis.
Taking again the above example but removing the formating tags:
hLog.Add(vsExtended,[['aString','{/}another one, followed by integers',1,2,3,'{/} and now, the "i" value :',i]);will output:
(aString; another one, followed by integers; 1; 2; 3; and now, the "i" value :; 0,502231817925349);
hLog.Add(vsExtended,[['aString','{/}another one, followed by integers',1,2,3,'{/} and now, the "i" value :',i]);will output:
AnsiString : aString AnsiString : another one, followed by integers Integer : 1 Integer : 2 Integer : 3 AnsiString : and now, the "i" value : Extended : 0,502231817925349
var TWinVer: Array of string; ... TWinVer[0] := 'Unknown'; TWinVer[1] := '16b'; TWinVer[2] := '32b'; hLog.Add(vsExtended,['This system could be ', TWinVer2Str]);but the result is not as expected:
AnsiString : This system could be Pointer : ^(0012F5B8)
Avoiding memory leaks:
The array of Const that you pass to hLog.Add(...) is your
property. hLog will make a copy of it, and free this copy when no longer
needed. But it doesn'tchange anything to YOUR array. This means
that if it is declared somewhere else than in the calling function itself
(thus being not freed when you'll leave it), you should free its contents,
and set its length to 0 as soon as it is no longer needed, after hLog.Add() returns).
2- Logging exceptions and errors |
Procedure AddException(ex: Exception; err: Integer=0; freeAfterPost: Boolean=False); overload;
This is the most basic way to log an exception raised
during code execution:
ex is, not so surprisingly, the exception occured.
err is reserved for internal use.
If freeAfterPost is set to True, ex will be freed by the
hLog.AddException procedure. Otherwise not.
This is an example:
Function DoSomethingAndDivide(x,y: Integer): Integer; Begin Result := -1; TRY ... Result := Abs(x div y); // If y is worth zero, an EZeroDivide exception will appear. EXCEPT On E:Exception Do hLog.AddException(E); // nothing else to do. ... END; End;With an EZeroDivide exception and hLog.SetErrorCaption('*** E R R O R ***') the result will be like this:
*********************** *** E R R O R *** "Division par zéro" ***********************To logg more informations, hLog provides an overloaded definition of the AddException procedure, with tha ability to handle much more things:
Procedure AddException(ex: Exception; func: String; args: Array of const; err: Integer=0; freeAfterPost: Boolean=False); overload;
This is the evolved way to logg an exception raising during code
execution:
Two new parametres are defined:
func is the name of the function in which the exception occurs.
args is an array of const that may handle anything you want.
The other parameters are the same than above.
This is an example:
Function DoSomethingAndDivide(Sender: TObject; x,y,z: Integer; aString: String): Integer; Begin Result := -1; TRY ... Result := Abs(x div (y*z)); // If y or z are worth zero, an EZeroDivide exception will appear. EXCEPT On E:Exception Do hLog.AddException(E,'DoSomethingAndDivide',[Sender,x,y,z,aString]); // nothing else to do. ... END; End;
Like above, hLog.SetErrorCaption( ) was called as hLog.SetErrorCaption('*** E R R O R ***').
The output will be:******************************************************************************************** *** E R R O R *** "Division par zéro" in function : *********************** DoSomethingAndDivide( TObject : Button1 (TButton, "Compute"); Integer : 1; Integer : 0; Integer : -547; AnsiString : Some string pointed at by aString ) *********************************************************************************************If SetErrorViewStyle received vsBasic, the result would have been:
*** E R R O R *** "Division par zéro" in DoSomethingAndDivide(Button1,1,0,-547,Some string pointed at by aString);
Procedure AddError(err: Integer); overload;
Procedure AddError(err: Integer; func: String; args : Array of const); overload;
Output examples:
hLog.AddError(err);
***********************
*** E R R O R *** (15) "Le lecteur spécifié est introuvable"
***********************
hLog.AddError(err,'MyProc',[]);
********************************************************************************************
*** E R R O R *** (15) "Le lecteur spécifié est introuvable" in MyProc
********************************************************************************************
15 is number passed as err.Warning :
These two functions need to receive the error code. But reading it
seems to reset it sometimes.
If you want to use it, you'll have to first copy it's value into
an integer variable when reading, before sending it:
If Form1.OpenDialog1.Execute then begin // Let's search an imaginary file, modifying the name of an existing one: AssignFile(F, Form1.OpenDialog1.FileName +'x'); {$I-} Reset(F); // It seems that IOresult is reseted to 0 as soon as you read it, // We'll then store it before usage ... err := IOResult; // IOResult is now worth 0: showMessage(IntToStr(IOresult)); -> "0"! If err <> 0 then hLog.AddError(err); {$I+} End;
THotLog |