Jump to content

Logfile Retention


hannes08
 Share

Recommended Posts

Hi Guys,

wirting logfiles with _FileWriteLog is a ver, very usefull thing. But have you ever had the problem that your Logfiles grow bigger and bigger?

I worte this little piece of code that does a "Logfile Retention" means old entries are truncated.

#include-once

#include <Date.au3>
#include <File.au3>

; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteLog2
; Description ...: Writes current date,time and the specified text to a log file.
; Syntax.........: _FileWriteLog2($sLogPath, $sLogMsg[, $iDays = -1])
; Parameters ....: $sLogPath  - Path and filename of the file to be written to
;                  $sLogMsg   - Message to be written to the log file
;                  $iDays     - [Optional] - Flag that defines if How long old entries in the Log will be kept.
;                  |If $iDays = -1 (default) all entries will be kept.
;                  |If $iDays <> -1 entries will be truncated if older than $iDays.
; Return values .: Success - Returns a 1
;                  Failure - Returns a 0
;                  @Error  - 0 = No error.
;                  |1 = Error opening specified file
;                  |2 = File could not be written to
; Author ........: Jeremy Landes <info at hannes08 dot de>
; Modified.......: ReinaueH - deleted $iFlag parameter, added $iDays parameter
; Remarks .......:
; Related .......: .FileOpen
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================

Func _FileWriteLog2($sLogPath, $sLogMsg, $iDays = -1)


    Local  $iOpenMode = $FO_APPEND

    Local $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    Local $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    Local $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg

    Local $hOpenFile = FileOpen($sLogPath, $iOpenMode)
    If $hOpenFile = -1 Then Return SetError(1, 0, 0)

    Local $iWriteFile = FileWriteLine($hOpenFile, $sMsg)
    Local $iRet = FileClose($hOpenFile)
    If $iWriteFile = -1 Then Return SetError(2, $iRet, 0)

    If $iDays > 0 Then
        Local $sDateBack = StringReplace(_DateAdd("D",(-1*$iDays),StringReplace($sDateNow,"-","/")),"/","-")
        Local $iDateback = StringReplace($sDateBack,"-","") & StringReplace($sTimeNow,":","")
        Local $aLogfileContents[1]

        If Not _FileReadToArray($sLogPath,$aLogfileContents) = 1 Then
            Return SetError(3,0,-1)
        EndIf

        $iOpenMode = $FO_OVERWRITE

        Local $hOpenFile = FileOpen($sLogPath, $iOpenMode)
        If $hOpenFile = -1 Then Return SetError(1, 0, 0)

        For $i = 1 To $aLogfileContents[0]
            $iDateNow = StringReplace(StringReplace(StringReplace(StringMid($aLogfileContents[$i],1,19),":","")," ",""),"-","")
            If $iDateNow > $iDateback Then
                FileWrite($hOpenFile,StringStripWS($aLogfileContents[$i],3) & @CRLF)
            EndIf
        Next

    EndIf


    Return $iRet
EndFunc   ;==>_FileWriteLog2

It is not meant to replace every occurance of _FileWriteLog, but a one time call at startup or before exiting your application makes sense.

Any comments are appreciated.

Regards,

Hannes

:graduated:

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...