periodic log to disk using async-tasks

This commit is contained in:
2016-06-19 21:16:22 +02:00
parent 48bfd34311
commit dad881e2b0
2 changed files with 74 additions and 15 deletions

View File

@@ -259,10 +259,10 @@ public class MainActivity extends WearableActivity {
}
// dump buffer stats every x entries
if (++loadCounterAny % 100 == 0) {
if (++loadCounterAny % 250 == 0) {
final TextView txt = (TextView) findViewById(R.id.txtBuffer);
final int kbPerMin = (int) (logger.getSize() / 1024 * 1000 * 60 / (System.currentTimeMillis() - logger.getStartTS()));
txt.setText( (logger.getSize() / 1024) + "kb, " + logger.getNumEntries() + ", " + kbPerMin + "kb/min");
final int kbPerMin = (int) (logger.getTotalSize() / 1024 * 1000 * 60 / (System.currentTimeMillis() - logger.getStartTS()));
txt.setText( (logger.getCurrentSize() / 1024) + "k, " + logger.getNumEntries() + ", " + kbPerMin + "k/m");
}
}

View File

@@ -1,13 +1,11 @@
package de.fhws.indoor.sensorreadout.sensors;
import android.content.Context;
import android.os.Environment;
import android.os.AsyncTask;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/**
* log sensor data to file
@@ -15,11 +13,16 @@ import java.io.OutputStreamWriter;
*/
public final class Logger {
private static final int FLUSH_LIMIT = 2*1024*1024;
private StringBuilder sb = new StringBuilder();
private File file;
private FileOutputStream fos;
private Context context;
private int entries = 0;
private int sizeCurrent = 0;
private int sizeTotal = 0;
/** timestamp of logging start. all entries are relative to this one */
private long startTS = 0;
@@ -34,6 +37,8 @@ public final class Logger {
// start empty
sb.setLength(0);
entries = 0;
sizeTotal = 0;
sizeCurrent = 0;
// starting timestamp
startTS = System.currentTimeMillis();
@@ -55,12 +60,8 @@ public final class Logger {
/** stop logging and flush RAM-data to the flash-chip */
public final void stop() {
synchronized (this) {
try {
fos.write(sb.toString().getBytes());
fos.close();
} catch (final Exception e) {
throw new MyException("error while writing log-file", e);
}
flush(true);
close();
}
}
@@ -68,7 +69,8 @@ public final class Logger {
return file;
}
public int getSize() {return sb.length();}
public int getCurrentSize() {return sizeCurrent;}
public int getTotalSize() {return sizeTotal;}
public int getNumEntries() {return entries;}
@@ -83,18 +85,75 @@ public final class Logger {
sb.append(csv);
sb.append('\n');
++entries;
sizeTotal += csv.length() + 10; // approx!
sizeCurrent = sb.length();
if (sb.length() > FLUSH_LIMIT) {flush(false);}
}
debug();
}
/** helper method for exception-less writing. DO NOT CALL DIRECTLY! */
private final void _write(final byte[] data) {
try {
fos.write(data);
Log.d("logger", "flushed " + data.length + " bytes to disk");
} catch (final Exception e) {
throw new RuntimeException("error while writing log-file", e);
}
}
/** helper-class for background writing */
class FlushAsync extends AsyncTask<byte[], Integer, Integer> {
@Override
protected final Integer doInBackground(byte[][] data) {
_write(data[0]);
return null;
}
};
/** flush current buffer-contents to disk */
private final void flush(boolean sync) {
// fetch current buffer contents to write and hereafter empty the buffer
// this action MUST be atomic, just like the add-method
byte[] data = null;
synchronized (this) {
data = sb.toString().getBytes(); // fetch data to write
sb.setLength(0); // reset the buffer
sizeCurrent = 0;
}
// write
if (sync) {
// write to disk using the current thread
_write(data);
} else {
// write to disk using a background-thread
new FlushAsync().execute(new byte[][] {data});
}
}
private final void close() {
try {
fos.close();
} catch (final Exception e) {
throw new MyException("error while writing log-file", e);
}
}
public final long getStartTS() {
return startTS;
}
int cnt = 0;
private final void debug() {
if (++cnt % 500 == 0) {
Log.d("buffer", "size: " + sb.length());
if (++cnt % 1000 == 0) {
Log.d("buffer", "size: " + sizeCurrent);
}
}