用以在記錄文件中寫入自定義的調試信息(主要是時間)的組件
2024-07-21 02:15:48
供稿:網友
 
'====================================================================
'tracespy - 用以在記錄文件中寫入自定義的調試信息(開發者:林健)
'====================================================================
'
'屬性:
'       tracefilename        - 記錄文件名
'
'方法:
'   ★文本寫入方面
'       writetext            - 寫入自定義文本
'       clearalltext         - 清除所有文本
'   ★時間記錄方面
'       settimepoint         - 設置時間起點
'       gettimespanfrominit  - 詢問時間跨度(距離時間起點)
'       gettimespanfromprev  - 詢問時間跨度(距離上次詢問時間)
'
'====================================================================
public class tracespy
    '記錄文件名
    public shared tracefilename as string = "trace.txt"
    '時間起點(初始為當前時刻)
    private shared inittimepoint as long = now.ticks
    '上次詢問時間點(初始為當前時刻)
    private shared prevtimepoint as long = now.ticks
    '寫入自定義文本
    public shared sub writetext(byval str as string, optional byval showtime as boolean = false)
        tracespyfilewriter.writetext(str, showtime)
    end sub
    '清除所有文本
    public shared sub clearalltext()
        tracespyfilewriter.clearalltext()
    end sub
    '設置時間起點
    public shared sub settimepoint(optional byval note as string = "")
        inittimepoint = now.ticks
        prevtimepoint = now.ticks
        tracespyfilewriter.writetext("設置時間起點[" & note & "]。")
    end sub
    '詢問時間跨度(距離時間起點)
    public shared function gettimespanfrominit(optional byval note as string = "") as decimal
        prevtimepoint = now.ticks
        dim span as decimal
        span = cdec(prevtimepoint - inittimepoint) / 10000d
        tracespyfilewriter.writetext("詢問時間跨度[" & note & "],距離時間起點為" & span.tostring() & "毫秒。")
        return span
    end function
    '詢問時間跨度(距離上次詢問時間)
    public shared function gettimespanfromprev(optional byval note as string = "") as decimal
        dim recttimepoint as long = now.ticks
        dim span as decimal
        span = cdec(recttimepoint - prevtimepoint) / 10000d
        prevtimepoint = recttimepoint
        tracespyfilewriter.writetext("詢問時間跨度[" & note & "],距離上次詢問時間為" & span.tostring() & "毫秒。")
        return span
    end function
end class
friend class tracespyfilewriter
    private shared filewriter as system.io.streamwriter
    '向文件中寫入一個字串
    friend shared sub writetext(byval str as string, optional byval showtime as boolean = false)
        if tracespy.tracefilename = string.empty then
            exit sub
        end if
        filewriter = new system.io.streamwriter(tracespy.tracefilename, true, text.encoding.default)
        dim words as string
        words = str
        if showtime then
            words &= " @ " & now.tolongdatestring & " " & now.tolongtimestring
        end if
        filewriter.writeline(words)
        filewriter.close()
    end sub
    '清除記錄文件
    friend shared sub clearalltext()
        if tracespy.tracefilename = string.empty then
            exit sub
        end if
        filewriter = new system.io.streamwriter(tracespy.tracefilename, false, text.encoding.default)
        filewriter.write("")
        filewriter.close()
    end sub
end class菜鳥學堂: