added logging, notify for log export

This commit is contained in:
Adithya Abraham Philip 2015-03-01 02:02:39 +05:30
parent 6a26ea908e
commit 95e9e2ac30
3 changed files with 67 additions and 21 deletions

View File

@ -707,6 +707,13 @@ public abstract class OperationResult implements Parcelable {
MSG_DEL_CONSOLIDATE (LogLevel.DEBUG, R.string.msg_del_consolidate), MSG_DEL_CONSOLIDATE (LogLevel.DEBUG, R.string.msg_del_consolidate),
MSG_DEL_OK (LogLevel.OK, R.plurals.msg_del_ok), MSG_DEL_OK (LogLevel.OK, R.plurals.msg_del_ok),
MSG_DEL_FAIL (LogLevel.WARN, R.plurals.msg_del_fail), 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; public final int mMsgId;

View File

@ -22,9 +22,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -37,7 +36,6 @@ import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; 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.LogEntryParcel;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel;
import org.sufficientlysecure.keychain.operations.results.OperationResult.SubLogEntryParcel; 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.FileHelper;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.FileNotFoundException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Iterator; import java.util.Iterator;
@ -100,7 +93,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
@Override @Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { 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); inflater.inflate(R.menu.log_display, menu);
super.onCreateOptionsMenu(menu, inflater); 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) { 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; PrintWriter pw = null;
try {
pw = new PrintWriter(f);
} catch(IOException e) {
e.printStackTrace();
}
Iterator<LogEntryParcel> logIterator = operationLog.iterator(); Iterator<LogEntryParcel> 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(); 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);
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<LogExportResult> CREATOR = new Creator<LogExportResult>() {
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 * returns an indented String of a LogEntryParcel
* @param entry log entry whose String representation is to be obtained * @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 DEBUG: subLogText+="[DEBUG]"; break;
case INFO: subLogText+="[INFO]"; break; case INFO: subLogText+="[INFO]"; break;
case WARN: subLogText+="[WARN]"; break; case WARN: subLogText+="[WARN]"; break;
case ERROR: subLogText+="[WARN]"; break; case ERROR: subLogText+="[ERROR]"; break;
case START: subLogText+="[START]"; break; case START: subLogText+="[START]"; break;
case OK: subLogText+="[OK]"; break; case OK: subLogText+="[OK]"; break;
case CANCELLED: subLogText+="[CANCELLED]"; break; case CANCELLED: subLogText+="[CANCELLED]"; break;
@ -192,7 +224,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
case DEBUG: logText="[DEBUG]"; break; case DEBUG: logText="[DEBUG]"; break;
case INFO: logText="[INFO]"; break; case INFO: logText="[INFO]"; break;
case WARN: logText="[WARN]"; break; case WARN: logText="[WARN]"; break;
case ERROR: logText="[WARN]"; break; case ERROR: logText="[ERROR]"; break;
case START: logText="[START]"; break; case START: logText="[START]"; break;
case OK: logText="[OK]"; break; case OK: logText="[OK]"; break;
case CANCELLED: logText="[CANCELLED]"; break; case CANCELLED: logText="[CANCELLED]"; break;

View File

@ -1168,6 +1168,13 @@
<string name="msg_download_query_failed">"An error occurred when searching for keys."</string> <string name="msg_download_query_failed">"An error occurred when searching for keys."</string>
<!-- Messages for Export Log operation -->
<string name="msg_export_log_start">"Exporting log"</string>
<string name="msg_export_log_error_fopen">"Error opening file"</string>
<string name="msg_export_log_error_no_file">"No file name specified!"</string>
<string name="msg_export_log_error_writing">"I/O error writing to file!"</string>
<string name="msg_export_log_success">"Log exported successfully!"</string>
<!-- PassphraseCache --> <!-- PassphraseCache -->
<string name="passp_cache_notif_click_to_clear">"Click to clear cached passphrases"</string> <string name="passp_cache_notif_click_to_clear">"Click to clear cached passphrases"</string>
<string name="passp_cache_notif_n_keys">"OpenKeychain has cached %d passphrases"</string> <string name="passp_cache_notif_n_keys">"OpenKeychain has cached %d passphrases"</string>