首页 > 开发 > 综合 > 正文

用以在记录文件中写入自定义的调试信息(主要是时间)的组件

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菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表