From 3488d86de2e114a46e59bd4d2a2d7b95bf633963 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 9 Oct 2021 21:31:24 -0700 Subject: [PATCH] altoslib: Add callback from AltosLog on file open failure This lets the UI tell the user that logging isn't working. Signed-off-by: Keith Packard --- altoslib/AltosLog.java | 35 +++++++++++++++++++++++++++++++++-- altoslib/AltosLogTrace.java | 28 ++++++++++++++++++++++++++++ altoslib/Makefile.am | 1 + 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 altoslib/AltosLogTrace.java diff --git a/altoslib/AltosLog.java b/altoslib/AltosLog.java index 7065614b..7dc9fd4c 100644 --- a/altoslib/AltosLog.java +++ b/altoslib/AltosLog.java @@ -37,6 +37,12 @@ public class AltosLog implements Runnable { Thread log_thread; AltosFile file; AltosLink link; + AltosLogTrace trace; + + private void trace(String format, Object ... arguments) { + if (trace != null) + trace.trace(format, arguments); + } private void close_log_file() { if (log_file != null) { @@ -62,10 +68,25 @@ public class AltosLog implements Runnable { } boolean open (AltosCalData cal_data) throws IOException, InterruptedException { + trace("open serial %d flight %d receiver_serial %d", + cal_data.serial, + cal_data.flight, + cal_data.receiver_serial); + AltosFile a = new AltosFile(cal_data); - log_file = new FileWriter(a, true); + trace("open file %s\n", a.getPath()); + + try { + log_file = new FileWriter(a, true); + } catch (IOException ie) { + log_file = null; + trace("open file failed\n"); + if (trace != null) + trace.open_failed(a); + } if (log_file != null) { + trace("open file success\n"); while (!pending_queue.isEmpty()) { String s = pending_queue.take(); log_file.write(s); @@ -79,6 +100,7 @@ public class AltosLog implements Runnable { } public void run () { + trace("log run start\n"); try { AltosConfigData receiver_config = link.config_data(); AltosCalData cal_data = new AltosCalData(); @@ -117,21 +139,30 @@ public class AltosLog implements Runnable { pending_queue.put(line.line); } } catch (InterruptedException ie) { + trace("interrupted exception\n"); } catch (TimeoutException te) { + trace("timeout exception\n"); } catch (IOException ie) { + trace("io exception %s message %s\n", ie.toString(), ie.getMessage()); } + trace("log run done\n"); close(); } - public AltosLog (AltosLink link) { + public AltosLog (AltosLink link, AltosLogTrace trace) { pending_queue = new LinkedBlockingQueue (); input_queue = new LinkedBlockingQueue (); link.add_monitor(input_queue); serial = -1; flight = -1; + this.trace = trace; this.link = link; log_file = null; log_thread = new Thread(this); log_thread.start(); } + + public AltosLog (AltosLink link) { + this(link, null); + } } diff --git a/altoslib/AltosLogTrace.java b/altoslib/AltosLogTrace.java new file mode 100644 index 00000000..64b41d61 --- /dev/null +++ b/altoslib/AltosLogTrace.java @@ -0,0 +1,28 @@ +/* + * Copyright © 2021 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altoslib_14; + +import java.io.*; +import java.net.*; + +public interface AltosLogTrace { + public abstract void trace(String format, Object ... arguments); + + public abstract void open_failed(File path); +} diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index 39e287fa..21651ef9 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -87,6 +87,7 @@ altoslib_JAVA = \ AltosLink.java \ AltosListenerState.java \ AltosLog.java \ + AltosLogTrace.java \ AltosMag.java \ AltosMma655x.java \ AltosMs5607.java \ -- 2.30.2