File logger

Share your advanced PureBasic knowledge/code with the community.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

File logger

Post by luis »

A logger include to collect debug data.


Logger.pbi

Code: Select all

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: Select all

 

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: Select all

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.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Nice. Been wanting to put something like this together for quite some time.

Many thanks for sharing this.

cheers
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Indeed, this is great. Just updated my game with this new version. :D
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post by luis »

Added an L2F_ASSERT macro, see first post.

Ciao!
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Love this tool :)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: File logger

Post by Kwai chang caine »

Very useful, thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply