Logging Patterns: OutputDebugString , Leverage DebugView

In this article, we look at an effective logging method using the Windows API OutputDebugString and DebugView utilities in Excel VBA.

1. Basic Logging Patterns

Excel VBA Courses (9): Excel VBA How-To

of the above 4.3. How to use the Immediate Window tool In the table of contents, “3. Checking the Output Message “was covered.

직접 실행 창 도구 활용 방법
How to use the Immediate Window tool

This is a way to check the contents output by Debug.Print in the Immediate Execution window. While this method has the advantage of being very simple, it has the following disadvantages.

  • VBE (Visual Basic Editor) cannot check the contents of the output log when the Excel process is stopped.
  • Among the above usage methods, “2. If you use it together with “checking variable values during execution”, log and variable values are mixed in the middle, which can interfere with log verification.
  • Messages longer than 200 lines are deleted. That is, only the last 200 lines are maintained.

A better way to replace Debug.Print is to use OutputDebugString, a Windows API, and DebugView, a utility.

2. OutputDebugString usage pattern

2.1. VBA code using OutputDebugString

In the article below, I introduced how to use the Windows API, OutputDebugString.

Optimization of work distribution using one-dimensional bin packing algorithm_4.Attachment

5.1.4. modUtil module source code By slightly modifying the code introduced in , the entire necessary contents were written as follows.

Option Explicit
Public Const LOG_PREFIX As String = "[VBA] " 'DebugView에서 로그 메시지를 필터링하기 위한 Prefix 지정

Private Declare PtrSafe Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal lpOutputString As String)

'OutputDebugString API를 이용한 Debug Message 출력
'DebugView등을 이용하여 메시지 View 가능함
Public Sub DoLog(aMsg As String)
    OutputDebugString LOG_PREFIX + aMsg
End Sub

'Log 예시 프로시져
Public Sub TestLog()
    Dim lIdx As Long
    For lIdx = 1 To 1000
        DoLog "Log[" + CStr(lIdx) + "]"
    Next
End Sub
  • Line 2: Specify a prefix for filtering log messages in DebugView. The filtering method is described below.
  • Line 4: Import the Windows API OutputDebugString and declare it as a function.
  • Line 9: Combine LOG_PREFIX and log message, pass it as a parameter to the function imported in line 4, and execute it. Make this process a DoLog function.
  • Line 16: Call the DoLog function where the log message output is required.

At this point, all the work required in the VBA code is complete. Next, let's look at the DebugView utility.

2.2. Introduction and use of the DebugView utility

2.2.1. Introduction to the DebugView utility

DebugView Sysinternals suiteIt is a utility tool included in .

DebugView – Windows Sysinternals | Microsoft Docs

DebugView v4.90 소개
Introducing DebugView v4.90

DebugView version 4.90

Mark Russinovich
Posted on: April 23rd, 2019
Download DebugView (1.3 MB)

Introduce
Debugview is an application that allows you to monitor debug output on your local system or on any computer on a network reachable via tcp/ip. It can display both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your application or device driver generates, and you don't have to modify your application or driver to use non-standard debug output APIs.

Capture DebugView
On Windows 2000, XP, Server 2003 and Vista debugview captures:
  – Win32 OutputDebugString
  – Kernel-mode DbgPrint
  – All kernel-mode variants of DbgPrint implemented in Windows XP and Server 2003

Briefly summarized, “DebugView is a utility tool that captures the string passed when calling OutputDebugString, a Windows API, and displays it on the screen”.

DebugView example screen is as follows.

DebugView 예시 화면
DebugView example screen

* source: https://docs.microsoft.com/ko-kr/sysinternals/downloads/debugview#installation-and-use

2.2.2. Utilize the DebugView utility

Download and run DebugView, then run the Filter/Highlight menu.

DebugView Filter/Highlight 메뉴
DebugView Filter/Highlight menu

When the above menu is executed, the following screen is displayed.

DebugView Filter/Highlight 화면
DebugView Filter/Highlight screen
  • Include: Enter a string included in the message to be displayed. Use * as a wildcard character (eg [VBA]*) to output all messages if empty. When entering multiple strings, separate them with semicolons (;).
  • Exclude: Enter a string included in the message not to be output. When entering multiple strings, separate them with semicolons (;).
  • Highlight: Select Filters 1 to 20 and select a string and color to highlight. Foreground color and background color can be set

One thing to note is that if you do not enter a string to filter in Include, the messages output from different processes will be captured and mixed up. It is recommended that the string to be filtered be specified as required.

“2.1. To capture the result of the example VBA code written in the table of contents of “VBA Code”, enter “[VBA]*” in Include and click the OK button.

If you execute the VBA code, you can check the message captured in DebugView as follows.

For reference, using the Highlight function, you can output something like this:

DebugView의 Highlight 기능 예시
Example of DebugView's Highlight function

If a background color and foreground color are different when a specific string is included, it is visually very good to check the current progress of the log.


So far, we have looked at effective logging methods using the Windows API OutputDebugString and DebugView utilities in Excel VBA.

DebugView has many attractive features such as Highlight and Remote Logging. These features will be covered later in a separate article.


DA# Macro function demonstration video (YouTube) In the post, I recorded the situation where messages are captured in the DebugView utility.

In the video below, the upper right corner is the DebugView. You can record a log of how the function works and review it later.

Note that, DA# Macro(1): DA#, DA# API, DA# Macro Overview The DA# Macro introduced in , uses LOG_PREFIX as “[DA#]”.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish