@@ -84,11 +84,6 @@ async def check_log(self):
8484 self .config .get ('log' , os .path .join (self .server .instance .home , 'Logs' , 'dcs.log' ))
8585 )
8686
87- combined_pattern = re .compile (
88- '|' .join (f'(?P<pattern{ i } >{ pat } )' for i , pat in enumerate (self .pattern .keys ())))
89- callback_map = {f'pattern{ i } ' : cb for i , (pat , cb ) in enumerate (self .pattern .items ())}
90-
91- self .log_pos = 0
9287 while not self .stop_event .is_set ():
9388 try :
9489 if not os .path .exists (logfile ):
@@ -105,22 +100,19 @@ async def check_log(self):
105100 self .log_pos = 0
106101
107102 self .log_pos = await file .seek (self .log_pos , 0 )
108- await file .seek ( self . log_pos , 0 )
109- async for line in file :
103+ lines = await file .readlines ( )
104+ for idx , line in enumerate ( lines ) :
110105 if '=== Log closed.' in line :
111106 self .log_pos = - 1
112107 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 )
122- self .log_pos = await file .tell ()
123- except FileNotFoundError as ex :
108+ for pattern , callback in self .pattern .items ():
109+ match = pattern .search (line )
110+ if match :
111+ if asyncio .iscoroutinefunction (callback ):
112+ asyncio .create_task (callback (self .log_pos + idx , line , match ))
113+ else :
114+ self .loop .run_in_executor (None , callback , self .log_pos + idx , line , match )
115+ except FileNotFoundError :
124116 pass
125117 finally :
126118 await asyncio .sleep (1 )
0 commit comments