Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack trace to debug log #1092

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<groupId>com.fsck.k9.mail</groupId>
<artifactId>lib</artifactId>
<type>aar</type>
<version>0.0.1.b13cb5f</version>
<version>0.0.1.8f1d14a</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zegoggles/smssync/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.squareup.otto.Bus;

public class App extends Application {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final boolean DEBUG = true;
public static final boolean LOCAL_LOGV = DEBUG;
public static final String TAG = "SMSBackup+";
public static final String LOG = "sms_backup_plus.log";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.net.NetworkInfo;
import android.text.format.DateFormat;
import android.util.Log;
import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.MessagingException;
import com.squareup.otto.Produce;
import com.squareup.otto.Subscribe;
Expand All @@ -37,6 +38,7 @@
import com.zegoggles.smssync.service.exception.RequiresWifiException;
import com.zegoggles.smssync.service.state.BackupState;
import com.zegoggles.smssync.service.state.SmsSyncState;
import com.zegoggles.smssync.utils.AppLog;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -222,7 +224,14 @@ private void handleErrorState(BackupState state) {
appLog(R.string.app_log_backup_failed_connectivity, state.getDetailedErrorMessage(getResources()));
} else {
appLog(R.string.app_log_backup_failed_general_error, state.getDetailedErrorMessage(getResources()));

String stackTrace = state.getStackTrace();
if (stackTrace != null) {
appLogDebug(stackTrace);
final String k9Log = AppLog.snapshotCurrentLog(K9MailLib.LOG_TAG, 20);
if (k9Log != null) {
appLogDebug(k9Log);
}
}
if (shouldNotifyUser(state)) {
notifyUser(android.R.drawable.stat_sys_warning,
getString(R.string.notification_general_error),
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/zegoggles/smssync/service/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.zegoggles.smssync.service.exception.RequiresLoginException;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.EnumSet;

public abstract class State {
Expand Down Expand Up @@ -55,6 +58,21 @@ public String getDetailedErrorMessage(Resources resources) {
}
}

public String getStackTrace() {
Throwable throwable = exception;
if (throwable != null) {
if (throwable instanceof MessagingException && throwable.getCause() instanceof IOException) {
throwable = throwable.getCause();
}
final StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
else {
return null;
}
}

public boolean isInitialState() {
return state == SmsSyncState.INITIAL;
}
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/zegoggles/smssync/utils/AppLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zegoggles.smssync.App.LOCAL_LOGV;
import static com.zegoggles.smssync.App.TAG;

public class AppLog {
// keep max 32k worth of logs
static final int MAX_SIZE = 32 * 1024;
private static final int MAX_SIZE = 32 * 1024;
public static final int ID = 1;

private PrintWriter writer;
Expand Down Expand Up @@ -180,6 +183,27 @@ public static boolean readLog(File f, TextView view) {
return text.length() > 0;
}

public static String snapshotCurrentLog(String tag, int maxLines) {
Process logcat;
List<String> lines = new ArrayList<String>(50);
try {
logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d", tag+":*", "*:S"});
BufferedReader br = new BufferedReader(new InputStreamReader(logcat.getInputStream()), 8192);
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
final String separator = System.getProperty("line.separator");
final StringBuilder log = new StringBuilder();
for (int i=lines.size() - Math.min(maxLines, lines.size()); i<lines.size(); i++) {
log.append(lines.get(i)).append(separator);
}
return log.toString();
} catch (Exception ignored) {
return null;
}
}

static File getFile(String name) {
return new File(Environment.getExternalStorageDirectory(), name);
}
Expand Down