ich hab mir einen kleinen Logger geschrieben, weil ich den für ein anderes Projekt verwenden will. Ist die erste kleine Sache, die ich PB fertiggestellt habe und sie spiegelt meinen aktuellen Stand in PB wider (bin ja noch nicht so lange dabei).
Ich bin nicht der Meinung, dass die Welt nur auf mein Progrämmchen mit den rudimentären Loggerfunktionen gewartet hat, aber falls jemand Zeit und Nerv hat, sich die Sache mal anzusehen und mir dann zu sagen, was alles falsch ist oder besser gelöst werden könnte, wäre das für mich ein sehr hilfreiches Feedback um mich weiterzuentwickeln. Stört euch bitte nicht an meinem ebenfalls rudimentären Englischkenntnissen, die Schule ist schon eine Weile her bei mir
Aktuelle Fassung vom 09.09.2018 (Copyright korrigiert)
Code: Alles auswählen
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; + SimplePBLogger
; + Utility to log informations in PureBasic-Programs
; + Version 0.3
; + MIT License
; + Copyright (c) 2018 Joachim Weiß
; + Permission is hereby granted, free of charge, to any person obtaining a copy
; + of this software and associated documentation files (the "Software"), to deal
; + in the Software without restriction, including without limitation the rights
; + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; + copies of the Software, and to permit persons to whom the Software is
; + furnished to do so, subject to the following conditions: +
; +
; + The above copyright notice and this permission notice shall be included in all
; + copies or substantial portions of the Software.
; +
; + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; + SOFTWARE.
; + Hints:
; + Before using the logger, initialize him! Use InitializeLogger an give him
; + as parameters the path, where the logfiles shall be stored and optional
; + the maximal number of logfiles you want to have for your application
; + (standard is 10 - with the maxNumber you can avoid to have too much files)
; + To write an Entry in a Logfile use WriteLogEntry. Select a LogLevel from
; + the constants (or create there a new loglevel, if you want) and create
; + the message you want to store. You have also the option to store variables
; + and their values etc. Please use SPBL_Parameter. Create a variable of
; + this structure an add key/value-pairs to the \entries-map. Put the
; + variable as the third parameter in the WriteLogEntry-call
; + If you want to export or import logfiles you can do this with the
; + ExportLogFiles / ImportLogFiles-procedure. These procedures need the path
; + of the Destination where the files should be copied from/to. You have the
; + option to tell the procedures how many files have to be copied. If there
; + are more files than to copy, the procedures take the youngest files of the
; + given number.
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DeclareModule SimplePBLogger
#SPBL_DEBUG = " Debug "
#SPBL_INFORMATION = "- Information -"
#SPBL_WARNING = "! Warning !"
#SPBL_ERROR = "*** ERROR ***"
Structure SPBL_Parameter ; for optional key/value-entries to write in Logfiles
Map Entries.s()
EndStructure
; Procedure to initialize the logger. Returns #True if successful
; If there are more logfiles than allowed (is set with the
; MaxNumberLogfiles parameter) the procedure will delete the oldest file(s)
Declare.i InitializeLogger(LocationLogfile.s, MaxNumberLogfiles.i = 10)
; Procedure to write an entry into the logfile. Returns #True if succesfull
; This procedure will create an new logfile, if there is no one for the actual
; date.
Declare.i WriteLogEntry(LogLevel.s, Message.s, *Parameters.SPBL_Parameter = #Null)
; Procedure to copy Logfiles from their standard location to another location
; e.g. for backup or for sendingt them to the programmer. Returns #True if successful
Declare.i ExportLogFiles(TargetLocation.s, NumberOfFiles.i = 10)
; Procedure to copy Logfiles to their standard location e.g. to restore them
; Returns #True if successful
Declare.i ImportLogFiles(FromLocation.s, NumberOfFiles.i = 10)
EndDeclareModule
Module SimplePBLogger
EnableExplicit
Structure Logfile ;
Name.s
Date.i
EndStructure
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#Separator = "\"
CompilerElse
#Separator = "/"
CompilerEndIf
Global.s Location
Global.i MaxFiles
Global NewList CurrentLogFiles.Logfile()
Global LoggerMutex = CreateMutex()
Global.i LoggerIsInitialized = #False
#LOGFILE = 1
#LOGDIRECTORY = 1
#TARGETDIRECTORY = 2
Procedure.i InitializeLogger(LocationLogFiles.s, MaxNumberLogfiles.i = 10)
LockMutex(LoggerMutex)
If ExamineDirectory(#LOGDIRECTORY, LocationLogFiles, "Logfile_*.txt")
Location = LocationLogFiles
MaxFiles = MaxNumberLogfiles
While NextDirectoryEntry(#LOGDIRECTORY)
AddElement(CurrentLogFiles())
With CurrentLogFiles()
\Name = DirectoryEntryName(#LOGDIRECTORY)
\Date = DirectoryEntryDate(#LOGDIRECTORY, #PB_Date_Created)
EndWith
Wend
SortStructuredList(CurrentLogFiles(), #PB_Sort_Ascending,
OffsetOf(Logfile\Date), TypeOf(Logfile\Date))
FinishDirectory(#LOGDIRECTORY)
; Make shure, that number of logfiles is lower than MaxFiles
FirstElement(CurrentLogFiles())
While ListSize(CurrentLogFiles()) > MaxFiles
DeleteFile(CurrentLogFiles()\Name)
DeleteElement(CurrentLogFiles(), 1)
Wend
LoggerIsInitialized = #True
EndIf
UnlockMutex(LoggerMutex)
ProcedureReturn LoggerIsInitialized
EndProcedure
Procedure.i WriteLogEntry(LogLevel.s, Message.s, *Parameters.SPBL_Parameter = #Null)
Define.i Result = #False
LockMutex(LoggerMutex)
If LoggerIsInitialized
Define.s Filename
Filename = Location +
"Logfile_" + FormatDate("%yyyy_%mm_%dd", Date()) + ".txt"
If OpenFile(#LOGFILE, Filename, #PB_File_Append)
WriteStringN(#LOGFILE, "-------------------------------------------")
WriteStringN(#LOGFILE, FormatDate("%hh:%ii:%ss: ", Date()))
WriteStringN(#LOGFILE, LogLevel)
WriteStringN(#LOGFILE, Message)
If *Parameters <> #Null
ForEach *Parameters\Entries()
WriteStringN(#LOGFILE, MapKey(*Parameters\Entries()) + " => " +
*Parameters\Entries())
Next
EndIf
CloseFile(#LOGFILE)
Result = #True
EndIf
EndIf
UnlockMutex(LoggerMutex)
ProcedureReturn Result
EndProcedure
Procedure ExportLogFiles(TargetLocation.s, NumberOfFiles.i = 10)
Define.i Result = #False
LockMutex(LoggerMutex)
If LoggerIsInitialized
If ExamineDirectory(#TARGETDIRECTORY, TargetLocation, "*.*")
Define i.i = 1
Define Destination.s
SortStructuredList(CurrentLogFiles(), #PB_Sort_Descending,
OffsetOf(Logfile\Date), TypeOf(Logfile\Date))
ForEach CurrentLogFiles()
If i > NumberOfFiles
Break
EndIf
Destination = TargetLocation
If Right(TargetLocation, 1) <> #Separator
Destination + #Separator
EndIf
Destination + GetFilePart(CurrentLogFiles()\Name)
CopyFile(CurrentLogFiles()\Name, Destination)
i + 1
Next
FinishDirectory(#TARGETDIRECTORY)
Result = #True
EndIf
EndIf
UnlockMutex(LoggerMutex)
ProcedureReturn Result
EndProcedure
Procedure ImportLogFiles(FromLocation.s, NumberOfFiles.i = 10)
Define.i Result = #False
LockMutex(LoggerMutex)
If LoggerIsInitialized
If ExamineDirectory(#TARGETDIRECTORY, FromLocation, "Logfile_*.txt")
Define i.i = 1
Define Destination.s
NewList FileList.Logfile()
While NextDirectoryEntry(#TARGETDIRECTORY)
AddElement(FileList())
With FileList()
\Name = DirectoryEntryName(#TARGETDIRECTORY)
\Date = DirectoryEntryDate(#TARGETDIRECTORY, #PB_Date_Created)
EndWith
Wend
SortStructuredList(FileList(), #PB_Sort_Ascending,
OffsetOf(Logfile\Date), TypeOf(Logfile\Date))
ForEach FileList()
If i > NumberOfFiles
Break
EndIf
Destination = FromLocation
If Right(FromLocation, 1) <> #Separator
Destination + #Separator
EndIf
Destination + GetFilePart(FileList()\Name)
CopyFile(Destination, Location + FileList()\Name)
i + 1
Next
FinishDirectory(#TARGETDIRECTORY)
Result = #True
EndIf
EndIf
UnlockMutex(LoggerMutex)
ProcedureReturn Result
EndProcedure
EndModule
Beispielcode:
Code: Alles auswählen
; Examplecode for SimplePBLogger
Define.s LogDirectory = GetCurrentDirectory()
Define.i MaxNumberOfLogFiles = 25
Define loggerWorks = InitializeLogger(LogDirectory, MaxNumberOfLogFiles)
; you can check loggerWorks to find out if the logger can be used now.
WriteLogEntry(#SPBL_INFORMATION, "My first entry...")
; let's transport some information to the logfile
Define.i a = 10
Define.s b = "Hello"
Define information.SPBL_Parameter ; the wrapper for the variables
information\Entries("a") = Str(a)
information\Entries("b") = b
WriteLogEntry(#SPBL_INFORMATION, "Here are some values:", information)
; Export the logfiles
ExportLogFiles(GetTemporaryDirectory(), MaxNumberOfLogFiles)
; to get the logfiles back, but maximal 10 files (standardoption):
ImportLogFiles(GetTemporaryDirectory())