It is currently Thu Jul 02, 2020 6:45 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: LogFileSystem - Module - OOP
PostPosted: Sat Apr 04, 2020 2:19 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jul 07, 2013 11:35 am
Posts: 531
Location: Canada
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.0
; Programming : OK
; Programmed by : StarBootics
; Date : Apr 04, 2020
; Last Update : Apr 04, 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)
   
    ClearStructure(*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

_________________
The Stone Age did not end due to a shortage of stones !


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: davido and 9 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