Debug log file

Share your advanced PureBasic knowledge/code with the community.
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Debug log file

Post by EdzUp[SD] »

How many of us wished they could have a log that they could look through when they wanted instead of after every compile. This procedure does just that :)

Code: Select all

;
;   DebugSystem.pbi - Copyright ©EdzUp
;   Coded by Ed Upton
;

Global DebugFileName.s = "DebugLog.txt"

Declare AddLog( StringToAdd.s )

Procedure AddLog( StringToAdd.s )
  Static FileHandle.l = 0
  
  FileHandle = OpenFile( #PB_Any, DebugFileName )       ;open or create a file
  If Not FileHandle
    MessageRequester( "DebugLog Error", "Could not access file "+DebugFileName )
    End
  EndIf
  FileSeek( FileHandle, Lof( FileHandle ) )             ;seek to the end
  WriteStringN( FileHandle, StringToAdd )                ;write out the string to the file
  CloseFile( FileHandle )                               ;close the file afterwards
EndProcedure
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Here's a small code I use for the same purpose :).

Code: Select all

 ProcedureDLL LOG_AddHeader(File.s,ProgramName.s) 
  ;
  Log.l = CreateFile(#PB_Any,File)
  If IsFile(Log)
   WriteStringN(Log,"<<< "+ProgramName.s+" >>>")
   WriteStringN(Log,"")
   FlushFileBuffers(Log)
   CloseFile(Log)
   ProcedureReturn 1
  Else
   ProcedureReturn -1
  EndIf
  ;
 EndProcedure

 ProcedureDLL LOG_AddFooter(File.s)
  ;
  Leng = FileSize(File)
  Log.l = OpenFile(#PB_Any,file.s)
  If IsFile(Log)
   FileSeek(Log,Leng)
   WriteStringN(Log,"")
   WriteStringN(Log,"<<< Session ended >>>")
   FlushFileBuffers(Log)
   CloseFile(Log)
   ProcedureReturn 1
  Else
   ProcedureReturn -1
  EndIf
  ;
 EndProcedure

 ProcedureDLL LOG_Write(File.s,Entry.s)
  ;
  Leng = FileSize(File)
  Log.l = OpenFile(#PB_Any,File)
  If IsFile(Log)
   FileSeek(Log,Leng)
   If Entry = ""
    WriteStringN(Log,"")
   Else
    TheData.s = "#" + FormatDate("%dd/%mm/%yy",Date())+" "+FormatDate("%hh:%ii:%ss", Date())+"      "+entry.s
    WriteStringN(Log,TheData)
   EndIf
   FlushFileBuffers(Log)
   CloseFile(Log)
   ProcedureReturn 1
  Else
   ProcedureReturn -1
  EndIf
  ;
 EndProcedure

 ProcedureDLL LOG_ClearLog(File.s)
  ;
  If FileSize(File) <> -1 And FileSize(File) <> -2
   ProcedureReturn DeleteFile(File)
  Else
   ProcedureReturn -1
  EndIf
  ;
 EndProcedure

 LOG_AddHeader("log.txt","MyProgram")
 LOG_Write("log.txt","Program initialized successfully")
 LOG_AddFooter("log.txt")
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Post Reply