evilC's LogRead Class Getting Rows Read Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

evilC's LogRead Class Getting Rows Read

Post by hasantr » 03 Jul 2022, 02:17

Why is this MSGbox blank?
I want to be able to get the rows read with a global variable.

Any ide?

Code: Select all

#SingleInstance force
#Persistent
OutputDebug DBGVIEWCLEAR
global LogT
lt := new LogTailer("C:\Test.txt", Func("OnNewLine"))
MsgBox % Logt
return

; This function gets called each time there is a new line
OnNewLine(line){
	Global LogT
	LogT .= line
	ToolTip % "New Line: " line
}

; LOG TAILER CLASS BY EVILC
; Pass it the filename, and a function object that gets fired when a new line is added
class LogTailer {
	seekPos := 0
	fileHandle := 0
	fileSize := 0
	
	__New(FileName, Callback){
		this.fileName := FileName
		this.callback := callback
		
		fileHandle := FileOpen(FileName, "r `n")
		if (!IsObject(fileHandle)){
			MsgBox % "Unable to load file " FileName
			ExitApp
		}
		this.fileHandle := fileHandle
		this.fileSize := fileHandle.Length
		fn := this.Read.Bind(this)
		this.ReadFn := fn
		this.Start()
	}
	
	Read(){
		if (this.fileHandle.Length < this.fileSize){
            ; File got smaller. Log rolled over. Reset to start
			this.seekPos := 0
		}
        ; Move to where we left off
		this.fileHandle.Seek(this.seekPos, 0)
		
        ; Read all new lines
		while (!this.fileHandle.AtEOF){
			line := this.fileHandle.ReadLine()
			if (line == "`r`n" || line == "`n"){
				continue
			}
            ; Fire the callback function and pass it the new line
			this.callback.call(line)
		}
        ; Store position we last processed
		this.seekPos := this.fileHandle.Pos
        ; Store length so we can detect roll over
		this.fileSize := this.fileHandle.Length
	}
	
    ; Starts tailing
	Start(){
		fn := this.ReadFn
		SetTimer, % fn, 10
	}
	
    ; Stops tailing
	Stop(){
		fn := this.ReadFn
		SetTimer, % fn, Off
	}
	
    ; Stop tailing and close file handle
	Delete(){
		this.Stop()
		this.fileHandle.Close()
	}
}

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: EvilC LogRead Class Getting Rows Read

Post by hasantr » 03 Jul 2022, 07:33

Can anyone explain what's going on here? I tried so hard but I really can't understand why.
The line read from the log file can never be used outside the function. I even tried using a pointer. I failed.
I wanted to assign it to a statement in the class and then retrieve it from there, again unsuccessful. What am I missing? Am I running into an incompatibility with AHK-H?

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: evilC's LogRead Class Getting Rows Read  Topic is solved

Post by hasantr » 03 Jul 2022, 07:50

I finally figured out that it works asynchronously. I accidentally solved the problem when I put a 100ms wait every now and then. This was a problem because msgbox kicked in before the process in the class was finished.

I was wrong as I didn't expect autohotkey to execute an asynchronous operation.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: evilC's LogRead Class Getting Rows Read

Post by boiler » 03 Jul 2022, 13:10

The key to spotting that is the presence of SetTimer.

Post Reply

Return to “Ask for Help (v1)”