home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / free_security / msshared / Shared_Computer_Toolkit_ENU.msi / FileInclude005 < prev    next >
Encoding:
Text (UTF-16)  |  2005-09-02  |  14.6 KB  |  200 lines

  1. ' ***
  2. ' *** ------------------------------------------------------------------------------
  3. ' *** Filename:        clsLogging.vbs
  4. ' *** ------------------------------------------------------------------------------
  5. ' *** Description:    Logging Class
  6. ' *** ------------------------------------------------------------------------------
  7. ' *** Version:        1.0
  8. ' *** Notes:        Creates appliation log files
  9. ' *** ------------------------------------------------------------------------------
  10. ' *** Copyright (C) Microsoft Corporation 2005, All Rights Reserved
  11. ' *** ------------------------------------------------------------------------------
  12. ' ***
  13.  
  14. ' ~~~ 
  15. ' ~~~ Force variables to be declared and turn off script error messages unless in DEBUG mode
  16. ' ~~~ 
  17. Option Explicit
  18.  
  19. Class Logging
  20.     ' ~~~ 
  21.     ' ~~~ declare variables and constants
  22.     ' ~~~ 
  23.     Dim oLogFile, sLogFileName, sLogFileNameBAK, bAppend, bBackup, bTimeStamp
  24.  
  25.     ' ~~~ 
  26.     ' ~~~ public methods & properties
  27.     ' ~~~ 
  28.  
  29.     ' ***
  30.     ' *** ------------------------------------------------------------------------------
  31.     ' *** Property:    Append
  32.     ' *** ------------------------------------------------------------------------------
  33.     ' *** Purpose:    Determines if the log file is appended or overwritten
  34.     ' ***            Default value = False, overwrite
  35.     ' *** ------------------------------------------------------------------------------
  36.     ' ***
  37.     Public Property Get Append
  38.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  39.         Append = bAppend
  40.     End Property    
  41.  
  42.     Public Property Let Append(bValue)
  43.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  44.         If VarType(bValue) = vbBoolean Then
  45.             bAppend = bValue
  46.         End If
  47.     End Property
  48.  
  49.     ' ***
  50.     ' *** ------------------------------------------------------------------------------
  51.     ' *** Property:    Backup
  52.     ' *** ------------------------------------------------------------------------------
  53.     ' *** Purpose:    Determines if and old log file is backed up (renamed .bak)
  54.     ' ***            Default value = True
  55.     ' *** ------------------------------------------------------------------------------
  56.     ' ***
  57.     Public Property Get Backup
  58.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  59.         Backup = bBackup
  60.     End Property    
  61.  
  62.     Public Property Let Backup(bValue)
  63.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  64.         If VarType(bValue) = vbBoolean Then
  65.             bBackup = bValue
  66.         End If
  67.     End Property
  68.  
  69.     ' ***
  70.     ' *** ------------------------------------------------------------------------------
  71.     ' *** Property:    TimeStamp
  72.     ' *** ------------------------------------------------------------------------------
  73.     ' *** Purpose:    Determines if a timestamp is added to the log entries
  74.     ' ***            Default value = True
  75.     ' *** ------------------------------------------------------------------------------
  76.     ' ***
  77.     Public Property Get TimeStamp
  78.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  79.         TimeStamp = bTimeStamp
  80.     End Property    
  81.  
  82.     Public Property Let TimeStamp(bValue)
  83.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  84.         If VarType(bValue) = vbBoolean Then
  85.             bTimeStamp = TimeStamp
  86.         End If
  87.     End Property
  88.  
  89.     ' ***
  90.     ' *** ------------------------------------------------------------------------------
  91.     ' *** Name:        Open
  92.     ' *** ------------------------------------------------------------------------------
  93.     ' *** Purpose:    Opens a new log file, backing up the old one if appropriate
  94.     ' *** ------------------------------------------------------------------------------
  95.     ' ***
  96.     Public Sub Open(sValue)
  97.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  98.         ' ~~~ store the logfile name
  99.         sLogFileName = sValue
  100.         sLogFileNameBAK = Left(sLogFileName, Len(sLogFileName)-4)
  101.  
  102.         ' ~~~ if old log file exists and we want to back it up
  103.         If oFso.FileExists(sLogFileName) and bBackup Then
  104.             ' ~~~ if backup already exists delete it
  105.             If oFso.FileExists(sLogFileNameBAK & ".bak.log") Then
  106.                 Call oFso.DeleteFile(sLogFileNameBAK & ".bak.log", True)
  107.             End If
  108.             
  109.             ' ~~~ backup current log file
  110.             Call oFso.MoveFile(sLogFileName, sLogFileNameBAK & ".bak.log")
  111.         End If
  112.  
  113.         ' ~~~ if old log file exists and we want to append to it
  114.         If oFso.FileExists(sLogFileName) and Not(bAppend) Then
  115.             Call oFso.DeleteFile(sLogFileName, True)
  116.         End If
  117.         
  118.         ' ~~~ Open or Create log file
  119.         ' ~~~ sLogFileName : Log file name
  120.         ' ~~~ 8 : is for appending
  121.         ' ~~~ True : Creates the file if not exists
  122.         ' ~~~ -1 : for Unicode support
  123.         Set oLogFile = oFso.OpenTextFile(sLogFileName, 8, True , -1)
  124.     End Sub
  125.  
  126.     ' ***
  127.     ' *** ------------------------------------------------------------------------------
  128.     ' *** Name:        Close
  129.     ' *** ------------------------------------------------------------------------------
  130.     ' *** Purpose:    Closes a new log file
  131.     ' *** ------------------------------------------------------------------------------
  132.     ' ***
  133.     Public Sub Close
  134.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  135.         oLogFile.Close
  136.     End Sub
  137.  
  138.     ' ***
  139.     ' *** ------------------------------------------------------------------------------
  140.     ' *** Name:        Write
  141.     ' *** ------------------------------------------------------------------------------
  142.     ' *** Purpose:    Writes a line to a log file
  143.     ' *** ------------------------------------------------------------------------------
  144.     ' ***
  145.     Public Sub Write(sLogFileEntry)
  146.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  147.         If bTimeStamp Then
  148.             oLogFile.Write(Now()) & " : "
  149.         End If
  150.         oLogFile.WriteLine(sLogFileEntry)
  151.     End Sub
  152.  
  153.     ' ***
  154.     ' *** ------------------------------------------------------------------------------
  155.     ' *** Name:        View
  156.     ' *** ------------------------------------------------------------------------------
  157.     ' *** Purpose:    Displays the log file in notepad
  158.     ' *** ------------------------------------------------------------------------------
  159.     ' ***
  160.     Public Sub View
  161.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  162.         Call oShell.Run("notepad.exe " & sLogFileName, 1, False)
  163.     End Sub
  164.  
  165.  
  166.     ' ~~~ 
  167.     ' ~~~ private methods, these routines must not be called directly
  168.     ' ~~~ 
  169.     
  170.     ' ***
  171.     ' *** ------------------------------------------------------------------------------
  172.     ' *** Name:        Class_Initialize
  173.     ' *** ------------------------------------------------------------------------------
  174.     ' *** Purpose:    Used internally by the class when it is created.
  175.     ' ***            Declared as private because it must not be called directly.
  176.     ' *** ------------------------------------------------------------------------------
  177.     ' ***
  178.     Private Sub Class_Initialize
  179.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  180.         ' ~~~ set default values of properties        
  181.         bAppend    = False
  182.         bBackup    = True
  183.         bTimeStamp = True
  184.         
  185.     End Sub
  186.  
  187.     ' ***
  188.     ' *** ------------------------------------------------------------------------------
  189.     ' *** Name:        Class_Terminate
  190.     ' *** ------------------------------------------------------------------------------
  191.     ' *** Purpose:    Used internally by the class when it is destroyed.
  192.     ' ***            Declared as private because it must not be called directly.
  193.     ' *** ------------------------------------------------------------------------------
  194.     ' ***
  195.     Private Sub Class_Terminate
  196.         If NOT DEBUG Then On Error Resume Next Else On Error Goto 0
  197.  
  198.     End Sub
  199. End Class
  200.