#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#include <Array.au3>
Global Const $_FILEPATH_LOG = @ScriptDir & '\_CheckLogFile.txt'
Global Const $_FILEPATH_INI = @ScriptDir & '\_CheckLogFile.ini'
Global $_LAST_LOG_LINE_POSITION = IniRead($_FILEPATH_INI,"Options","Last checked position", 0)
;for testing purposes
FileWrite($_FILEPATH_LOG, 'Markus and Ben are late again'&@CRLF&'Today is a fine day'&@CRLF&'And where are Markus, Ben and Tammy'&@CRLF)
_CheckLogFile("Markus|Ben|Tammy")
Func _CheckLogFile($sSearchTerms)
Local $sFileRead
Local $iPosition
Local $aResults[0]
Local $iTotalFound = 0
Local $aSearchTerms = StringSplit($sSearchTerms, '|', 2)
If Not FileExists($_FILEPATH_LOG) Then
ConsoleWrite('!>!'&@ScriptLineNumber&': File not found ['&$_FILEPATH_LOG&']'&@CRLF) ;Red/Brown
Else
Local $iLineCount = _FileCountLines($_FILEPATH_LOG)
If Not $iLineCount Or $iLineCount = $_LAST_LOG_LINE_POSITION Then
Local $sStr = @ScriptLineNumber&': '
ConsoleWrite($iLineCount > 0 ? '>-> '& $sStr &'NO NEW LOG UPDATED - ['&$_FILEPATH_LOG&']' &@CRLF : '' & _ ;Yellow
'!>! '& $sStr &'Log File is Emptied - ['&$_FILEPATH_LOG&']'&@CRLF) ;Red
Else
; making sure log file new or got reset
If $iLineCount < $_LAST_LOG_LINE_POSITION Then
$_LAST_LOG_LINE_POSITION = 0
IniWrite($_FILEPATH_INI, "Options", "Last checked position", 0)
EndIf
Local $hFileOpen = FileOpen($_FILEPATH_LOG, $FO_READ)
If $hFileOpen = -1 Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
Return False
EndIf
; only check from: previous line position + 1 to: end of file
Local $iReposition = $_LAST_LOG_LINE_POSITION + 1 ;<--- prevent from re-checking the last known line position
For $i = $iReposition To $iLineCount
;~ For $i = $iLineCount To 1 Step -1
$sFileRead = FileReadLine($hFileOpen, $i)
;~ $iPosition = StringInStr($sFileRead, $sSearchTerms)
Local $iPosition = False
Local $jPosition = 0
Local $sPosition = ''
Local $sSearchTerms_result = ''
;~ _ArrayDisplay($aSearchTerms, UBound($aSearchTerms))
For $j = 0 To UBound($aSearchTerms)-1
$jPosition = StringInStr($sFileRead, $aSearchTerms[$j])
If $jPosition Then
$iPosition = True
$sPosition = $sPosition &','& $jPosition
$sSearchTerms_result = $sSearchTerms_result&','&$aSearchTerms[$j]
ConsoleWrite('>->'&@ScriptLineNumber&': Line# '&$i&' | Item# '&$j&' - '&$jPosition&'='&$aSearchTerms[$j]&@CRLF) ;Blue/Cyan
EndIf
Next
If $iPosition Then
$sPosition = StringTrimLeft($sPosition, 1)
$sSearchTerms_result = StringTrimLeft($sSearchTerms_result, 1)
EndIf
;debug
Local $sStr = @ScriptLineNumber&': line-'& $i &' | position-('& $sPosition &') | '
ConsoleWrite($iPosition > 0 ? ' '& $sStr &'found-('& $sSearchTerms_result &')'&@CRLF : '!>! '& $sStr &'NOT FOUND for neither [' &$sSearchTerms&']'&@CRLF) ;White/Red
;--opt 1---
;keep track all
;~ $iTotalFound += 1
;~ _ArrayAdd($aResults, '#'& $iTotalFound &' - line: ' & $i &'/'& $iLineCount & _
;~ ' pos: ' & $sPosition & _
;~ ' str: ' & $sSearchTerms)
;--opt 2---
;keep track only found
If Not $iPosition Then ContinueLoop
$iTotalFound += 1
_ArrayAdd($aResults, 'result #'& $iTotalFound &')|'&@CRLF& _
' log_line: (' & $i &'/'& $iLineCount &')|'& _
' at_pos: (' & $sPosition &')|'& _
' matched: (' & $sSearchTerms_result &')')
Next
; remember and prevent from reading old log lines in case of system crash/shutdown/or script terminated
If $iLineCount > $_LAST_LOG_LINE_POSITION Then
$_LAST_LOG_LINE_POSITION = $iLineCount
IniWrite($_FILEPATH_INI, "Options", "Last checked position", $_LAST_LOG_LINE_POSITION)
EndIf
FileClose($hFileOpen)
_ArrayInsert($aResults, 0, $iTotalFound)
If UBound($aResults)-1 Then
;MsgBox(0,"Found", $iTotalFound &" ["& $sSearchTerms &"] found", 5)
_ArrayDisplay($aResults, @ScriptLineNumber&' | Found '&$iTotalFound)
Else
MsgBox($MB_ICONWARNING,"Not Found", $iTotalFound &" ["& $sSearchTerms &"] found", 5)
EndIf
EndIf
EndIf
EndFunc