It is currently Sun May 26, 2013 4:52 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: File logger
PostPosted: Wed Mar 04, 2009 9:17 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 2242
Location: Italy
A logger include to collect debug data.


Logger.pbi

Code:
EnableExplicit

; Version 2.0
; Luis, April 2009, PB 4.30
; This include is useful to collect debug data when other people are testing your programs.
; Define #L2F_ENABLE = 1 to enable it and = 0 to remove any trace from your final executable.
; The only macro you really need to call is L2F() and maybe L2F_ASSERT().
; All the other macros are to customize the log if you don't like the defaults.


; Changes from the previous one:
; * changed the name of a couple of macros, sorry for the inconvenience :-)
; + added two macros to set time format and date format masks
; + added and ASSERT macro writing to the log only if the passed expression is not verified

CompilerIf Defined(L2F_ENABLE, #PB_Constant) = 0
 CompilerError "This include requires a constant #L2F_ENABLE defined to 0 or 1"
CompilerEndIf

CompilerIf #L2F_ENABLE = 1

;{ DATA STRUCTURE

 Structure T_L2F
  nFileNum.i
  sLogFile.s
  flgAppend.i
  flgTimeStamp.i
  sTimeFormat.s
  flgDateStamp.i
  sDateFormat.s
  flgAborted.i
 EndStructure : Define tL2F.T_L2F
 
 With tL2F ; defaults
    \sLogFile = GetPathPart(ProgramFilename()) + "debug.txt" ; exe path + debug.txt
    \flgAppend = #False             ; overwrite mode
    \flgTimeStamp = #True           ; time stamp enabled
    \sTimeFormat = "%hh:%ii:%ss"    ; format time
    \flgDateStamp = #False          ; date stamp disabled   
    \sDateFormat = "[%dd/%mm/%yy]"  ; format date
    \flgAborted = #False            ; internal use   
 EndWith
;}

;{ INTERNAL PROCEDURES

Procedure _L2F_SET_LOGFILE (sLogFile.s)
  Shared tL2F
  tL2F\sLogFile = sLogFile
EndProcedure

Procedure _L2F_ENABLE_APPEND (flgAppend)
  Shared tL2F
  tL2F\flgAppend = flgAppend
 EndProcedure

Procedure _L2F_ENABLE_TIMESTAMP (flgTimeStamp)
  Shared tL2F
  tL2F\flgTimeStamp = flgTimeStamp
EndProcedure

Procedure _L2F_SET_TIMEFORMAT (sTimeFormat.s)
  Shared tL2F
  tL2F\sTimeFormat = sTimeFormat
 EndProcedure

Procedure _L2F_ENABLE_DATESTAMP (flgDateStamp)
  Shared tL2F
  tL2F\flgDateStamp = flgDateStamp
 EndProcedure
 
Procedure _L2F_SET_DATEFORMAT (sDateFormat.s)
  Shared tL2F
  tL2F\sDateFormat = sDateFormat
 EndProcedure
 
Procedure _L2F (sText.s)
  Shared tL2F
  Protected sTextBuffer.s, iNow = Date()
 
  If IsFile(tL2F\nFileNum) = 0 And tL2F\flgAborted = #False
    If tL2F\flgAppend
        tL2F\nFileNum = OpenFile(#PB_Any, tL2F\sLogFile)       
        If tL2F\nFileNum
            FileSeek(tL2F\nFileNum, Lof(tL2F\nFileNum))
        EndIf
    Else
        tL2F\nFileNum = CreateFile(#PB_Any, tL2F\sLogFile)
    EndIf
 
  If IsFile(tL2F\nFileNum) = 0
    tL2F\flgAborted =#True ; stop trying
    MessageRequester("L2F", "Unable to start the log session.")   
  EndIf
   
  EndIf
 
  If Len(sText)
    If tL2F\flgDateStamp
        sTextBuffer + FormatDate(tL2F\sDateFormat, iNow)
    EndIf       
   
    If tL2F\flgTimeStamp       
        If Len(sTextBuffer) : sTextBuffer + " " : EndIf
        sTextBuffer + FormatDate(tL2F\sTimeFormat, iNow)
    EndIf   
   
    If Len(sTextBuffer) : sTextBuffer + " - " : EndIf
  EndIf
 
  If IsFile(tL2F\nFileNum)
    WriteString(tL2F\nFileNum, sTextBuffer + sText + #CRLF$)
    FlushFileBuffers(tL2F\nFileNum)
  EndIf
 EndProcedure

;}
 
CompilerEndIf


; The following are the macros you can call in your program

Macro L2F_SET_LOGFILE (sLogFile)
CompilerIf #L2F_ENABLE = 1
 _L2F_SET_LOGFILE (sLogFile)
CompilerEndIf
EndMacro

Macro L2F_ENABLE_APPEND (flgAppend)
CompilerIf #L2F_ENABLE = 1
 _L2F_ENABLE_APPEND (flgAppend)
CompilerEndIf
EndMacro

Macro L2F_ENABLE_TIMESTAMP (flgTimeStamp)
CompilerIf #L2F_ENABLE = 1
 _L2F_ENABLE_TIMESTAMP (flgTimeStamp)
CompilerEndIf
EndMacro

Macro L2F_SET_TIMEFORMAT (sTimeFormat)
CompilerIf #L2F_ENABLE = 1
 _L2F_SET_TIMEFORMAT (sTimeFormat)
CompilerEndIf
EndMacro

Macro L2F_ENABLE_DATESTAMP (flgDateStamp)
CompilerIf #L2F_ENABLE = 1
 _L2F_ENABLE_DATESTAMP (flgDateStamp)
CompilerEndIf
EndMacro

Macro L2F_SET_DATEFORMAT (sDateFormat)
CompilerIf #L2F_ENABLE = 1
 _L2F_SET_DATEFORMAT (sDateFormat)
CompilerEndIf
EndMacro

Macro L2F_ASSERT (expression)
CompilerIf #L2F_ENABLE = 1
 If Not(expression)
    _L2F ("ASSERT FAILED: " + #PB_Compiler_File + ", line " + Str(#PB_Compiler_Line))
 EndIf
CompilerEndIf
EndMacro

Macro L2F (sText)
CompilerIf #L2F_ENABLE = 1
 _L2F (sText)
CompilerEndIf
EndMacro




Test program


Code:
 

EnableExplicit

#L2F_ENABLE = 1 ;  set this to 0 and all the logging code will disappear from your final executable

IncludeFile "Logger.pbi"

Define a, b, c

L2F_SET_LOGFILE("test.log") ; this call is optional, defaults to a "debug.txt" in the same directory of the exe

L2F_ENABLE_APPEND(#False) ; this call is optional, defaults to overwrite mode (#False)

L2F_SET_DATEFORMAT("%dd-%mm-%yyyy") ; this call is optional, defaults to "[%dd/%mm/%yy]"

L2F_SET_TIMEFORMAT("%hh:%ii:%ss") ; this call is optional, defaults to "%hh:%ii:%ss"

L2F_ENABLE_DATESTAMP(#False) ; this call is optional, defaults to datestamp disabled in the log (#False)

L2F_ENABLE_TIMESTAMP(#True) ; this call is optional, defaults to timestamp enabled in the log (#True)


L2F("CoolProgram v1.0 beta started.") ; this is the *ONLY* required call, the one reponsible of sending useful debug data to the log

; *** your program code here

L2F("a = " + Str(a))

a + 10

L2F("a = " + Str(a))

L2F("We are in the middle of the program...")

; *** more code here

b = 5

L2F_ASSERT(a = b) ; this call test the validity of an expression and will log an error message only if the assertion fails (as in this case)

L2F_ASSERT(c = 0) ; this is verified, no action taken in the log

L2F("Program ended.");

End




Output

Code:
23:55:52 - CoolProgram v1.0 beta started.
23:55:52 - a = 0
23:55:52 - a = 10
23:55:52 - We are in the middle of the program...
23:55:52 - ASSERT FAILED: D:\Documents and Settings\luis\Desktop\Logger\Logger test.pb, line 38
23:55:52 - Program ended.


EDIT: version 2.0


Last edited by luis on Sun Apr 05, 2009 10:57 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 10:45 pm 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2559
Location: Southwest OH - USA
Nice. Been wanting to put something like this together for quite some time.

Many thanks for sharing this.

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 12:17 am 
Offline
Addict
Addict
User avatar

Joined: Fri Jul 21, 2006 4:41 am
Posts: 2300
Location: Berlin, Germany
Indeed, this is great. Just updated my game with this new version. :D

_________________
Image
Windows 7, 64-Bit, PB v4.51 / Whose Hoff is it anyway?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 05, 2009 10:50 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 2242
Location: Italy
Added an L2F_ASSERT macro, see first post.

Ciao!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 05, 2009 11:45 pm 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2559
Location: Southwest OH - USA
Love this tool :)


Top
 Profile  
 
 Post subject: Re: File logger
PostPosted: Sun Feb 12, 2012 8:34 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 2513
Location: Lyon - France
Very useful, thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye