Search notes:

VBA classes for hierarchical logging

dbg.cls

dbg is a class that can be used for hierarchical logging.
Calling indent increases the indent-level, dedent decreases it.
The method text actually writes the passed string to the destination. The string is prepended with twice the amount of current indent levels of spaces.
When the class is initialized with the method init, a derived class of dbgWriter needs to be given. Currently, there are four derived classes:

Interface

init
indent / dedent Indent or dedent the level by one. indent can optionally be passed a string.
text Write the text with current indent level
text_ Private method that calls the instance referred to by dbgWriter to write some text to the respective output destination
undhandledError write err.description, call dedent and pass error to calling function
removeSpecialCharacters Private function to remove new lines and curly braces from the string passed to text
checkIndentation Support function to check if indent is messed up.

Source code for the dbg object

'
' vim: ft=vb
'
' dbg.cls
'
option explicit

private indent_     as integer
private dbgWriter_  as dbgWriter

private function removeSpecialCharacters(txt as string) ' {

  removeSpecialCharacters = replace(txt                    , "{"    , "[")
  removeSpecialCharacters = replace(removeSpecialCharacters, "}"    , "]")
  removeSpecialCharacters = replace(removeSpecialCharacters, chr(10), " ")
  removeSpecialCharacters = replace(removeSpecialCharacters, chr(13), " ")

end function ' }

public sub init(dbgWriter__ as dbgWriter) ' {
    indent_      = 0
    set dbgWriter_  = dbgWriter__
end sub ' }

private sub text_(txt as string) ' {
  dbgWriter_.out format(now(), "yyyy-mm-dd hh:MM:ss") & " " & space(indent_) & txt
end sub ' }

public sub text(txt as string) ' {
     text_ removeSpecialCharacters(txt)
end sub ' }

public sub indent(txt as string) ' {

  if not enabled then
     exit sub
  end if

  text_ "{ " & removeSpecialCharacters(txt)

  indent_ = indent_ + 2
end sub ' }

public sub dedent() ' {

  if not enabled then
     exit sub
  end if

  indent_ = indent_ - 2

  if indent_ < 0 then
     indent_ = 0
     text "! dbg: Warning, indent_ was set to 0"
  end if
  text_ "}"

end sub ' }

public sub unhandledError() ' {

    text ("Caught unhandled error: " & err.description)
    dedent
    err.raise err.number, err.source, err.description

end sub ' }

function enabled() as boolean ' {

' if environ$("username") = "René" and environ$("computername") = "THINKPAD" then
     enabled = true
' else
'    enabled = false
' end if

end function ' }

public sub checkIndentation(expectedIndent as long, txt as string) ' {
    if indent_ <> expectedIndent then
       text_("indent check failed, " & txt)
    end if
end sub ' }
Github repository VBAModules, path: /dbg/dbg.cls

dbgWriter.cls

dbgWriter is an interface that is used by dbg to actually write debug text to a destination.
'
'  vim: ft=vb
'
'  dbgWriter.cls
'
option explicit

sub out(txt as string) ' {
end sub ' }
Github repository VBAModules, path: /dbg/dbgWriter.cls
Classes that implement dbgWriter include

See also

VBA built in class debug
VBA Module Debug [Common]
René's VBA Modules

Index