extend Logger

This commit is contained in:
René Linder 2021-05-21 08:04:40 +02:00
parent 8013677c37
commit d34a4ed900

View file

@ -2,6 +2,10 @@
;* ;*
;* lhs_log.pbi ;* lhs_log.pbi
;* ;*
;* Single Logthread logger.
;*
;* For multiple logthread use lhs_log_ext.pbi
;*
;* (c) by René Linder ;* (c) by René Linder
;* (c) by Linder Hard- und Software ;* (c) by Linder Hard- und Software
;* ;*
@ -12,10 +16,12 @@
;* lhs_log::App_Name = "Test App" ;* lhs_log::App_Name = "Test App"
;* lhs_log::SetLogFile("test_app.log")) ;* lhs_log::SetLogFile("test_app.log"))
;* lhs_log::SetMaxSize(32) ;* lhs_log::SetMaxSize(32)
;* lhs_log::SetLogLevel(lhs_log::#Error)
;* lhs_log::SetLogDateFormat("%yyyy.%mm.%dd %hh:%ii:%ss") ;* lhs_log::SetLogDateFormat("%yyyy.%mm.%dd %hh:%ii:%ss")
;* lhs_log::SetLogFileDateFormat("%yyyy_%mm_%dd_%hh_%ii_%ss") ;* lhs_log::SetLogFileDateFormat("%yyyy_%mm_%dd_%hh_%ii_%ss")
;* lhs_log::Init() ;* lhs_log::Init()
;* lhs_log::Out("Log started.", #lhs_log::#Debug) ;* lhs_log::Out("Log started.", #lhs_log::#Debug)
;* lhs_log::OutL(lhs_log::#Info, "Info Level")
;* lhs_log::Close() ;* lhs_log::Close()
;} ;}
CompilerIf #PB_Compiler_Thread <> 1 CompilerIf #PB_Compiler_Thread <> 1
@ -28,12 +34,20 @@ DeclareModule lhs_log
#Debug = 1 #Debug = 1
#NoDebug = 0 #NoDebug = 0
Enumeration LogLevel
#Info
#Warning
#Error
EndEnumeration
Declare SetLogFile(LogFileName.s) Declare SetLogFile(LogFileName.s)
Declare SetMaxSize(Megabyte.i) Declare SetMaxSize(Megabyte.i)
Declare SetLogLevel(Level.i)
Declare SetLogFileDateFormat(DateFormat.s) Declare SetLogFileDateFormat(DateFormat.s)
Declare SetLogDateFormat(DateFormat.s) Declare SetLogDateFormat(DateFormat.s)
Declare Init() Declare Init()
Declare Out(ToLog.s, debugger.i = #NoDebug) Declare Out(ToLog.s, debugger.i = #NoDebug)
Declare OutL(LogType.i, ToLog.s, debugger.i = #NoDebug)
Declare Close() Declare Close()
EndDeclareModule EndDeclareModule
@ -54,6 +68,7 @@ Module lhs_log
Global ExitSemaphore = CreateSemaphore() Global ExitSemaphore = CreateSemaphore()
Global MaxSize.q = 32 * 1024 * 1024 Global MaxSize.q = 32 * 1024 * 1024
Global LastSize.q = 0 Global LastSize.q = 0
Global LogLevel.i = #Info
Global LogFileDateFormat.s = "%yyyy_%mm_%dd_%hh_%ii_%ss" Global LogFileDateFormat.s = "%yyyy_%mm_%dd_%hh_%ii_%ss"
Global LogDateFormat.s = "%yyyy.%mm.%dd %hh:%ii:%ss" Global LogDateFormat.s = "%yyyy.%mm.%dd %hh:%ii:%ss"
Global NewList Messages.s() Global NewList Messages.s()
@ -66,6 +81,19 @@ Module lhs_log
EndIf EndIf
EndProcedure EndProcedure
Procedure SetLogLevel(Level.i)
Select Level
Case #Info
LogLevel = #Info
Case #Warning
LogLevel = #Warning
Case #Error
LogLevel = #Error
Default
LogLevel = #Error
EndSelect
EndProcedure
Procedure SetLogFile(LogFileName.s) Procedure SetLogFile(LogFileName.s)
Protected.i TestFile_ID Protected.i TestFile_ID
@ -168,6 +196,22 @@ Module lhs_log
SignalSemaphore(Semaphore) SignalSemaphore(Semaphore)
EndProcedure EndProcedure
Procedure OutL(LogType.i, ToLog.s, debugger.i = #NoDebug)
If LogType >= LogLevel
Select LogType
Case #Info
ToLog = "Info: " + ToLog
Case #Warning
ToLog = "Warning: " + ToLog
Case #Error
ToLog = "Error: " + ToLog
Default
ToLog = "Error: " + ToLog
EndSelect
Out(ToLog, debugger)
EndIf
EndProcedure
Procedure Close() Procedure Close()
Exit=1 Exit=1
SignalSemaphore(Semaphore) SignalSemaphore(Semaphore)