重拾VB之四,日志加强篇
关劲松 PMP
一、关于日志记录功能
主要功能点有
1 在程序目录下生成log目录,用于保存日志文件。
2 自动在log目录中,生成日志文件,文件命名用年月日定义,如20100119_log.log。
3 在日志文件中,每行记录一个时间点的运行内容。时间点的精度应该达到毫秒。
4 在日志文件中,每一行的记录内容为文本信息。
5 在需要记录日志的地方,调用记录日志过程。
二、相关过程、函数。
'* 备注:由于FSO对象包含在Scripting 类型库 (Scrrun.Dll)中,所以在使用前首先需要在在工程中引用这个文件,
'* 单击“工程”,“引用”,然后在“引用”对话框中选中“Microsoft Scripting Runtime”前的复选框,然后单击“确定”。
'*-----------------------------------------------------------------------------*
'* 注释内容,用于获取系统时间的毫秒。
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
'*-----------------------------------------------------------------------------*
'*-----------------------------------------------------------------------------*
'* 注释内容,用于保存详细log文件的方法。使用中。
Public Sub Dtllog(ByVal logText As String)
Dim fso As FileSystemObject
Dim fileName As String
Dim a As Object
Set fso = New FileSystemObject
If fso.FolderExists(App.Path & "/log/") Then
Set a = fso.OpenTextFile(App.Path & "/log/" & Format(Now, "yyyymmdd_") & "log.log", ForAppending, True) '记录文件在工程的LOG子目录下
Else
fso.CreateFolder (App.Path & "/log/")
Set a = fso.OpenTextFile(App.Path & "/log/" & Format(Now, "yyyymmdd_") & "log.log", ForAppending, True) '记录文件在工程的LOG子目录下
End If
a.WriteLine (logText) '日期时间记录文本
a.Close
End Sub
'*-----------------------------------------------------------------------------*
'*-----------------------------------------------------------------------------*
'* 注释内容,用于生成按日期时间命名文件名的函数。使用中。
Public Function getfilename() As String
getfilename = App.Path & "/log/" & Format(Now, "yyyymmdd_hhmmss_") & "error"
End Function
'*-----------------------------------------------------------------------------*
'*-----------------------------------------------------------------------------*
'* 注释内容,用于获取系统时间毫秒的函数。使用中
Public Function getdatetime() As String
Dim LCT As SYSTEMTIME
Dim ymd As String, hms As String
GetLocalTime LCT
ymd = Format(LCT.wYear & "-" & LCT.wMonth & "-" & LCT.wDay, "yyyy-MM-dd")
hms = Format(LCT.wHour, "00") & ":" & Format(LCT.wMinute, "00") & ":" & Format(LCT.wSecond, "00") & "." & Format(LCT.wMilliseconds, "000")
getdatetime = ymd & " " & hms
'MsgBox ymd & " " & hms
End Function
'*-----------------------------------------------------------------------------*
三、调用过程
'*-----------------------------------------------------------------------------*
'* 注释内容,用于输出日志到信息框和日志文件,输入参数包括文本内容和字体颜色。使用中
Public Sub OutputText(Str As String, colour As Long)
WindowText.SelStart = Len(WindowText.Text)
WindowText.SelColor = colour
WindowText.SelText = getdatetime & " " & Str & vbCrLf
WindowText.SelColor = vbBlack
'在界面上用checkbox控制是否记录日志
If CckBxDtllog.Value Then
Call Oplog.Dtllog(getdatetime & " " & Str & vbCrLf) '调用过程记录日志。因为是TXT文本文件,字体颜色只能用黑色。
End If
End Sub
'*-----------------------------------------------------------------------------*
四、日志输出格式
时间戳 + 文本内容
对OUD下发数据的例子:
2010-01-19 00:00:20.404 (F6 28 9 9 1 4F EF 7 68 A0 67 )
2010-01-19 00:00:20.591 Execution succeeds.
2010-01-19 00:00:21.795 F6 28 9 12 2 4F EF 0 7 68 0 0 0 0 0 0 0 0 B0 66
2010-01-19 00:00:21.795 (F6 28 11 9 1 4F EF 7 70 97 88 )
2010-01-19 00:00:21.982 Execution succeeds.
2010-01-19 00:00:23.185 F6 28 11 12 2 4F EF 0 7 70 0 0 0 0 0 0 0 0 F8 E8
2010-01-19 00:00:23.185 (F6 28 19 9 1 4F EF 7 78 85 2D )
2010-01-19 00:00:23.373 Execution succeeds.
2010-01-19 00:00:24.576 F6 28 19 12 2 4F EF 0 7 78 0 0 0 0 0 0 0 0 C0 92
2010-01-19 00:00:24.576 (F6 28 21 9 1 4F EF 7 80 21 1A )
2010-01-19 00:00:24.763 Execution succeeds.
2010-01-19 00:00:25.966 F6 28 21 12 2 4F EF 0 7 80 0 0 0 0 0 0 0 0 89 9F
2010-01-19 00:00:25.966 (F6 28 29 9 1 4F EF 7 88 33 BF )
2010-01-19 00:00:26.154 Execution succeeds.
2010-01-19 00:00:27.357 F6 28 29 12 2 4F EF 0 7 88 0 0 0 0 0 0 0 0 B1 E5
2010-01-19 00:00:27.357 (F6 28 31 9 1 4F EF 7 90 4 50 )
2010-01-19 00:00:27.545 Execution succeeds.