Code: Select all
;-===MODULE DESCRIPTION===
;Tiny module to create plain text logfiles
;Created with PB 6.20 Beta 1
;Author: :miso
;Creation date:2025.01.29
;Scope :all platform, but tested only on Windows 7 64 bit
;Permissions :777, do what you want, (author takes no responsibilities nor any claims)
;Known Issues :Does not check permissions for parent directory (platform compatibility reasons)
EnableExplicit
;-===MODULE DECLARATION===
DeclareModule log
;-===CONSTANTS===
#DirectoryType = -2
#BufferSize = 2048
;-===GLOBALS===
;###
; these globals are public, but they're not meant to be changed manually outside from the module.
; They're here, because that way I can use them together with modulename inside the module,
; and that seems a bit more clear/distinctive to my eyes (personal preference)
;###
Global directory.s, filename.s
Global filehandle.i = #False
Global opened.i = #False
;-===PROCEDURE DECLARATIONS===
Declare.i setdirectory(dir.s)
Declare.i setfile(filename.s)
Declare.i open()
Declare.i close()
Declare.i delete()
Declare.i write(entry.s)
Declare.i write_plain(entry.s)
EndDeclareModule
;-===MODULE===
Module log
;-===AUXILIARY PROCEDURES===
;***************************************
;auxiliary function for date
;***************************************
Procedure.s sub_getdate()
ProcedureReturn FormatDate("%yyyy-%mm-%dd %hh:%mm:%ss :: ",Date())
EndProcedure
;-===PUBLIC PROCEDURES===
;***************************************
;Sets the directory for the logfile
;***************************************
Procedure.i setdirectory(dir.s)
If log::opened
Debug "error, trying to set directory, but a logfile is already opened"
ProcedureReturn #False
EndIf
If FileSize(dir.s) = #DirectoryType
log::directory.s = dir.s
ProcedureReturn #True
EndIf
Debug "Error. Trying to set invalid log directory:" + dir.s
ProcedureReturn #False
EndProcedure
;***************************************
;Sets the name of the logfile
;***************************************
Procedure.i setfile(name.s)
If name.s<>""
log::filename.s = name.s
ProcedureReturn #True
EndIf
Debug "Error. Trying to set empty string as logfile name:" + name.s
ProcedureReturn #False
EndProcedure
;***************************************
;Opens a logfile to write
;***************************************
Procedure.i open()
If log::opened Or IsFile(log::filehandle.i)
Debug "Error, logfile is already opened"
ProcedureReturn #False
EndIf
log::filehandle.i = OpenFile(#PB_Any,log::directory.s+log::filename.s,#PB_File_Append)
If Not IsFile(log::filehandle.i)
Debug "Error, could not open the logfile "+log::directory.s+log::filename.s
ProcedureReturn #False
EndIf
log::opened.i = #True
FileBuffersSize(log::filehandle.i,log::#BufferSize)
ProcedureReturn #True
EndProcedure
;***************************************
;closes the logfile
;***************************************
Procedure.i close()
If Not log::opened Or Not IsFile((log::filehandle.i))
Debug "Error, trying to close logfile that is not opened."
ProcedureReturn #False
EndIf
CloseFile(log::filehandle.i)
log::filehandle.i = #False
log::opened.i = #False
ProcedureReturn #True
EndProcedure
;***************************************
;Deletes a logfile, closing it if opened
;***************************************
Procedure.i delete()
If log::opened : log::close() : EndIf
If FileSize(log::directory.s+log::filename.s)
DeleteFile(log::directory.s+log::filename.s)
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
;***************************************
;Writes an entry into the opened logfile
;***************************************
Procedure.i write(entry.s)
If Not log::opened Or Not IsFile(log::filehandle.i)
Debug "Error, trying to write logentry, but logfile is not opened"
ProcedureReturn #False
EndIf
WriteStringN(log::filehandle.i,sub_getdate()+entry.s)
FlushFileBuffers(log::filehandle.i)
ProcedureReturn #True
EndProcedure
;*************************************************************
;Writes an entry into the opened logfile without date and time
;*************************************************************
Procedure.i write_plain(entry.s)
If Not log::opened Or Not IsFile(log::filehandle.i)
Debug "Error, trying to write log entry, but logfile is not opened"
ProcedureReturn #False
EndIf
WriteStringN(log::filehandle.i,entry.s)
FlushFileBuffers(log::filehandle.i)
ProcedureReturn #True
EndProcedure
EndModule
;-===EXAMPLE USAGE===
;***************************************************************
;Example usage
;***************************************************************
log::setdirectory(GetCurrentDirectory())
log::setfile("mylog.log")
;log::delete() ;uncomment this to check and delete a possible previous logfile to make the log not continous
log::open()
log::write_plain("#######################################")
log::write("Started program")
log::write("Test log entry")
log::write("Program closed")
log::close()
;log::delete() ;uncomment this line to remove the created sample logfile from your disk.
End