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 basic log export #994

Merged
merged 1 commit into from
Jun 18, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import fr.gaulupeau.apps.Poche.service.OperationsHelper;
import fr.gaulupeau.apps.Poche.ui.BaseActionBarActivity;
import fr.gaulupeau.apps.Poche.ui.Themes;
import fr.gaulupeau.apps.Poche.utils.LoggingUtils;

public class SettingsActivity extends BaseActionBarActivity {

Expand Down Expand Up @@ -127,6 +128,7 @@ public void onCreate(Bundle savedInstanceState) {
setOnClickListener(R.string.pref_key_misc_wipeDB);
setOnClickListener(R.string.pref_key_misc_localQueue_dumpToFile);
setOnClickListener(R.string.pref_key_misc_localQueue_removeFirstItem);
setOnClickListener(R.string.pref_key_misc_logging_logcatToFile);

ListPreference themeListPreference = (ListPreference)findPreference(
getString(R.string.pref_key_ui_theme));
Expand Down Expand Up @@ -513,6 +515,13 @@ public void onClick(DialogInterface dialogInterface, int i) {
removeFirstOfflineQueueItem();
return true;
}
case R.string.pref_key_misc_logging_logcatToFile: {
Activity activity = getActivity();
if (activity != null) {
LoggingUtils.saveLogcatToFile(activity);
}
return true;
}
}

return false;
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/fr/gaulupeau/apps/Poche/utils/LoggingUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package fr.gaulupeau.apps.Poche.utils;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.data.StorageHelper;

public class LoggingUtils {

private static final String TAG = LoggingUtils.class.getSimpleName();

public static void saveLogcatToFile(Activity context) {
String filePath = null;
try {
filePath = saveLogcatToFileInternal();

Toast.makeText(context, context.getString(R.string.misc_logging_logcatToFile_result_saved,
filePath), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.w(TAG, "Error saving logcat output to file", e);
Toast.makeText(context, context.getString(R.string.misc_logging_logcatToFile_result_error,
e.toString()), Toast.LENGTH_LONG).show();
}

if (filePath != null) {
WallabagFileProvider.shareFile(context, new File(filePath));
}
}

private static String saveLogcatToFileInternal() throws IOException {
if (!StorageHelper.isExternalStorageWritable()) {
throw new IllegalStateException("External storage is not writable!");
}

String path = StorageHelper.getExternalStoragePath() + "/"
+ "logcat_"
+ new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date())
+ ".txt";

Runtime.getRuntime().exec("logcat -d -f " + path);

return path;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package fr.gaulupeau.apps.Poche.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ShareCompat;
import androidx.core.content.FileProvider;

import java.io.File;
Expand All @@ -14,8 +18,30 @@ public class WallabagFileProvider extends FileProvider {

public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider";

private static final String TAG = WallabagFileProvider.class.getSimpleName();

public static Uri getUriForFile(@NonNull Context context, @NonNull File file) {
return getUriForFile(context, AUTHORITY, file);
}

public static boolean shareFile(@NonNull Activity activity, @NonNull File file) {
try {
Uri uri = getUriForFile(activity, file);

ShareCompat.IntentBuilder shareBuilder = ShareCompat.IntentBuilder.from(activity)
.setStream(uri)
.setType(activity.getContentResolver().getType(uri));

shareBuilder.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

shareBuilder.startChooser();

return true;
} catch (Exception e) {
Log.w(TAG, "Error sharing file", e);
}

return false;
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings-preference-keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<string name="pref_key_misc_localQueue_notice" translatable="false">misc.localQueue.notice</string>
<string name="pref_key_misc_localQueue_dumpToFile" translatable="false">misc.localQueue.dumpToFile</string>
<string name="pref_key_misc_localQueue_removeFirstItem" translatable="false">misc.localQueue.removeFirstItem</string>
<string name="pref_key_misc_logging_logcatToFile" translatable="false">misc.logging.logcatToFile</string>

<string name="pref_key_internal_preferencesVersion" translatable="false">internal.preferencesVersion</string>
<string name="pref_key_internal_firstRun" translatable="false">internal.firstRun</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,16 @@
<string name="pref_desc_misc_localQueue_dumpToFile">Dumps all local changes to a txt file (you can study it yourself or attach it to a bug report)</string>
<string name="pref_name_misc_localQueue_removeFirstItem">Remove first local queue item</string>
<string name="pref_desc_misc_localQueue_removeFirstItem">Removes the first not synchronized item. May help if you get repeating error notifications during sync. Be sure to dump all local changes to file first</string>
<string name="pref_name_misc_logging_logcatToFile">Save logcat output to a file</string>
<string name="pref_desc_misc_logging_logcatToFile">Saves logcat output (Android app log) to a file in /sdcard/Android/data/fr.gaulupeau.apps.InThePoche/files/</string>

<string name="misc_localQueue_empty">No local changes</string>
<string name="misc_localQueue_removeFirstItem_done">Done</string>
<string name="misc_localQueue_dumpToFile_result_dumped">Dumped to %s</string>
<string name="misc_localQueue_dumpToFile_result_error">Error: %s</string>
<string name="misc_localQueue_dumpToFile_header">If you experience troubles with synchronization, please report your problem here: %s</string>
<string name="misc_logging_logcatToFile_result_saved">Logs saved to %s</string>
<string name="misc_logging_logcatToFile_result_error">Error: %s</string>

<string name="issues_url" translatable="false">https://github.com/wallabag/android-app/issues</string>

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@
android:title="@string/pref_name_misc_localQueue_removeFirstItem" />
</PreferenceCategory>
</PreferenceScreen>
<Preference
android:key="@string/pref_key_misc_logging_logcatToFile"
android:persistent="false"
android:summary="@string/pref_desc_misc_logging_logcatToFile"
android:title="@string/pref_name_misc_logging_logcatToFile" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>