:: On Fri, 06 Dec 2019 04:47:59 -0500
:: (comp.lang.basic.visual.misc,microsoft.public.vb.general.discussion)
:: <qsd84j$7n4$
1...@dont-email.me>
:: GS <g...@v.invalid> wrote:
> Got it!
> Thanks a lot; - that's very handy to have.
Y/W, I slightly modified/improved the code to speed it up a little bit
and to make it a bit more readable, by the way, feel free to modify it
as you want :)
' =====================================================================
' CDbgLog.cls - class to send debug/trace messages to a debug console
' like the "debugview" from SysInternals
'
' the class allows to specify a bitmask to enable/disable selected
' message types, here's an usage example
'
' Set gCDbg = New CDbgLog
' gCDbg.LogLevel = mtError + mtWarning + mtDebug
' gCDbg.DbgPrint "The application is starting", mtInformation
' gCDbg.DbgPrint "Current date is " & Now(), mtDebug
'
' the above will only enable error, warning and debug messages and will
' ignore (drop w/o sending to the debugger) all the others, so running
' the code you'll only see the "current date" message and not the other
' notice that omitting the message type, it defaults to "information"
' =====================================================================
Option Explicit
' message types (and log level bitmask)
Public Enum enMsgType
mtNone = 0 ' no messages at all
mtError = 1 ' error
mtWarning = 2 ' warning
mtInformation = 4 ' information
mtVerbose = 8 ' detailed information
mtDebug = 16 ' debugging information
mtUserDef = 32 ' user defined
mtEverything = 255 ' any message
End Enum
' Debugger API
Private Declare Sub OutputDebugString Lib "kernel32" _
Alias "OutputDebugStringA" _
(ByVal lpString as String)
' private workareas
Private msAppName As String ' application name
Private mbIsIDE As Boolean ' true=in IDE
Private mnLogFlags As enMsgType ' enabled message types bitmask
' initializes the class module
Private Sub Class_Initialize()
On Local Error Resume Next
' setup basic informations
msAppName = "[" & App.ExeName & "]"
mnLogFlags = mtError
' set the "in IDE" flag
mbIsIDE = False
Err.Clear
Debug.Print 1/0
If Err.Number <> 0 Then
mbIsIDE = True
End If
' all done
Err.Clear
End Sub
' set the logging flags (bitmask)
Public Property Let LogFlags(ByVal nFlags As enMsgType)
mnLogFlags = nFlags
End Property
' reat the logging flags (bitmask)
Public Property Get LogFlags() As enMsgType
LogFlags = mnLogFlags
End Property
' send a message to the debugger
Public Sub DbgPrint(ByVal sStr As String, _
Optional ByVal nType As enMsgType = mtInformation)
Dim sType As String, sMsg As String
' check if logging for this message type is enabled
If ((mnLogFlags And nType) = 0) Then
Exit Sub
End If
' base message type and text
sMsg = MsgType(nType) & sStr
' check if IDE or runtime
If mbIsIDE Then
' if in IDE, send to immediate window
Debug.Print sMsg
Else
' not in IDE compose and send to the debugger
sMsg = msAppName & sMsg & vbCrlf & Chr(0)
Call OutputDebugString(sMsg)
End If
End Sub
' decode a message type to string
Private Function MsgType(ByVal nType As enMsgType) As String
Dim sType As String
' check the type and set the string
Select Case nType
Case mtError
sType = "[ERROR ] "
Case mtWarning
sType = "[WARNING ] "
Case mtInformation
sType = "[INFORMATION] "
Case mtVerbose
sType = "[VERBOSE ] "
Case mtDebug
sType = "[DEBUG ] "
Case mtUserDef
sType = "[USERDEFINED] "
Case Else
' unknown, use the type number (hex)
sType = Left(Right("0000" & Hex(nType), 4) & Space(11), 11)
sType = "[" & sType & "]"
End Select
' all done
MsgType = sType
End Function