unit TILogWrappers; //****************************************************************************** interface uses JvLogFile; // From lowest to highest type TIDebugLevel = (dbg_OFF, dbg_Fatal, dbg_Critical, dbg_Error, dbg_Info, dbg_Tracing, dbg_Debug); const TIDebugTitle : array[TIDebugLevel] of string = ('Off', 'Fatal', 'Critical', 'Error', 'Info', 'Tracing', 'Debug'); type // Wraps TJvLogFile to add some pretty features and ease its use // "The simpler and easier, the better" TILogWrapper = class(TJvLogFile) private FDebugLevel: TIDebugLevel; public procedure Print(aDebugLevel: TIDebugLevel; aMessage: string); overload; procedure Print(aDebugLevel: TIDebugLevel; aContext: string; aMessage: string); overload; procedure Print(aDebugLevel: TIDebugLevel; anObject: TObject; aMessage: string); overload; published property DebugLevel: TIDebugLevel read FDebugLevel write FDebugLevel; end; var LogFile: TILogWrapper; //****************************************************************************** implementation uses SysUtils, JclStrings, TIUtils, Contnrs, Classes; { TILogWrapper } procedure TILogWrapper.Print(aDebugLevel: TIDebugLevel; aMessage: string); begin Print(aDebugLevel, nil, aMessage); end; procedure TILogWrapper.Print(aDebugLevel: TIDebugLevel; anObject: TObject; aMessage: string); var objectName: string; begin if Assigned(anObject) then begin if anObject is TComponent then begin objectName := (anObject as TComponent).Name; if objectName='' then objectName := anObject.ClassName; end else begin objectName := anObject.ClassName; end; end; Print(aDebugLevel, objectName, aMessage); end; procedure TILogWrapper.Print(aDebugLevel: TIDebugLevel; aContext, aMessage: string); const fmtLineHead = ' %20s'; fmtLineFoot = ' [%s] : %s'; begin { TODO 4 -oRDU -cCodeQuality : Log file uses Unicode, this is not supported by most tools and may there be simplified to regular ansi string } if aDebugLevel <= FDebugLevel then begin Add( Format(fmtLineHead, [ StrPadRight(StrRepeat('- ', Ord(aDebugLevel)) + TIDebugTitle[aDebugLevel], 20, ' ') ]) , Format(fmtLineFoot, [ aContext, aMessage] ) ); end; end; initialization if not Assigned(LogFile) then begin LogFile := TILogWrapper.Create(nil); LogFile.SizeLimit := 10*1024*1024; // 10 megs limit LogFile.Active := True; LogFile.FileName := ChangeFileExt(ParamStr(0),'.log'); LogFile.AutoSave := True; LogFile.DebugLevel := dbg_Debug; end; finalization if Assigned(LogFile) then FreeAndNil(LogFile); end.