Skip to content

Commit

Permalink
Show a notification if file write failure occurs. Continues logging a…
Browse files Browse the repository at this point in the history
…nyway, except for NMEA, which stops logging. This is because NMEA has a high frequency of writes.

Issue #1138 #1053
  • Loading branch information
mendhak committed May 27, 2024
1 parent 972c06e commit 708b60b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ private void writeToFile(Location loc) {
}
catch(Exception e){
LOG.error(getString(R.string.could_not_write_to_file), e);
Systems.showErrorNotification(this, getString(R.string.could_not_write_to_file));
}

session.clearDescription();
Expand Down Expand Up @@ -1174,6 +1175,14 @@ public void onEvent(CommandEvents.LogOnce logOnce){
}


@EventBusHook
public void onEvent(CommandEvents.FileWriteFailure writeFailure){
Systems.showErrorNotification(this, getString(R.string.could_not_write_to_file));
if(writeFailure.stopLoggingDueToNMEA){
LOG.error("Could not write to NMEA file, stopping logging due to high frequency of write failures");
stopLogging();
}
}

@EventBusHook
public void onEvent(ProfileEvents.SwitchToProfile switchToProfileEvent){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ public Annotate(String annotation) {
*/
public static class LogOnce {
}

/**
* Used to indicate that the file write failed.
* The intention is to then notify the user of the failure since it represents data loss.
* Pass stopLogging, if true, ask the logging to stop. Use this for NMEA which is very high frequency.
*/
public static class FileWriteFailure {
public boolean stopLoggingDueToNMEA;

public FileWriteFailure(){
this.stopLoggingDueToNMEA = false;
}
public FileWriteFailure(boolean stopLoggingDueToNMEA) {
this.stopLoggingDueToNMEA = stopLoggingDueToNMEA;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import androidx.annotation.NonNull;

import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.CommandEvents;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.loggers.Files;

Expand All @@ -13,6 +14,8 @@
import java.io.IOException;
import java.io.RandomAccessFile;

import de.greenrobot.event.EventBus;

/**
* Created by clemens on 10.05.17.
*/
Expand Down Expand Up @@ -66,8 +69,8 @@ public void run() {
raf.close();
}
} catch (IOException e) {
e.printStackTrace();
LOG.error("GeoJSONWriterPoints", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure());
LOG.error("Failed to write to GeoJSON file", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.mendhak.gpslogger.common.PreferenceHelper;
import com.mendhak.gpslogger.common.RejectionHandler;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.CommandEvents;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.loggers.FileLogger;
import com.mendhak.gpslogger.loggers.Files;
Expand All @@ -37,6 +38,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import de.greenrobot.event.EventBus;


public class Gpx10FileLogger implements FileLogger {
protected final static Object lock = new Object();
Expand Down Expand Up @@ -160,7 +163,8 @@ public void run() {

LOG.debug("Finished annotation to GPX10 File");
} catch (Exception e) {
LOG.error("Gpx10FileLogger.annotate", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure());
LOG.error("Error annotating GPX file", e);
}

}
Expand Down Expand Up @@ -241,7 +245,8 @@ public void run() {
LOG.debug("Finished writing to GPX10 file");

} catch (Exception e) {
LOG.error("Gpx10FileLogger.write", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure());
LOG.error("Error writing to GPX file", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.mendhak.gpslogger.common.PreferenceHelper;
import com.mendhak.gpslogger.common.RejectionHandler;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.CommandEvents;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.loggers.FileLogger;
import com.mendhak.gpslogger.loggers.Files;
Expand All @@ -36,6 +37,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import de.greenrobot.event.EventBus;

public class Kml22FileLogger implements FileLogger {
protected final static Object lock = new Object();
private final static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
Expand Down Expand Up @@ -117,7 +120,8 @@ public void run() {

}
} catch (Exception e) {
LOG.error("Kml22FileLogger.annotate", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure());
LOG.error("Error writing KML annotation", e);
}
}

Expand Down Expand Up @@ -222,7 +226,8 @@ public void run() {
}

} catch (Exception e) {
LOG.error("Kml22FileLogger.write", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure());
LOG.error("Error writing KML file", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import com.mendhak.gpslogger.common.RejectionHandler;
import com.mendhak.gpslogger.common.Session;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.CommandEvents;
import com.mendhak.gpslogger.common.slf4j.Logs;
import com.mendhak.gpslogger.loggers.Files;

import org.slf4j.Logger;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand All @@ -33,9 +37,12 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import de.greenrobot.event.EventBus;

public class NmeaFileLogger {

protected final static Object lock = new Object();
private static final Logger LOG = Logs.of(NmeaFileLogger.class);
String fileName;
private final static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(10), new RejectionHandler());
Expand All @@ -60,7 +67,8 @@ public void write(long timestamp, String nmeaSentence) {
try {
nmeaFile.createNewFile();
} catch (IOException e) {

LOG.error("Error creating NMEA file", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure(true));
}
}

Expand All @@ -71,6 +79,7 @@ public void write(long timestamp, String nmeaSentence) {

class NmeaWriteHandler implements Runnable {

private static final Logger LOG = Logs.of(NmeaWriteHandler.class);
File gpxFile;
String nmeaSentence;

Expand All @@ -92,7 +101,8 @@ public void run() {
Files.addToMediaDatabase(gpxFile, "text/plain");

} catch (IOException e) {

LOG.error("Error writing NMEA sentence", e);
EventBus.getDefault().post(new CommandEvents.FileWriteFailure(true));
}
}

Expand Down

0 comments on commit 708b60b

Please sign in to comment.