Search code examples
debuggingloggingvbscriptintrospectionprintf-debugging

How to extract context informationm the equivalent of __LINE__, __FILE__,etc., in VBSCRIPT


I'd like to know how to get the line number of a line in vbscript programmaticly either at the point of the code like __LINE__ or more ideally a way to get the line number of where the current function was called like python's stack module so I can write a reusable debugging function(and the file the code is located in) and no I don't want to know how to turn on line numbers in my editor.

Also I'd like to now any similar useful information that can be extracted such as calling function, variable type as string, etc.


Solution

  • Unfortunatly that doesn't work the way like in Ruby and Python. The next best thing i worked out is putting a call to a errorhandling function everywhere where things could go wrong. The numbers in the parameter of this function are adapted each time i execute a macro in my editor (i use textpad, the \i is autonumbering in a Regular Expression). If your editor doesn't support this you could write a script that does this. So when an error occurs, it is logged with the number the errorhandling function was called and you can easily find it back in the source by looking for #number#.

    This is usable for both asp and vbs but for vbs there is an easier way. Some editors like textpad or sublimle text let you execute a vbs script, show the output in a tab and if an error is produced let you double click the line with the errormessage which opens the script at that line. This is also done by a regular expression. Let me know if you need the one for textpad.

    on error resume next
    'initialize constants DEBUGLEVEL and LOGFILE
    'initialize strHostName
    
    'some code
    oConn.execute(sql)
    
    if not LogError("#1#") then
      'do the things if successfull, otherwise log error with number
    end if
    
    'again some code
    if not LogError("#2#") then
      'do the things if successfull, otherwise log error with number
    end if
    
    'the debug and log functions
    function LogError(errornumber)
      'LogError\(\"#[0-9]+#\"\) replace by LogError("#\i#")
      if err.number <> 0 then
        call debug("<name of script>/Logerror","","","Errornumber:" _
          & errornumber & " " & err.number & " " & err.description & " " _
          & err.source)
        LogError = True
        err.clear
        errors = errors+1
      else
        LogError = False
      end if
    end function
    
    function Debug (pagina, lijn, varnaam, varinhoud)
      if DEBUGLEVEL > 0 then
        const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
        dim fs,f, var, strHostName
        set fs=CreateObject("Scripting.FileSystemObject")
        strHostName    = fs.GetFileName(WScript.FullName)
        if fs.FileExists(LOGFILE) then
          set f=fs.OpenTextFile(LOGFILE, forAppending)
        else
          set f=fs.OpenTextFile(LOGFILE, forWriting,true)
        end if
        var = now & " " & pagina & ":" & lijn & ":" & varnaam & ":" & varinhoud
        f.WriteLine var
        if LCase(strHostName) = "cscript.exe" then 'debugging
          if DEBUGLEVEL > 1 then
            wscript.echo var
          end if
        end if
        f.Close
        set f=Nothing
        set fs=Nothing
      end if
      debug = true
    end function