From 564c92343b2297c3cdc0346521273bf3ea95c505 Mon Sep 17 00:00:00 2001 From: PabloLec Date: Sat, 2 Oct 2021 11:16:39 +0200 Subject: [PATCH] Handle inotify related exception --- livelog/__main__.py | 4 ++-- livelog/reader.py | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/livelog/__main__.py b/livelog/__main__.py index 6684ffe..0f2792a 100644 --- a/livelog/__main__.py +++ b/livelog/__main__.py @@ -43,8 +43,8 @@ def _parse_args(): if args.file is not None: file = Path(args.file) else: - file = Path( - "/tmp/livelog.log" + file = ( + Path("/tmp/livelog.log") if system() == "Darwin" else Path(gettempdir()) / "livelog.log" ) diff --git a/livelog/reader.py b/livelog/reader.py index 5eddc5f..1b38e34 100644 --- a/livelog/reader.py +++ b/livelog/reader.py @@ -94,7 +94,7 @@ def file_exists(self): bool: File exists """ - return self.file.isfile() + return self.file.is_file() def print_output(self): """Drive the printing process by getting new lines, filtering log @@ -177,6 +177,13 @@ def on_modified(self, *args, **kwargs): self.print_output() + def loop_without_event(self): + """If inotify instance limit reached, loop without watching file.""" + + while True: + self.print_output() + sleep(1) + def start_reader(file: str, level: str, nocolors: bool): """Start reader process. @@ -190,8 +197,11 @@ def start_reader(file: str, level: str, nocolors: bool): event_handler = Reader(file=file, level=level, nocolors=nocolors) observer = Observer() observer.schedule(event_handler, file, recursive=True) - observer.start() - - input("") - observer.stop() - observer.join() + try: + observer.start() + input("") + observer.stop() + observer.join() + except OSError: + # Handle "OSError: [Errno 24] inotify instance limit reached" exception + event_handler.loop_without_event()