hannes08 39 Posted November 11, 2010 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. expandcollapse popup#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 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Share this post Link to post Share on other sites
hannes08 39 Posted November 24, 2010 Has no one tried it yet? It would be nice to get a feedback. Thanks, Hannes Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Share this post Link to post Share on other sites
jaberwacky 327 Posted November 24, 2010 (edited) Having studied under the great enlightened master Gung Ho, I tend to not use log files. Edited November 24, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Share this post Link to post Share on other sites
hench 2 Posted November 24, 2010 (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 November 24, 2010 by hench Share this post Link to post Share on other sites