Skip to content

Commit 935bd1c

Browse files
committed
Merge branch 'development'
2 parents 54d68f1 + cce53eb commit 935bd1c

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

extensions/loganalyser/extension.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def do_startup(self):
5454
asyncio.create_task(self.check_log())
5555

5656
async def prepare(self) -> bool:
57+
os.remove(self.logfile)
5758
await self.do_startup()
5859
self.running = True
5960
return await super().startup()
@@ -71,6 +72,12 @@ def shutdown(self) -> bool:
7172
self.stop_event.set()
7273
return super().shutdown()
7374

75+
@property
76+
def logfile(self) -> str:
77+
return os.path.expandvars(
78+
self.config.get('log', os.path.join(self.server.instance.home, 'Logs', 'dcs.log'))
79+
)
80+
7481
async def check_log(self):
7582
try:
7683
logfile = os.path.expandvars(
@@ -83,28 +90,40 @@ async def check_log(self):
8390

8491
self.log_pos = 0
8592
while not self.stop_event.is_set():
86-
if not os.path.exists(logfile):
87-
self.log_pos = 0
88-
await asyncio.sleep(1)
89-
continue
90-
91-
async with aiofiles.open(logfile, mode='r', encoding='utf-8', errors='ignore') as file:
92-
await file.seek(self.log_pos, 0)
93-
async for line in file:
94-
if '=== Log closed.' in line:
95-
self.log_pos = -1
96-
return
97-
match = combined_pattern.search(line)
98-
if match:
99-
for key, value in match.groupdict().items():
100-
if value:
101-
callback = callback_map[key]
102-
if asyncio.iscoroutinefunction(callback):
103-
asyncio.create_task(callback(self.log_pos, line, match))
104-
else:
105-
self.loop.run_in_executor(None, callback, self.log_pos, line, match)
93+
try:
94+
if not os.path.exists(logfile):
95+
self.log_pos = 0
96+
continue
97+
98+
async with aiofiles.open(logfile, mode='r', encoding='utf-8', errors='ignore') as file:
99+
max_pos = os.fstat(file.fileno()).st_size
100+
if self.log_pos == -1 or max_pos == self.log_pos:
101+
self.log_pos = max_pos
102+
continue
103+
# if the logfile was rotated, seek to the beginning of the file
104+
elif max_pos < self.log_pos:
105+
self.log_pos = 0
106+
107+
self.log_pos = await file.seek(self.log_pos, 0)
108+
await file.seek(self.log_pos, 0)
109+
async for line in file:
110+
if '=== Log closed.' in line:
111+
self.log_pos = -1
112+
return
113+
match = combined_pattern.search(line)
114+
if match:
115+
for key, value in match.groupdict().items():
116+
if value:
117+
callback = callback_map[key]
118+
if asyncio.iscoroutinefunction(callback):
119+
asyncio.create_task(callback(self.log_pos, line, match))
120+
else:
121+
self.loop.run_in_executor(None, callback, self.log_pos, line, match)
106122
self.log_pos = await file.tell()
107-
123+
except FileNotFoundError as ex:
124+
pass
125+
finally:
126+
await asyncio.sleep(1)
108127
except Exception as ex:
109128
self.log.exception(ex)
110129
finally:

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.3.30"
1+
__version__ = "3.0.3.31"

0 commit comments

Comments
 (0)