From 1321313fabc8034cbbb5da1b60f0036ca5a69e4b Mon Sep 17 00:00:00 2001 From: Knut-Frode Dagestad Date: Thu, 20 Jun 2024 19:02:59 +0200 Subject: [PATCH] OpenDriftGUI now logs to file in addition to GUI window --- opendrift/scripts/opendrift_gui.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/opendrift/scripts/opendrift_gui.py b/opendrift/scripts/opendrift_gui.py index 638cc80fd..fe381893f 100755 --- a/opendrift/scripts/opendrift_gui.py +++ b/opendrift/scripts/opendrift_gui.py @@ -722,11 +722,6 @@ def check_seeding(self): def run_opendrift(self): sys.stdout.write('running OpenDrift') - # Creating fresh instance of the current model, but keeping config - adjusted_config = self.o._config - self.set_model(self.modelname, rebuild_gui=False) - self.o._config = adjusted_config - try: self.budgetbutton.destroy() except Exception as e: @@ -752,6 +747,23 @@ def run_opendrift(self): start_time = local_start.astimezone(pytz.utc).replace(tzinfo=None) end_time = local_end.astimezone(pytz.utc).replace(tzinfo=None) + # Creating fresh instance of the current model, but keeping config + adjusted_config = self.o._config + self.set_model(self.modelname, rebuild_gui=False) + self.o._config = adjusted_config + + # Add secondary log-handler to file + if self.has_diana is True and True: + logfile = self.outputdir + '/opendrift_' + self.modelname + start_time.strftime('_%Y%m%d_%H%M.log') + import logging + if hasattr(self, 'handler'): + logging.getLogger('opendrift').removeHandler(self.handler) + self.handler = logging.FileHandler(logfile, mode='w') + self.handler.setFormatter(logging.getLogger('opendrift').handlers[0].formatter) + logging.getLogger('opendrift').addHandler(self.handler) + if len(logging.getLogger('opendrift').handlers) > 2: + raise ValueError('Too many log handlers') + sys.stdout.flush() lon = float(self.lon.get()) lat = float(self.lat.get())