2013-08-21 60 views
0

我必須對程序進行日誌記錄,並且發現了類似的代碼,但仍然不知道如何對其進行編碼。 日誌文件的代碼:程序的記錄(記錄)事件

Option Explicit 

Public Enum LogTypeEnum 
ltInfo = 1 
ltWarning = 2 
ltError = 3 
End Enum 

Public Enum LogProgramDomainEnum 
lpdRole = 1 
lpdCinnosti = 2 
End Enum 

Private mWinAdLogCol As New EMWinAdLog.WinAdLogCol 
Private mFrmLog As New EMWinAdLog.WinadLogFrm 

Public Sub WinADLogInit(cfg As EMACTIVEXLib.EMCONFIG, ByVal ProgramID As Integer) 
mWinAdLogCol.Init cfg, ProgramID 

mFrmLog.AddLogType LogTypeEnum.ltInfo, "Info" 
mFrmLog.AddLogType LogTypeEnum.ltWarning, "Warning" 
mFrmLog.AddLogType LogTypeEnum.ltError, "Error" 

mFrmLog.AddProgramDomain LogProgramDomainEnum.lpdRole, "Role" 
mFrmLog.AddProgramDomain LogProgramDomainEnum.lpdCinnosti, "Activity" 

mFrmLog.Init cfg, ProgramID 
End Sub 

Public Sub WriteLog(LogProgramDomain As LogProgramDomainEnum, LogType As 
LogTypeEnum,Description1 As String, Optional Description2 As String = "") 
mWinAdLogCol.xAdd LogProgramDomain, LogType, Description1, Description2 
End Sub 

Public Sub ShowLog() 
mFrmLog.Show 0 
End Sub 

Public Sub Done() 
mFrmLog.Done 
Set mFrmLog = Nothing 
Set mWinAdLogCol = Nothing 
End Sub 

和〔實施例我有一個事件:

 Private Sub cmAdd_Click() 
     Load frmAddrolu 
     frmAddrolu.Show vbModal, Me 
     If frmAddrolu.Nazov <> "" Then 
     Dim LokRola As TRola 
     Set LokRola = Role.xAdd(frmAddrolu.Nazov) 
     ZobrazRoleToLst cmbRole, Role 
     SetCmbItem cmbRole, LokRola.RolaID 
     If cmbRole.ListIndex >= 0 Then 
     ZobrazSkupiny AllSkupiny, RolaProgramPristup, treeSkup, True 
     treeSkup_NodeClick treeSkup.SelectedItem 
     End If 
     End If 
     End Sub 

我只是寫例子,因爲我不知道該怎麼做。 感謝您的示例或解釋或您的任何幫助。

+0

沒有人幫助的記錄? – John

+1

編寫一個函數'Log(message as String)',它打開一個文件進行追加,寫入日誌消息,然後關閉文件。然後在你的代碼中使用這個函數來將日誌條目添加到你的日誌文件中。如果您需要,可以使用更多功能對其進行增強。 – MicSim

回答

1

MicSim是正確的,這並不難。我的基本日誌記錄Sub下面是隨意採取它並使用它或改變它以適應您的特定需求。我在應用程序的.bas文件中使用它。這裏還有一些代碼可以讓你限制文件大小。

Public Sub WriteDebugFile(ByVal DebugString As String, Optional ByRef ShowDateTime As Boolean = True, Optional sAltFileName As Variant, _ 
Optional ByVal lMaxFileSize As Long) 

Dim hFile As Integer 
Dim hTmpFile As Integer 
Dim sFileName As String 
Dim sTmpFile As String * 255 
Dim lfilesize As Long 
Dim sFBuffer As String * 100 
Dim lRtn As Long 

On Error GoTo 0 'turn off error checking 

If IsMissing(sAltFileName) Then 
    sFileName = AppPath() & App.ProductName & "dbg.log" 
Else 
    If InStr(sAltFileName, "\") > 0 Then 'the name appears to have a path 
     sFileName = sAltFileName 
    Else 
     sFileName = AppPath() & sAltFileName 
    End If 
End If 

'check and adjust the file size? lMaxFileSize must be greater than the 1k to reduce file by 
If lMaxFileSize >= Len(sFBuffer) And FileExists(sFileName) = True Then 
    If FileLen(sFileName) > lMaxFileSize Then 
     sFBuffer = Space$(Len(sFBuffer)) 'initialize the buffer 
     lRtn = GetTempFilename(AppPath(), "dbg", 0, sTmpFile) 
     'remove 1k from the top of the file 
     hFile = FreeFile 
     Open Trim$(sFileName) For Binary As hFile Len = Len(sFBuffer) 'the original file 
     DoEvents 
     hTmpFile = FreeFile 
     Open sTmpFile For Binary As hTmpFile Len = Len(sFBuffer) 'the new file 
     Get #hFile, , sFBuffer 
     Do Until EOF(hFile) = True 
      Get #hFile, , sFBuffer 'forget the first len(buffer) 
      If InStr(1, sFBuffer, Chr$(0), vbBinaryCompare) Then 
       Put #hTmpFile, , Left$(sFBuffer, InStr(1, sFBuffer, Chr$(0), vbBinaryCompare) - 1) 
      Else 
       Put #hTmpFile, , sFBuffer 
      End If 
     Loop 
     Close #hFile 
     Close #hTmpFile 
     Kill sFileName 
     Name sTmpFile As sFileName 
    End If 
End If 

'free to continue 
hFile = FreeFile 
Open sFileName For Append As hFile 

If ShowDateTime Then 
    DebugString = "[" & Format$(Date$, "M-D-YYYY") & " " & Format$(Time$, "Hh:Nn:ss") & "]" _ 
    & Chr$(9) & DebugString 
End If 

Print #hFile, DebugString 
Close #hFile 

End Sub 

ShowDateTime,sAltFileName和lMaxFileSize參數是可選的。要使用這種方法,你可以從任何你想寫消息到日誌的地方調用它。 WriteDebugFile "The code just did something."

或者如果你喜歡使用Call聲明,Call WriteDebugFile("The code just did something.")