VBScriptで、現在実行中の関数の名前を判別することは可能ですか?
.NETでは、次のことができます。
MethodBase method = MethodBase.GetCurrentMethod();
Console.WriteLine(method.Name);
VBScriptで、現在実行中の関数の名前を判別することは可能ですか?
.NETでは、次のことができます。
MethodBase method = MethodBase.GetCurrentMethod();
Console.WriteLine(method.Name);
以前は、コールスタック ビューアーを作成して、呼び出された各関数のパフォーマンスを確認していました。これには、関数/サブごとに 1 行の追加の VBS コードが必要であり、追加のコードが原因で、もちろん実行時にいくらかのオーバーヘッドが必要です。
一気飲み:
Function DoSomething(a, b, c)
dim registerFunctionObj : Set registerFunctionObj = [new RegisterFunction]("DoSomething")
' other code
End Function
関数が呼び出されるたびに、RegisterFunction オブジェクトの新しいインスタンスが作成されます。関数が終了すると、registerFunctionObj
変数は自動的にスコープ外になり、インスタンスの Class_Terminate サブルーチンが呼び出されます。
[new RegisterFunction]
registerFunction インスタンスを返す単なる関数です。
Function [new RegisterFunction](funcName)
Set [new RegisterFunction] = new cls_RegisterFunction
[new RegisterFunction].FunctionName = funcName
Set [new RegisterFunction].CallStackViewer = CallStackViewer
End function
Class cls_RegisterFunction
Private functionName_, startTime_, callStackViewer_, endTime_
Private Sub Class_Initialize
startTime_ = now
callStackViewer_.LogInitialize me
End Sub
Public Property Let FunctionName(fName)
functionName_ = fName
End Property
Public Property Set CallStackViewer(byRef csv)
Set callStackViewer_ = csv
End Property
Private Sub Class_Terminate
endTime_ = now
callStackViewer_.LogTerminate me
End Sub
End Class
CallStackViewer インスタンスは CallStackViewer クラスのシングルトン インスタンスですが、プロジェクトの一部にすることができるため、グローバル プロジェクト クラスから取得します。
Private PRIV_callStackViewer
Public Function CallStackViewer()
If not IsObject(PRIV_callStackViewer) Then
Set PRIV_callStackViewer = new cls_CallStackViewer
End If
Set CallStackViewer = PRIV_callStackViewer
End Function
Class cls_CallStackViewer
Public Sub Class_Initialize
' Here you can retrieve all function libraries (as text file) extract the
' function name, the file they are in and the linenumber
' Put them in a dictionary or a custom object
End Sub
Public Sub LogInitialize(byref registerFunction)
' Here you can push the function on a stack (use a standard dotnet list>stack for it),
' log the starttime to a log object or handle custom breakpoints
End Sub
Public Sub LogTerminate(byref registerFunction)
' Here you can pop the function from a stack, log the endtime to a log
' object or handle custom breakpoints
End Sub
End Class
免責事項: ここにあるコードは、オンザフライで作成された純粋なデモ コードです。機能が不足しており、概念を説明するためだけにここにいます。エラーが含まれている可能性があり、完全ではありません。
必要なのは、関数ごとに 1 行のコードと、それを拡張するための想像力だけです。
いいえ、でも簡単に実装できます
dim module_name
sub sub1
module_name = "sub1"
wscript.echo "i'm " & module_name
'do something
end sub
function function1
module_name = "function1"
wscript.echo "i'm " & module_name
function1 = "something"
end function
再帰の場合、自分がいるレベルを覚えておくこともできます。これにより、深くなりすぎた場合に抜け出すことができます。