Logfiles mit Visual Basic
Das folgende Visual Basic Modul erlaubt die einfache
Fehlersuche über Logfiles. In komplexen Projekten können bestimmte
Fehlerarten leichter über die Analyse von Logfiles gefunden werden, als
über langwierige Debug - Sessions. Sie können dieses Modul gerne zur
Fehlersuche unserer Visual Basic Schnittstellen directCONTROL, tiffSTAMP,
directINF u.a. verwenden.
- Download ErrorLog.bas (7kB)
Attribute VB_Name = "Errorlog"
'(c) MSYS Document Techology, 2003, 2004, All rights reserved
'BONUSWARE: You may use it freely.
'NO WARRANTY: It is as-is.
'
'Usage:
'
' First of all you need to initialise this module:
'
' Sub InitErrorLog(LogFile As String = "",
' LogLevel As TErrorLevel = elError,
' AppendToLogFile As Boolean = True)
' LogFile: Filename of created logfile. Empty for none.
' LogLevel: ErrorLevel of Message appended to logfile. If
' ErrorLevel < LogLevel, message is suspended. For
' ErrorLevel see LogMessage()
' AppendToLogFile: If False, Logfile is deleted first, otherwise
' Messages were appended to.
'
'
' To write a text to the logfile:
'
' Sub LogMessage(Message As String, _
' ErrorLevel As TErrorLevel = elLogging)
' Message: Your text
' ErrorLevel: The emergency level of the message. If Errorlevel
' is less than LogLevel (see InitErrorLog()), the message
' is suspended from logfile.
' For ErrorLevels equal or higher then elERRORBOX, a
' MessageBox is displayed with OK and CANCEL - Button. If
' the user presses CANCEL, the UserAbort-Flag is set. You
' should query this flag with isUserAbort() and abort your
' application as quickly as possible.
'
' For your convenience, there are some shortcuts to LogMessage():
'
' Sub LogWarning(Message As String)
' Sub LogError(Message As String)
' Sub ShowErrorBox(Message As String)
' Sub LogFatalError(Message As String)
'
' To check, if there was an error:
' Function isError() As Boolean
'
' To clear any error - conditions and reset the ErrorCount and
' WarningCount values:
' Sub ClearError()
'
' Assertion: Checks an Value and throws an log-entry only if an invalid
' state is entered.
'
' To watch that a Condition is always TRUE:
' Sub LogAssert(Condition As Boolean,
' Message As String)
'
' To watch that an integer keeps inside a range:
' Sub LogAssertRange(RangeLow As Integer,
' RangeHigh As Integer,
' Value As Integer,
' Message As String)
'
' UserAbort: Check if user whishes to terminate the application
' To Check userabort:
' Function isUserAbort() As Boolean
'
' To set Userabort - condition and throw condition to logfile:
' Sub SetUserAbort()
'
' To Reset Userabort - condition:
' Sub ClearUserAbort()
'Defines the level of "emergency" of an message
Public Enum TErrorLevel
elLogging = 0 'Just write to logfile, if enabled
elWarning = 1 'Log it as warning
elError = 2 'Error condition
elAssert = 3 'Debugging: Check ranges and values
elErrorBox = 4 'Error condition, showing an ErrorBox
elFatal = 5 'Fatal error condition
End Enum
'Last error message
Public LastErrorMsg As String
'Last error or warning message
Public LastMsg As String
'Count of fatal error - messages since ClearError() or InitErrorLog()
Public ErrorCount As Integer
'Count of nonfatal warning - messages since ClearError() or InitErrorLog()
Public WarningCount As Integer
'Total count of errors and warnings since InitErrorLog()
Public TotalErrorCount As Integer
Public TotalWarningCount As Integer
'---- PRIVATE -----------------------------------------------
'flag for userabort - condition
Private fUserAbort As Boolean
'Min. message level to write message to logfile
Private fLogLevel As TErrorLevel
'Name of logfile
Private fLogFile As String
'Init this unit
Public Sub InitErrorLog(Optional ByVal LogFile As String = "", _
Optional ByVal LogLevel As TErrorLevel = elError, _
Optional AppendToLogFile As Boolean = True)
ClearError
TotalErrorCount = 0
TotalWarningCount = 0
fUserAbort = False
fLogLevel = LogLevel
fLogFile = LogFile
If (fLogFile <> "") And Not AppendToLogFile Then
On Error Resume Next
Kill fLogFile
End If
LogMessage "*** APPLICATION CREATED"
End Sub
'Clears all error - conditions.
Public Sub ClearError()
LastMsg = ""
LastErrorMsg = ""
ErrorCount = 0
WarningCount = 0
End Sub
'Set userabort - condition
Public Sub SetUserAbort()
LogMessage "User abort"
fUserAbort = True
End Sub
Public Sub ClearUserAbort()
fUserAbort = False
End Sub
Public Function isUserAbort() As Boolean
isUserAbort = fUserAbort
End Function
Public Function isError() As Boolean
isError = ErrorCount > 0
End Function
Public Sub LogError(ByVal Message As String)
LogMessage Message, elError
End Sub
Public Sub LogWarning(ByVal Message As String)
LogMessage Message, elWarning
End Sub
Public Sub ShowErrorBox(ByVal Message As String)
LogMessage Message, elErrorBox
End Sub
Public Sub LogFatalError(ByVal Message As String)
LogMessage Message, elFatal
End Sub
Public Sub LogAssert(Condition As Boolean, _
ByVal Message As String)
If Condition Then _
LogMessage Message, elAssert
End Sub
Public Sub LogAssertRange(ByVal RangeLow As Integer, _
ByVal RangeHigh As Integer, _
ByVal Value As Integer, _
ByVal Message As String)
If (Value < RangeLow) Or (Value > RangeHigh) Then _
LogMessage Message, elAssert
End Sub
Public Sub LogMessage(ByVal Message As String, _
Optional ByVal ErrorLevel As TErrorLevel = elLogging)
Dim Str As String
Dim LogFile
'Update error statistic
If ErrorLevel >= elError Then
ErrorCount = ErrorCount + 1
TotalErrorCount = TotalErrorCount > 0
LastErrorMsg = Message
ElseIf ErrorLevel > elLogging Then
WarningCount = WarningCount + 1
TotalWarningCount = TotalWarningCount + 1
End If
'Update last Errormsg
If ErrorLevel > elLogging Then _
LastMsg = Message
'Write-through to Logfile
If (fLogFile <> "") And (ErrorLevel >= fLogLevel) Then
'Create log-string
Select Case ErrorLevel
Case elWarning:
Str = "WARNING: "
Case elError:
Str = "ERROR: "
Case elAssert:
Str = "ASSERT: "
Case elErrorBox:
Str = "ERROR (BOX): "
Case elFatal:
Str = "FATALERROR: "
Case Else
Str = ""
End Select
Str = "[" + CStr(Now) + "]: " + Str
'Write-trough to logfile. Opening the logfile for
'any message is slow - but enables you to read the
'correct file if the application crashes.
LogFile = FreeFile 'Search free file number
Open fLogFile For Append Access Write Shared As #LogFile
Print #LogFile, Str + Message
Close #LogFile
End If 'LogFile
If (ErrorLevel >= elErrorBox) Then
If ErrorLevel > elErrorBox Then
Str = "Fataler Fehler"
Else
Str = "Fehler"
End If
If MsgBox(Message, vbOKCancel, Str) = vbCancel Then
SetUserAbort
End If
End If
End Sub
|
|
M-SYS Document Technology, Haseldorfer Str. 20B, D-25492 Heist, Germany
Sales: +49 (0)4122 9509047, Fax: +49 (0)4122 9509077, www.directtools.de, © 15.03.2011
|