-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
590 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,40 @@ | ||
import threading | ||
import time | ||
import os | ||
import datetime | ||
from glob import glob | ||
|
||
from PyQt5.QtCore import QFileSystemWatcher, pyqtSignal | ||
|
||
class ThreadedLogReader(threading.Thread): | ||
from helpers import strip_timestamp | ||
|
||
def __init__(self, eq_directory, interval = 250): | ||
self._interval = interval | ||
self._eq_directory = eq_directory | ||
|
||
self._log_new_lines = [] | ||
self._log_file = "" | ||
self._update_log_file() | ||
|
||
self._active = True | ||
|
||
threading.Thread.__init__(self) | ||
|
||
|
||
def get_new_lines(self): | ||
new_lines = [line.strip() for line in self._log_new_lines[:]] | ||
for num in range(0, len(new_lines)): | ||
self._log_new_lines.pop(0) | ||
return new_lines | ||
|
||
def _update_log_file(self): | ||
log_filter = os.path.join( | ||
self._eq_directory, | ||
"eqlog*.*" | ||
) | ||
files = glob(log_filter) | ||
tlog_file = max(files, key=os.path.getmtime) | ||
if tlog_file != self._log_file: | ||
with open(tlog_file, 'rb') as tlog: | ||
tlog.seek(0, os.SEEK_END) | ||
self._last_read = tlog.tell() | ||
self._file_size = os.stat(tlog_file).st_size | ||
self._log_file = tlog_file | ||
|
||
def run(self): | ||
while self._active: | ||
self._update_log_file() | ||
self._check_log() | ||
time.sleep(self._interval/1000.0) | ||
|
||
def stop(self): | ||
self._active = False | ||
|
||
def _check_log(self): | ||
try: | ||
new_size = os.stat(self._log_file).st_size | ||
if type(self._file_size) == int and self._file_size < new_size: | ||
self._file_size = new_size | ||
with open(self._log_file, 'r') as file: | ||
file.seek(self._last_read, os.SEEK_SET) | ||
self._log_new_lines.extend(file.readlines()) | ||
self._last_read = file.tell() | ||
return True | ||
return False | ||
except Exception as e: | ||
print("FileReader().check_log():", e, type(e)) | ||
class LogReader(QFileSystemWatcher): | ||
|
||
new_line = pyqtSignal(object) | ||
|
||
def __init__(self, eq_directory): | ||
super().__init__() | ||
|
||
self._eq_directory = eq_directory | ||
self._files = glob(os.path.join(self._eq_directory, 'eqlog*.txt')) | ||
self._watcher = QFileSystemWatcher([self._eq_directory] + self._files) | ||
self._watcher.fileChanged.connect(self._file_changed) | ||
|
||
self._stats = { | ||
'log_file':'', | ||
'last_read':0, | ||
} | ||
|
||
def _file_changed(self, changed_file): | ||
if changed_file != self._stats['log_file']: | ||
self._stats['log_file'] = changed_file | ||
with open(self._stats['log_file']) as log: | ||
log.seek(0, os.SEEK_END) | ||
self._stats['last_read'] = log.tell() | ||
with open(self._stats['log_file']) as log: | ||
log.seek(self._stats['last_read'], os.SEEK_SET) | ||
lines = log.readlines() | ||
self._stats['last_read'] = log.tell() | ||
for line in lines: | ||
self.new_line.emit((datetime.datetime.now(), strip_timestamp(line)) | ||
) | ||
|
Oops, something went wrong.