From 95e9e2ac30407722ac08fff055ac9839babbffc4 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 1 Mar 2015 02:02:39 +0530 Subject: added logging, notify for log export --- .../operations/results/OperationResult.java | 7 +++ .../keychain/ui/LogDisplayFragment.java | 72 ++++++++++++++++------ OpenKeychain/src/main/res/values/strings.xml | 7 +++ 3 files changed, 66 insertions(+), 20 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java index f79900aab..aef61075f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java @@ -707,6 +707,13 @@ public abstract class OperationResult implements Parcelable { MSG_DEL_CONSOLIDATE (LogLevel.DEBUG, R.string.msg_del_consolidate), MSG_DEL_OK (LogLevel.OK, R.plurals.msg_del_ok), MSG_DEL_FAIL (LogLevel.WARN, R.plurals.msg_del_fail), + + //export log + MSG_EXPORT_LOG(LogLevel.START,R.string.msg_export_log_start), + MSG_EXPORT_LOG_EXPORT_ERROR_NO_FILE(LogLevel.ERROR,R.string.msg_export_log_error_no_file), + MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN(LogLevel.ERROR,R.string.msg_export_log_error_fopen), + MSG_EXPORT_LOG_EXPORT_ERROR_WRITING(LogLevel.ERROR,R.string.msg_export_log_error_writing), + MSG_EXPORT_LOG_EXPORT_SUCCESS (LogLevel.OK, R.string.msg_export_log_success), ; public final int mMsgId; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 997b977ff..bd77294c1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -22,9 +22,8 @@ import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; +import android.os.Parcel; import android.support.v4.app.ListFragment; -import android.support.v4.view.MenuItemCompat; -import android.support.v7.widget.SearchView; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.Menu; @@ -37,7 +36,6 @@ import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -45,16 +43,11 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogEntryParcel; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.operations.results.OperationResult.SubLogEntryParcel; -import org.sufficientlysecure.keychain.provider.KeychainContract; -import org.sufficientlysecure.keychain.provider.KeychainDatabase; -import org.sufficientlysecure.keychain.ui.util.Notify; -import org.sufficientlysecure.keychain.util.ExportHelper; import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.Preferences; import java.io.File; -import java.io.IOException; +import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Iterator; @@ -100,7 +93,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe @Override public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { - Toast.makeText(this.getActivity(),"Options created",Toast.LENGTH_SHORT).show(); inflater.inflate(R.menu.log_display, menu); super.onCreateOptionsMenu(menu, inflater); @@ -123,24 +115,64 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) { + OperationResult.OperationLog currLog = new OperationResult.OperationLog(); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG,0); + + boolean error = false; PrintWriter pw = null; - try { - pw = new PrintWriter(f); - } catch(IOException e) { - e.printStackTrace(); - } Iterator logIterator = operationLog.iterator(); + try { + pw = new PrintWriter(f); - while(logIterator.hasNext()) { + while(logIterator.hasNext()) { - pw.println(getPrintableLogEntry(logIterator.next())); + pw.println(getPrintableLogEntry(logIterator.next())); + if(pw.checkError()) {//IOException + Log.e(Constants.TAG, "Log Export I/O Exception "+f.getAbsolutePath()); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); + error = true; + break; + } + } + } catch(FileNotFoundException e) { + Log.e(Constants.TAG, "File not found for exporting log "+f.getAbsolutePath()); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1); + error = true; } + if(pw!=null) { + pw.close(); + error = error && pw.checkError();//check for errors on closing PrintWriter object too + } + + if(!error) currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_SUCCESS,1); - pw.close(); + int opResultCode = error?OperationResult.RESULT_ERROR:OperationResult.RESULT_OK; + OperationResult opResult = new LogExportResult(opResultCode,currLog); + opResult.createNotify(getActivity()).show(); } + private static class LogExportResult extends OperationResult { + public LogExportResult(int result, OperationLog log) { + super(result, log); + } + + /** trivial but necessary to implement the Parcelable protocol. */ + public LogExportResult(Parcel source) { + super(source); + } + + public static Creator CREATOR = new Creator() { + public LogExportResult createFromParcel(final Parcel source) { + return new LogExportResult(source); + } + + public LogExportResult[] newArray(final int size) { + return new LogExportResult[size]; + } + }; + } /** * returns an indented String of a LogEntryParcel * @param entry log entry whose String representation is to be obtained @@ -162,7 +194,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe case DEBUG: subLogText+="[DEBUG]"; break; case INFO: subLogText+="[INFO]"; break; case WARN: subLogText+="[WARN]"; break; - case ERROR: subLogText+="[WARN]"; break; + case ERROR: subLogText+="[ERROR]"; break; case START: subLogText+="[START]"; break; case OK: subLogText+="[OK]"; break; case CANCELLED: subLogText+="[CANCELLED]"; break; @@ -192,7 +224,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe case DEBUG: logText="[DEBUG]"; break; case INFO: logText="[INFO]"; break; case WARN: logText="[WARN]"; break; - case ERROR: logText="[WARN]"; break; + case ERROR: logText="[ERROR]"; break; case START: logText="[START]"; break; case OK: logText="[OK]"; break; case CANCELLED: logText="[CANCELLED]"; break; diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 917c64bda..ed1dd1233 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -1168,6 +1168,13 @@ "An error occurred when searching for keys." + + "Exporting log" + "Error opening file" + "No file name specified!" + "I/O error writing to file!" + "Log exported successfully!" + "Click to clear cached passphrases" "OpenKeychain has cached %d passphrases" -- cgit v1.2.3