First draft
This commit is contained in:
parent
0616c16e9f
commit
2090b3c7e1
11 changed files with 235 additions and 7 deletions
|
@ -38,6 +38,9 @@ import com.squareup.otto.Subscribe;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import info.nightscout.androidaps.data.Profile;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.events.EventAppExit;
|
import info.nightscout.androidaps.events.EventAppExit;
|
||||||
import info.nightscout.androidaps.events.EventFeatureRunning;
|
import info.nightscout.androidaps.events.EventFeatureRunning;
|
||||||
|
@ -53,6 +56,7 @@ import info.nightscout.utils.AndroidPermission;
|
||||||
import info.nightscout.utils.ImportExportPrefs;
|
import info.nightscout.utils.ImportExportPrefs;
|
||||||
import info.nightscout.utils.LocaleHelper;
|
import info.nightscout.utils.LocaleHelper;
|
||||||
import info.nightscout.utils.LogDialog;
|
import info.nightscout.utils.LogDialog;
|
||||||
|
import info.nightscout.utils.LoggerUtils;
|
||||||
import info.nightscout.utils.OKDialog;
|
import info.nightscout.utils.OKDialog;
|
||||||
import info.nightscout.utils.PasswordProtection;
|
import info.nightscout.utils.PasswordProtection;
|
||||||
import info.nightscout.utils.SP;
|
import info.nightscout.utils.SP;
|
||||||
|
@ -391,6 +395,17 @@ public class MainActivity extends AppCompatActivity {
|
||||||
return true;
|
return true;
|
||||||
case R.id.nav_show_logcat:
|
case R.id.nav_show_logcat:
|
||||||
LogDialog.showLogcat(this);
|
LogDialog.showLogcat(this);
|
||||||
|
return true;
|
||||||
|
case R.id.nav_export_log:
|
||||||
|
String recipient = "mmay@gmx.net";
|
||||||
|
|
||||||
|
String logDirectory = LoggerUtils.getLogDirectory();
|
||||||
|
List<File> logs = LoggerUtils.getLogfiles(logDirectory, 2);
|
||||||
|
String zipName = LoggerUtils.constructName();
|
||||||
|
File zip = LoggerUtils.zipLogs(zipName, logDirectory, logs);
|
||||||
|
Intent emailIntent = LoggerUtils.sendMail(zip, recipient, "Log Export");
|
||||||
|
startActivity(Intent.createChooser(emailIntent , "Send email..."));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
case R.id.nav_about:
|
case R.id.nav_about:
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
|
|
|
@ -78,6 +78,7 @@ import info.nightscout.androidaps.receivers.DataReceiver;
|
||||||
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
||||||
import info.nightscout.androidaps.receivers.NSAlarmReceiver;
|
import info.nightscout.androidaps.receivers.NSAlarmReceiver;
|
||||||
import info.nightscout.utils.FabricPrivacy;
|
import info.nightscout.utils.FabricPrivacy;
|
||||||
|
import info.nightscout.utils.LoggerUtils;
|
||||||
import info.nightscout.utils.NSUpload;
|
import info.nightscout.utils.NSUpload;
|
||||||
import io.fabric.sdk.android.Fabric;
|
import io.fabric.sdk.android.Fabric;
|
||||||
|
|
||||||
|
@ -127,7 +128,7 @@ public class MainApp extends Application {
|
||||||
log.info("Version: " + BuildConfig.VERSION_NAME);
|
log.info("Version: " + BuildConfig.VERSION_NAME);
|
||||||
log.info("BuildVersion: " + BuildConfig.BUILDVERSION);
|
log.info("BuildVersion: " + BuildConfig.BUILDVERSION);
|
||||||
|
|
||||||
String extFilesDir = this.getLogDirectory();
|
String extFilesDir = LoggerUtils.getLogDirectory();
|
||||||
File engineeringModeSemaphore = new File(extFilesDir, "engineering_mode");
|
File engineeringModeSemaphore = new File(extFilesDir, "engineering_mode");
|
||||||
|
|
||||||
engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile();
|
engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile();
|
||||||
|
@ -388,11 +389,6 @@ public class MainApp extends Application {
|
||||||
return devBranch;
|
return devBranch;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogDirectory() {
|
|
||||||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
|
||||||
return lc.getProperty("EXT_FILES_DIR");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTerminate() {
|
public void onTerminate() {
|
||||||
super.onTerminate();
|
super.onTerminate();
|
||||||
|
|
171
app/src/main/java/info/nightscout/utils/LoggerUtils.java
Normal file
171
app/src/main/java/info/nightscout/utils/LoggerUtils.java
Normal file
|
@ -0,0 +1,171 @@
|
||||||
|
package info.nightscout.utils;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.LoggerContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides serveral methods for log-handling (eg. sending logs as emails).
|
||||||
|
*/
|
||||||
|
public class LoggerUtils {
|
||||||
|
|
||||||
|
private static String SUFFIX = ".log.zip";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the directory, in which the logs are stored on the system. This is configured in the
|
||||||
|
* logback.xml file.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getLogDirectory() {
|
||||||
|
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||||
|
return lc.getProperty("EXT_FILES_DIR");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a list of log files. The number of returned logs is given via the amount
|
||||||
|
* parameter. The log files are sorted by the name descending.
|
||||||
|
*
|
||||||
|
* @param directory
|
||||||
|
* @param amount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static List<File> getLogfiles(String directory, int amount) {
|
||||||
|
File logDir = new File(directory);
|
||||||
|
|
||||||
|
File[] files = logDir.listFiles(new FilenameFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file, String name) {
|
||||||
|
return name.startsWith("AndroidAPS")
|
||||||
|
&& (name.endsWith(".log")
|
||||||
|
|| (name.endsWith(".zip") && !name.endsWith(SUFFIX)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Arrays.sort(files, new Comparator<File>() {
|
||||||
|
public int compare(File f1, File f2) {
|
||||||
|
return f2.getName().compareTo(f1.getName());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
List<File> result = Arrays.asList(files);
|
||||||
|
int toIndex = amount++;
|
||||||
|
|
||||||
|
|
||||||
|
if (toIndex > result.size()) {
|
||||||
|
toIndex = result.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.subList(0, toIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zips the given files in a zipfile which is stored in the given directory using the givven
|
||||||
|
* name.
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* @param directory
|
||||||
|
* @param files
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static File zipLogs(String name, String directory, List<File> files) {
|
||||||
|
File zip = new File(directory, name);
|
||||||
|
|
||||||
|
try {
|
||||||
|
zip(zip, files);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.print("REmomve this one sooner or later....");
|
||||||
|
}
|
||||||
|
|
||||||
|
return zip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct the name of zip file which is used to export logs.
|
||||||
|
*
|
||||||
|
* The name is constructed using the following scheme:
|
||||||
|
* AndroidAPS_LOG_ + Long Time + .log.zip
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String constructName() {
|
||||||
|
return "AndroidAPS_LOG_" + String.valueOf(new Date().getTime()) + SUFFIX;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method stores all given files inside the given zipFile.
|
||||||
|
*
|
||||||
|
* @param zipFile
|
||||||
|
* @param files
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void zip(File zipFile, List<File> files) throws IOException {
|
||||||
|
final int BUFFER_SIZE = 2048;
|
||||||
|
|
||||||
|
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
byte data[] = new byte[BUFFER_SIZE];
|
||||||
|
|
||||||
|
try(FileInputStream fileInputStream = new FileInputStream( file )) {
|
||||||
|
|
||||||
|
try(BufferedInputStream origin = new BufferedInputStream(fileInputStream, BUFFER_SIZE)) {
|
||||||
|
ZipEntry entry = new ZipEntry(file.getName());
|
||||||
|
|
||||||
|
out.putNextEntry(entry);
|
||||||
|
int count;
|
||||||
|
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
|
||||||
|
out.write(data, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* send a mail with the given file to the recipients with the given subject.
|
||||||
|
*
|
||||||
|
* the returned intent should be used to really send the mail using
|
||||||
|
*
|
||||||
|
* startActivity(Intent.createChooser(emailIntent , "Send email..."));
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @param recipient
|
||||||
|
* @param subject
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Intent sendMail(File file, String recipient, String subject) {
|
||||||
|
Intent emailIntent = new Intent(Intent.ACTION_SEND);
|
||||||
|
|
||||||
|
emailIntent .setType("vnd.android.cursor.dir/email");
|
||||||
|
emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{recipient});
|
||||||
|
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
||||||
|
|
||||||
|
Uri path = Uri.fromFile(file);
|
||||||
|
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
|
||||||
|
|
||||||
|
return emailIntent;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -34,6 +34,10 @@
|
||||||
android:id="@+id/nav_show_logcat"
|
android:id="@+id/nav_show_logcat"
|
||||||
app:showAsAction="never"
|
app:showAsAction="never"
|
||||||
android:title="@string/nav_show_logcat" />
|
android:title="@string/nav_show_logcat" />
|
||||||
|
<item
|
||||||
|
android:id="@+id/nav_export_log"
|
||||||
|
app:showAsAction="never"
|
||||||
|
android:title="@string/nav_export_log" />
|
||||||
<item
|
<item
|
||||||
android:id="@+id/nav_about"
|
android:id="@+id/nav_about"
|
||||||
app:showAsAction="never"
|
app:showAsAction="never"
|
||||||
|
|
|
@ -500,6 +500,7 @@
|
||||||
<string name="copy_to_clipboard">Copy To Clipboard</string>
|
<string name="copy_to_clipboard">Copy To Clipboard</string>
|
||||||
<string name="copied_to_clipboard">Copied to clipboard</string>
|
<string name="copied_to_clipboard">Copied to clipboard</string>
|
||||||
<string name="nav_show_logcat">Show log</string>
|
<string name="nav_show_logcat">Show log</string>
|
||||||
|
<string name="nav_export_log">Export Logs</string>
|
||||||
<string name="overview_calibration">Calibration</string>
|
<string name="overview_calibration">Calibration</string>
|
||||||
<string name="send_calibration" formatted="false">Send calibration %.1f to xDrip?</string>
|
<string name="send_calibration" formatted="false">Send calibration %.1f to xDrip?</string>
|
||||||
<string name="xdripnotinstalled">xDrip+ not installed</string>
|
<string name="xdripnotinstalled">xDrip+ not installed</string>
|
||||||
|
|
|
@ -12,6 +12,7 @@ import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.interfaces.PluginType;
|
import info.nightscout.androidaps.interfaces.PluginType;
|
||||||
import info.nightscout.androidaps.interfaces.PumpInterface;
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
||||||
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
|
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
|
||||||
|
import info.nightscout.utils.LoggerUtils;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,7 +128,7 @@ public class MainAppTest {
|
||||||
@Test
|
@Test
|
||||||
public void getLogDirectoryTest() {
|
public void getLogDirectoryTest() {
|
||||||
// logger not initialized in Roboelectric
|
// logger not initialized in Roboelectric
|
||||||
Assert.assertNull(mainApp.getLogDirectory());
|
Assert.assertNull(LoggerUtils.getLogDirectory());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
40
app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java
Normal file
40
app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
package info.nightscout.utils;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class LoggerUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getLogfilesTest() {
|
||||||
|
String logDirectory = "src/test/res/logger";
|
||||||
|
|
||||||
|
List<File> logs = LoggerUtils.getLogfiles(logDirectory, 2);
|
||||||
|
assertEquals(2, logs.size());
|
||||||
|
assertEquals("AndroidAPS.log", logs.get(0).getName());
|
||||||
|
assertEquals("AndroidAPS.2018-01-03_01-01-00.1.zip", logs.get(1).getName());
|
||||||
|
|
||||||
|
logs = LoggerUtils.getLogfiles(logDirectory, 10);
|
||||||
|
assertEquals(4, logs.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void zipLogsTest() {
|
||||||
|
String logDirectory = "src/test/res/logger";
|
||||||
|
List<File> logs = LoggerUtils.getLogfiles(logDirectory, 2);
|
||||||
|
|
||||||
|
String name = "AndroidAPS.log.zip";
|
||||||
|
|
||||||
|
File zipFile = LoggerUtils.zipLogs(name, "build", logs);
|
||||||
|
|
||||||
|
assertTrue(zipFile.exists());
|
||||||
|
assertTrue(zipFile.isFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
0
app/src/test/res/logger/AndroidAPS.log
Normal file
0
app/src/test/res/logger/AndroidAPS.log
Normal file
Loading…
Reference in a new issue