From 7ae0ed395ef6d5168a56a1c2f7fa0491670724e2 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 12 Sep 2018 10:54:37 +0000 Subject: [PATCH] Send log files as attachments --- .../java/eu/faircode/email/FragmentAbout.java | 103 ++++++++++++++++-- .../main/java/eu/faircode/email/Helper.java | 37 ------- 2 files changed, 92 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAbout.java b/app/src/main/java/eu/faircode/email/FragmentAbout.java index 494c5338..0a9feefb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAbout.java +++ b/app/src/main/java/eu/faircode/email/FragmentAbout.java @@ -31,7 +31,13 @@ import android.widget.Button; import android.widget.TextView; import android.widget.Toast; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -97,20 +103,10 @@ public class FragmentAbout extends FragmentEx { sb.append(String.format("Id: %s\r\n", Build.ID)); sb.append("\r\n"); - // Get recent log - long from = new Date().getTime() - 12 * 3600 * 1000L; - DateFormat DF = SimpleDateFormat.getTimeInstance(); - DB db = DB.getInstance(context); - for (EntityLog log : db.log().getLogs(from)) - sb.append(DF.format(log.time)).append(" ").append(log.data).append("\r\n"); - sb.append("\r\n"); - - sb.append(Helper.getLogcat()); - String body = "
" + sb.toString().replaceAll("\\r?\\n", "
") + "
"; EntityMessage draft; - + DB db = DB.getInstance(context); try { db.beginTransaction(); @@ -134,6 +130,91 @@ public class FragmentAbout extends FragmentEx { draft.id = db.message().insertMessage(draft); draft.write(context, body); + // Attach recent log + { + EntityAttachment log = new EntityAttachment(); + log.message = draft.id; + log.sequence = 1; + log.name = "log.txt"; + log.type = "text/plain"; + log.size = null; + log.progress = 0; + log.id = db.attachment().insertAttachment(log); + + OutputStream os = null; + File file = EntityAttachment.getFile(context, log.id); + try { + os = new BufferedOutputStream(new FileOutputStream(file)); + + int size = 0; + long from = new Date().getTime() - 24 * 3600 * 1000L; + DateFormat DF = SimpleDateFormat.getTimeInstance(); + for (EntityLog entry : db.log().getLogs(from)) { + String line = String.format("%s %s\r\n", DF.format(entry.time), entry.data); + byte[] bytes = line.getBytes(); + os.write(bytes); + size += bytes.length; + } + + log.size = size; + log.progress = null; + log.available = true; + db.attachment().updateAttachment(log); + } finally { + if (os != null) + os.close(); + } + } + + // Attach logcat + { + EntityAttachment logcat = new EntityAttachment(); + logcat.message = draft.id; + logcat.sequence = 2; + logcat.name = "logcat.txt"; + logcat.type = "text/plain"; + logcat.size = null; + logcat.progress = 0; + logcat.id = db.attachment().insertAttachment(logcat); + + Process proc = null; + BufferedReader br = null; + OutputStream os = null; + File file = EntityAttachment.getFile(context, logcat.id); + try { + os = new BufferedOutputStream(new FileOutputStream(file)); + + String[] cmd = new String[]{"logcat", + "-d", + "-v", "threadtime", + //"-t", "1000", + Helper.TAG + ":I"}; + proc = Runtime.getRuntime().exec(cmd); + br = new BufferedReader(new InputStreamReader(proc.getInputStream())); + + int size = 0; + String line; + while ((line = br.readLine()) != null) { + line += "\r\n"; + byte[] bytes = line.getBytes(); + os.write(bytes); + size += bytes.length; + } + + logcat.size = size; + logcat.progress = null; + logcat.available = true; + db.attachment().updateAttachment(logcat); + } finally { + if (os != null) + os.close(); + if (br != null) + br.close(); + if (proc != null) + proc.destroy(); + } + } + EntityOperation.queue(db, draft, EntityOperation.ADD); db.setTransactionSuccessful(); diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index d5b53bc5..ee6c5b2d 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -37,13 +37,11 @@ import android.widget.Spinner; import com.android.billingclient.api.BillingClient; import com.google.android.material.bottomnavigation.BottomNavigationView; -import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; @@ -126,41 +124,6 @@ public class Helper { return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); } - static StringBuilder getLogcat() { - StringBuilder sb = new StringBuilder(); - - Process proc = null; - BufferedReader br = null; - try { - String[] cmd = new String[]{"logcat", - "-d", - "-v", "threadtime", - "-t", "1000", - TAG + ":I"}; - proc = Runtime.getRuntime().exec(cmd); - br = new BufferedReader(new InputStreamReader(proc.getInputStream())); - String line; - while ((line = br.readLine()) != null) - sb.append(line).append("\r\n"); - } catch (IOException ex) { - Log.e(TAG, ex + "\n" + Log.getStackTraceString(ex)); - } finally { - if (br != null) - try { - br.close(); - } catch (IOException ignored) { - } - if (proc != null) - try { - proc.destroy(); - } catch (Throwable ex) { - Log.w(TAG, ex + "\n" + Log.getStackTraceString(ex)); - } - } - - return sb; - } - static Address myAddress() throws UnsupportedEncodingException { return new InternetAddress("marcel+fairemail@faircode.eu", "FairCode"); }