News:

Function Finder  Find all the functions within source code files

Main Menu

Logging library

Started by thaaks, September 16, 2008, 04:51:29 PM

Previous topic - Next topic

thaaks

Hi,
I wrote a nice little file logging library (or more a collection of functions) that might be of some use for some people.

It works like this:

  • There are five log levels supported, namely LOWDEBUG, DEBUG, INFO, WARNING and ERROR. So each logged message must be one of those levels.
  • Each log message will be appended to a given filename with date and timestamp and log level at the beginning of the message (automatically inserted).
  • You can switch all logging functionality on or off at any time.
  • You can switch debug and lowdebug messages on or off at any time. This allows you to do detailed logging in one part of your program/game and high level logging in other parts.
  • INFO, WARNING and ERROR level messages can not be switched on or off separately. There is no reason to switch off error logging, except you want to switch off all logging.

You first need to call the init function at the beginning of your program/game where you have to pass in the filename for the logfile and three flags: logging on or off, debug messages on or off, low debug messages on or off.


LogInit("mygame.log", 1, 1, 1) ; logging with all debug informations switched on


Afterwards you use the appropriate LogXXX functions to add log messages to your log file.

LogLowDebug("I am a low debug message")
LogDebug("I am a debug message")
LogInfo("I am a info message")
LogWarning("I am a warning message")
LogError("I am a error message")


You can switch logging of debug and lowdebug messages on or off any time by calling

LogSetDebug(debugOn, lowDebugOn)


You can switch all logging  on or off any time by calling

LogSetLogging(loggingOn)


That's pretty much it. Simple but helpful.

And here's the code, have fun with it!  ;)

Cheers,
Tommy

PlayBASIC Code: [Select]
Type TLoggingInfo
FileName As String
FileHandle As Integer
LoggingIsOn As Integer
DebugIsOn As Integer
LowDebugIsOn As Integer
EndType

Dim LoggingInfo As TLoggingInfo


Constant LOWDEBUG = 1
Constant DEBUG = 2
Constant ERROR = 3
Constant WARNING = 4
Constant INFO = 5
Constant MSG_TYPES = 5

Dim LogTypeNames$(MSG_TYPES+1)

LoggingInfo.FileName = "log.txt"

Data "LOWDEBUG", "DEBUG ", "ERROR ", "WARNING ", "INFO "

Function LogInit(filename$, loggingOn, debugOn, lowDebugOn)
Local i

LoggingInfo.FileName = filename$
LoggingInfo.LoggingIsOn = loggingOn
LoggingInfo.DebugIsOn = debugOn
LoggingInfo.LowDebugIsOn = lowDebugOn
Restore "LOWDEBUG"
For i = 1 To MSG_TYPES
LogTypeNames$(i) = ReadData$()
Next
EndFunction True


Function LogSetFilename(filename$)
LoggingInfo.FileName = filename$
EndFunction True


Function LogSetDebug(debugOn, lowDebugOn)
LoggingInfo.DebugIsOn = debugOn
LoggingInfo.LowDebugIsOn = lowDebugOn
EndFunction True


Function LogSetLogging(loggingOn)
LoggingInfo.LoggingIsOn = loggingOn
EndFunction True


Function LogMsg(LogType, msg$)
Local size, logtypename$

If LoggingInfo.LoggingIsOn = False
Exitfunction False
EndIf
If LoggingInfo.DebugIsOn = False And LogType = DEBUG
Exitfunction False
EndIf
If LoggingInfo.LowDebugIsOn = False And LogType = LOWDEBUG
Exitfunction False
EndIf
LoggingInfo.FileHandle = GetFreeFile()
; check if file exists. if not create an empty one
If FileExist(LoggingInfo.FileName$)=False Then WriteFile LoggingInfo.FileName$, LoggingInfo.FileHandle: CloseFile LoggingInfo.FileHandle
; get the size of the file to place the string behind it at the end
size = FileSize(LoggingInfo.FileName$)
; open the file for random access
OpenFile LoggingInfo.FileName$, LoggingInfo.FileHandle
WriteStringAt LoggingInfo.FileHandle, size, CurrentDate$()+ ", " + CurrentTime$()+ ": " + LogTypeNames$(LogType) + ": " + msg$
CloseFile LoggingInfo.FileHandle
EndFunction True


Function LogInfo(msg$)
Exitfunction LogMsg(INFO, msg$)
EndFunction False


Function LogWarning(msg$)
Exitfunction LogMsg(WARNING, msg$)
EndFunction False


Function LogError(msg$)
Exitfunction LogMsg(ERROR, msg$)
EndFunction False


Function LogDebug(msg$)
Exitfunction LogMsg(DEBUG, msg$)
EndFunction False


Function LogLowDebug(msg$)
Exitfunction LogMsg(LOWDEBUG, msg$)
EndFunction False




Adaz


Ádáz

Hungary