@@ -183,32 +183,30 @@ def limit_time(src_dir, dest_dir, duration, file_name_reg_ex,
183183 + f"unexpected characters: { invalid_chars } " )
184184
185185 include_lines_without_timestamp = False
186- supported_file_types = ['.log' , '.gz ' ]
186+ supported_file_types = ['.log' , '.txt ' ]
187187 start_time , end_time = FilterLog ._parse_duration (duration )
188188 Log .info (f'start_time = { start_time } , end_time = { end_time } ' )
189189 # sort files based on timestamp
190- list_of_files = filter (lambda f : os .path .isfile (os . path . join ( src_dir , f )),
191- os .listdir (src_dir ))
190+ list_of_files = filter (lambda f : os .path .isfile (
191+ os .path . join ( src_dir , f )), os . listdir (src_dir ))
192192 # sort the files based on last modification time in descending order
193193 list_of_files = sorted (list_of_files ,
194194 key = lambda f : os .path .getmtime (os .path .join (src_dir , f )),
195195 reverse = True )
196196 for file in list_of_files :
197197 log_scope_exceeded = False
198- is_log_written_to_file = False
199- file_extension = pathlib .Path (file ).suffix
200- # Ignore processing of other file format.
201- if file_extension not in supported_file_types :
202- Log .warn (f'{ file } file is skipped..' )
203- continue
198+ log_written_to_file = False
204199 op_file = os .path .join (dest_dir , 'tmp_' + file )
205200 if file .startswith (file_name_reg_ex ):
206201 in_file = os .path .join (src_dir , file )
207- if file . endswith ( '.gz' ):
208- # File modification time is in
202+ file_extension = pathlib . Path ( file ). suffix
203+ if file_extension not in supported_file_types :
209204 FilterLog ._collect_rotated_log_file (
210205 in_file , dest_dir , start_time , end_time )
211206 continue
207+ # TODO: Instead of processing the file line by line,
208+ # apply binary search on file so that we can get log lines
209+ # in Log(N) complexity.
212210 with open (in_file , 'r' ) as fd_in , open (op_file , 'a' ) as fd_out :
213211 line = fd_in .readline ()
214212 while (line ):
@@ -220,7 +218,7 @@ def limit_time(src_dir, dest_dir, duration, file_name_reg_ex,
220218 log_time = datetime .strptime (log_duration , datetime_format )
221219 if start_time <= log_time and log_time <= end_time :
222220 include_lines_without_timestamp = True
223- is_log_written_to_file = True
221+ log_written_to_file = True
224222 fd_out .write (line )
225223 elif log_time > end_time and include_lines_without_timestamp :
226224 include_lines_without_timestamp = False
@@ -233,11 +231,11 @@ def limit_time(src_dir, dest_dir, duration, file_name_reg_ex,
233231 # flag include_lines_without_timestamp = True
234232 except ValueError :
235233 if include_lines_without_timestamp :
236- is_log_written_to_file = True
234+ log_written_to_file = True
237235 fd_out .write (line )
238236 line = fd_in .readline ()
239237 try :
240- if is_log_written_to_file :
238+ if log_written_to_file :
241239 final_op_file = os .path .join (dest_dir , file )
242240 os .rename (op_file , final_op_file )
243241 else :
0 commit comments