diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index f7cfeaa..c8e07ee 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -567,6 +567,7 @@ def export_config(interface): def common(): """Shared code for all of our command line wrappers""" + logfile = None our_globals = Globals.getInstance() args = our_globals.get_args() parser = our_globals.get_parser() @@ -621,8 +622,8 @@ def common(): logging.info(f"Logging serial output to {args.seriallog}") # Note: using "line buffering" # pylint: disable=R1732 - logfile = open(args.seriallog, 'w+', - buffering=1, encoding='utf8') + logfile = open(args.seriallog, 'w+', buffering=1, encoding='utf8') + our_globals.set_logfile(logfile) subscribe() if args.ble: @@ -636,6 +637,8 @@ def common(): # We assume client is fully connected now onConnected(client) + #if logfile: + #logfile.close() if args.noproto or (have_tunnel and args.tunnel): # loop until someone presses ctrlc while True: @@ -828,6 +831,10 @@ def main(): our_globals.set_parser(parser) initParser() common() + logfile = our_globals.get_logfile() + if logfile: + logfile.close() + def tunnelMain(): diff --git a/meshtastic/globals.py b/meshtastic/globals.py index 05049c1..bee1c2d 100644 --- a/meshtastic/globals.py +++ b/meshtastic/globals.py @@ -29,6 +29,7 @@ class Globals: self.parser = None self.target_node = None self.channel_index = None + self.logfile = None def reset(self): """Reset all of our globals. If you add a member, add it to this method, too.""" @@ -37,6 +38,7 @@ class Globals: self.target_node = None self.channel_index = None + # setters def set_args(self, args): """Set the args""" self.args = args @@ -53,6 +55,11 @@ class Globals: """Set the channel_index""" self.channel_index = channel_index + def set_logfile(self, logfile): + """Set the logfile""" + self.logfile = logfile + + # getters def get_args(self): """Get args""" return self.args @@ -68,3 +75,7 @@ class Globals: def get_channel_index(self): """Get channel_index""" return self.channel_index + + def get_logfile(self): + """Get logfile""" + return self.logfile