Mega Code Archive

 
Categories / Delphi / System
 

Easy logger

Title: Easy logger Question: very often we have Logs to keep track of processes and/or errors, some times we have the logs displaying on screen using TMemo or TRichEdit and you often have to watch for logfiles too large as they might consume your memory... Answer: I created a procedure just to make life easier when loggin the common stuff, instead of using code like: MyLogMemo.Lines.Add('an event just happened!'); or MyRichEdit.Lines.Append('Windows is crashing...') etc... I have this procedure, which I have on my "goodies.pas" unit (togheter with my setstatusbar procedure) Procedure Log(S:String; LogTo:TStrings; Const MaxLogSize:Integer; Const AddTime:Boolean=True; Const OnlyTime:Boolean=True); Var X:Integer; Begin &nbsp&nbspIf (AddTime) Then &nbsp&nbsp&nbsp&nbspIf (OnlyTime) Then &nbsp&nbsp&nbsp&nbsp&nbsp&nbspS:=FormatDateTime('hh:mm - ', Now)+S &nbsp&nbsp&nbsp&nbspElse &nbsp&nbsp&nbsp&nbsp&nbsp&nbspS:=FormatDateTime('mm/dd hh:mm - ', Now)+S; &nbsp&nbspLogTo.Append(S); &nbsp&nbspIf (LogTo.CountMaxLogSize) Then &nbsp&nbsp&nbsp&nbspFor X:=0 To (LogTo.Count-(MaxLogSize Div 10)) Do &nbsp&nbsp&nbsp&nbsp&nbsp&nbspLogTo.Delete(0) End; ...there's 2 constants by default to TRUE - addtime (go figure its use!) - onlytime (take out the date) so, when I need it, I just include my "goodies.pas" uses goodies; and call it like: Log('AnyEvent', Memo.Lines, 1000) or Log('whatever', MyLogFile, 1000) hope this is useful by the way, the source code of the article was formatted using my PAS 2 HTML converter keep up coding salu2 EberSys