PureBasic Forum
http://forums.purebasic.com/english/

LogFileSystem - Module - OOP
http://forums.purebasic.com/english/viewtopic.php?f=12&t=75026
Page 1 of 1

Author:  StarBootics [ Sat Apr 04, 2020 2:19 pm ]
Post subject:  LogFileSystem - Module - OOP

Hello everyone,

A simple LogFileSystem, first the module version :
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : LogFileSystem
; File Name : LogFileSystem - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : Feb 17, 2015
; Last Update : Apr 04, 2020
; PureBasic code : V5.72
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule LogFileSystem
 
  Declare Initialize(P_FileName.s, P_OverwriteFile.b = #True)
  Declare Write(P_Information.s)
  Declare Finish()
  Declare.s DateMask(P_DateMask.s)
  Declare Reset()
 
EndDeclareModule

Module LogFileSystem
 
  Structure Instance
   
    FileName.s
    DateMask.s
    Spacing.s
   
  EndStructure
 
  Global Instance.Instance

  Procedure Initialize(P_FileName.s, P_OverwriteFile.b = #True)
   
    Instance\FileName = P_FileName
    Instance\DateMask = "%dd-%mm-%yyyy %hh:%ii:%ss"

    If P_OverwriteFile.b = #True
      FileID = CreateFile(#PB_Any, Instance\FileName)
    Else
      FileID = OpenFile(#PB_Any, Instance\FileName)
    EndIf
   
    If IsFile(FileID) <> 0
     
      Instance\Spacing = LSet("", 5, "<")
      Comment.s = "; " + Instance\Spacing + " LOG ENTRY START " + Instance\Spacing
      Line.s = LSet("; ", Len(Comment), "<")
     
      FileSeek(FileID, FileSize(Instance\FileName))
      WriteStringN(FileID, Line)
      WriteStringN(FileID, Comment)
      WriteStringN(FileID, Line)
     
      CloseFile(FileID)
     
      Success.b = #True
     
    EndIf
   
    ProcedureReturn Success
  EndProcedure
 
  Procedure Write(P_Information.s)
   
    FileID = OpenFile(#PB_Any, Instance\FileName)
   
    If IsFile(FileID)
     
      FileSeek(FileID, Lof(FileID))
      WriteStringN(FileID, FormatDate(Instance\DateMask, Date()) + " : " + P_Information)
      CloseFile(FileID)
     
    EndIf
   
  EndProcedure
 
  Procedure Finish()
   
    FileID = OpenFile(#PB_Any, Instance\FileName)
   
    If IsFile(FileID)
     
      Comment.s = "; " + Instance\Spacing + " LOG ENTRY STOP " + Instance\Spacing
      Line.s = LSet("; ", Len(Comment), "<")
     
      FileSeek(FileID, Lof(FileID))
      WriteStringN(FileID, Line)
      WriteStringN(FileID, Comment)
      WriteStringN(FileID, Line)
     
      CloseFile(FileID)
     
    EndIf
   
  EndProcedure
 
  Procedure.s DateMask(P_DateMask.s)
   
    If P_DateMask <> ""
      Instance\DateMask = P_DateMask
    Else
      Output.s = Instance\DateMask
    EndIf
   
    ProcedureReturn Output
  EndProcedure
 
  Procedure Reset()
   
    Instance\FileName = ""
    Instance\DateMask = ""
    Instance\Spacing = ""
   
  EndProcedure
 
EndModule

CompilerIf #PB_Compiler_IsMainFile
 
  LogFileSystem::Initialize(GetHomeDirectory() + "Test.log")
 
  Debug LogFileSystem::DateMask("")  ; Getting the current Date mask
  LogFileSystem::DateMask("%mm-%dd-%yyyy %hh:%ii:%ss")  ; Setting the date mask to english format
 
  LogFileSystem::Write("Engine3D initialized")
  Delay(5000)
  LogFileSystem::Write("Shader programs successfully created")
  Delay(1000)
  LogFileSystem::Write("Default material created")
 
  LogFileSystem::Finish()
  LogFileSystem::Reset()
 
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<

And the OOP style version :
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : LogFileSystem
; File Name : LogFileSystem - OOP.pb
; File version: 1.0.1
; Programming : OK
; Programmed by : StarBootics
; Date : Apr 04, 2020
; Last Update : Oct 29, 2020
; PureBasic code : V5.72
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule LogFileSystem
 
  Interface LogFileSystem
   
    Write(P_Information.s)
    Finish()
    DateMask.s(P_DateMask.s)
    Free()
   
  EndInterface
 
  Declare.i New(P_FileName.s, P_OverwriteFile.b = #True)
 
EndDeclareModule

Module LogFileSystem
 
  Structure Private_Members
   
    VirtualTable.i
    FileName.s
    DateMask.s
    Spacing.s
   
  EndStructure
 
  Procedure Write(*This.Private_Members, P_Information.s)
   
    FileID = OpenFile(#PB_Any, *This\FileName)
   
    If IsFile(FileID)
     
      FileSeek(FileID, Lof(FileID))
      WriteStringN(FileID, FormatDate(*This\DateMask, Date()) + " : " + P_Information)
      CloseFile(FileID)
     
    EndIf
   
  EndProcedure
 
  Procedure Finish(*This.Private_Members)
   
    FileID = OpenFile(#PB_Any, *This\FileName)
   
    If IsFile(FileID)
     
      Comment.s = "; " + *This\Spacing + " LOG ENTRY STOP " + *This\Spacing
      Line.s = LSet("; ", Len(Comment), "<")
     
      FileSeek(FileID, Lof(FileID))
      WriteStringN(FileID, Line)
      WriteStringN(FileID, Comment)
      WriteStringN(FileID, Line)
     
      CloseFile(FileID)
     
    EndIf
   
  EndProcedure
 
  Procedure.s DateMask(*This.Private_Members, P_DateMask.s)
   
    If P_DateMask <> ""
      *This\DateMask = P_DateMask
    Else
      Output.s = *This\DateMask
    EndIf
   
    ProcedureReturn Output
  EndProcedure
 
  Procedure Free(*This.Private_Members)
   
    FreeStructure(*This)
   
  EndProcedure
 
  Procedure.i New(P_FileName.s, P_OverwriteFile.b = #True)
   
    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
   
    *This\FileName = P_FileName
    *This\DateMask = "%dd-%mm-%yyyy %hh:%ii:%ss"
    *This\Spacing = LSet("", 5, "<")
   
    If P_OverwriteFile = #True
      FileID = CreateFile(#PB_Any, *This\FileName)
    Else
      FileID = OpenFile(#PB_Any, *This\FileName)
    EndIf
   
    If IsFile(FileID) <> 0
     
      *This\Spacing = LSet("", 5, "<")
      Comment.s = "; " + *This\Spacing + " LOG ENTRY START " + *This\Spacing
      Line.s = LSet("; ", Len(Comment), "<")
     
      FileSeek(FileID, FileSize(*This\FileName))
      WriteStringN(FileID, Line)
      WriteStringN(FileID, Comment)
      WriteStringN(FileID, Line)
     
      CloseFile(FileID)
     
    EndIf
   
    ProcedureReturn *This
  EndProcedure
 
  DataSection
    START_METHODS:
    Data.i @Write()
    Data.i @Finish()
    Data.i @DateMask()
    Data.i @Free()
    END_METHODS:
  EndDataSection
 
EndModule

CompilerIf #PB_Compiler_IsMainFile
 
  LFS.LogFileSystem::LogFileSystem = LogFileSystem::New(GetHomeDirectory() + "Test.log")
 
  Debug LFS\DateMask("") ; Getting the current Date mask
  LFS\DateMask("%mm-%dd-%yyyy %hh:%ii:%ss") ; Setting the date mask to english format
 
  LFS\Write("Engine3D initialized")
  Delay(5000)
  LFS\Write("Shader programs successfully created")
  Delay(1000)
  LFS\Write("Default material created")
 
  LFS\Finish()
 
  LFS\Free()
 
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<

Choose the programming style you prefer.

Best regards
StarBootics

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/