Sign in to follow this  
Followers 0
hannes08

Logfile Retention

4 posts in this topic

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]

Share this post


Link to post
Share on other sites



Has no one tried it yet?

It would be nice to get a feedback.

:graduated:

Thanks,

Hannes


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I was curious about this thread and then You made me discover the _FileWriteLog function

Will try your addon as soon as I can !!

Thanks for sharing it !!

-hench

Edited by hench

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0