File Name = LOG_MODULE.bas
Link for donwload: https://www.dropbox.com/s/6h0g2m9nu2huc6k/LOG_MODULE.bas?raw=1
Raw Code:
----------------------------------------------------------------------------------------------------------------------
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' @module LOG_MODULE
' @author Yan França Tosta
' @version 35
' @since 06/2015
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' @function: Log_File_Name
' @description: this function returns the standard log file path
' @returns: String with the path of the log file
' @exception: Prints message to immediate window
'
Public Function Log_File_Name() As String
Dim currentDate, fileName, fullFileName, extension As String
On Error GoTo e1000
fileName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))
currentDate = Date
currentDate = Format(currentDate, "yyyy-mm-dd")
extension = ".log"
fullFileName = fileName & "_" & _
currentDate & _
extension
Log_File_Name = Log_Folder + "\" + fullFileName
Exit Function
e1000:
Debug.Print "Log file name creation failed"
Exit Function
End Function
'
' @function: Log_Folder
' @description: this function returns the standard log folder path
' @returns: String with the name of the log folder
' @exception: NO
'
Private Function Log_Folder() As String
On Error Resume Next
Log_Folder = "C:\TEMP"
Exit Function
End Function
'
' @function: Log_Mode
' @description: this function receives the log mode name and returns the code of the mode of the log printing
' There are 3 log modes: ON, OFF and DEBUG
' @param: Optional Log as String: the name of the log mode
' @returns: Integer with the defined log mode
' @exception: NO
'
Private Function Log_Mode(Optional ByVal Log As String = "debug") As Integer
On Error GoTo e1000:
Log = UCase(Log)
If Log = "ON" Or Log = "ERROR" Or Log = "1" Then
Log_Mode = 1
Exit Function
ElseIf Log = "DEBUG" Or Log = "2" Then
Log_Mode = 2
Exit Function
Else
Log_Mode = 0
Exit Function
End If
e1000:
Exit Function
End Function
'
' @function: logInformation
' @description: receives a message and prints it to the log file
' @param: Byval LogMessage as string - the message to be printed
' @exception: Displays message box
'
Private Sub logInformation(LogMessage As String)
Dim LogFileName As String
On Error GoTo error_1000:
LogFileName = Log_File_Name
Dim FileNum As Integer
FileNum = FreeFile ' next file number
Open LogFileName For Append As #FileNum ' creates the file if it doesn't exist
Print #FileNum, LogMessage ' write information at the end of the text file
Close #FileNum ' close the file
Exit Sub
error_1000:
On Error GoTo error_2000
MkDir Log_Folder
Exit Sub
error_2000:
MsgBox "The log folder '" + Log_Folder() + "' does not exist." + vbNewLine + _
"Please create it manually. (It may be a permission issue)"
Exit Sub
End Sub
'
' @function: Log
' @description: receives messages and prints the correspondent log message to the log file
' @param: Optional Byval Title as String - the title of the log message or a log code message
' @param: Optional Byval Message as String - the log message
' @exception: NO
'
Public Function Log(Optional ByVal Title As String = vbNullString, _
Optional ByVal Message As String = vbNullString)
On Error Resume Next
Log_Print Title, Message
Exit Function
End Function
'
' @function: Log_Print
' @description: receives messages and prints the correspondent log message to the log file
' @param: Optional Byval Title as String - the title of the log message or a log code message
' @param: Optional Byval Message as String - the log message
' @exception: Prints message to immediate window
'
Public Sub Log_Print(Optional ByVal Title As String = vbNullString, _
Optional ByVal Message As String = vbNullString)
Dim Text As String
Dim logMode As Integer
On Error GoTo error_1000
If Title = vbNullString And Message = vbNullString Then
Exit Sub
End If
logMode = Log_Mode()
If logMode = 0 Then
Exit Sub
End If
If Len(Message) > 200 Then
Message = Left(Message, 250)
End If
' Normal log mode - it only prints errors
If logMode = 1 Then
If Message = "1" Or Message = "ERROR" Then
Application.StatusBar = "ERROR"
Text = Title + " - " + "ERROR " + " - " + "EXIT FAILURE"
Else
Exit Sub
End If
' debug mode
ElseIf logMode = 2 Then
If Message = "1" Or Message = "ERROR" Then
Text = Title + " - " + "ERROR " + " - " + "EXIT FAILURE"
ElseIf Message = "0" Or Message = "SUCCESS" Then
Application.StatusBar = "SUCCESS"
Text = Title + " - " + "EXIT SUCCESS"
Else
If Message = vbNullString Then
Text = Title
Else
Application.StatusBar = Message
Text = Title + " - " + Message
End If
End If
End If
logInformation Format(Now, "yyyy-mm-dd hh:mm") & " - " & Text
Exit Sub
error_1000:
Debug.Print "Unable to log"
Exit Sub
End Sub
'
' @function: Open_Log
' @description: opens the log file using Notepad.exe
' @exception: Display Message Box
'
Public Sub Open_Log()
Dim pid As Double
Dim LogFileName As String
On Error GoTo error_1000
LogFileName = Log_File_Name
pid = Shell("notepad " + LogFileName, vbMaximizedFocus)
Exit Sub
error_1000:
MsgBox "Unable to open '" + Log_File_Name + "' file."
Exit Sub
End Sub
Nenhum comentário:
Postar um comentário