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

ApplicationLogFileDir #506

Merged
merged 1 commit into from
Aug 31, 2016
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
9 changes: 9 additions & 0 deletions src/main/java/org/acra/annotation/ReportsCrashes.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.acra.config.RetryPolicy;
import org.acra.dialog.BaseCrashReportDialog;
import org.acra.dialog.CrashReportDialog;
import org.acra.file.Directory;
import org.acra.security.KeyStoreFactory;
import org.acra.security.NoKeyStoreFactory;
import org.acra.sender.DefaultReportSenderFactory;
Expand Down Expand Up @@ -553,6 +554,14 @@
*/
int applicationLogFileLines() default ACRAConstants.DEFAULT_APPLICATION_LOGFILE_LINES;

/**
* To use in combination with {@link ReportField#APPLICATION_LOG} to set the root
* for the path provided in {@link #applicationLogFile()}
*
* @return the directory of the application log file
*/
@NonNull Directory applicationLogFileDir() default Directory.FILES_LEGACY;

/**
* @return Class for the CrashReportDialog used when prompting the user for crash details.
* If not provided, defaults to CrashReportDialog.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public CrashReportData createCrashData(@NonNull ReportBuilder builder) {
// Application specific log file
if (crashReportFields.contains(APPLICATION_LOG)) {
try {
final String logFile = new LogFileCollector().collectLogFile(context, config.applicationLogFile(), config.applicationLogFileLines());
final String logFile = new LogFileCollector().collectLogFile(context, config.applicationLogFileDir(), config.applicationLogFile(), config.applicationLogFileLines());
crashReportData.put(APPLICATION_LOG, logFile);
} catch (IOException e) {
ACRA.log.e(LOG_TAG, "Error while reading application log file " + config.applicationLogFile(), e);
Expand Down
70 changes: 52 additions & 18 deletions src/main/java/org/acra/collector/LogFileCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;

import org.acra.ACRA;
import org.acra.file.Directory;
import org.acra.util.IOUtils;

import java.io.*;
Expand Down Expand Up @@ -49,28 +52,59 @@ class LogFileCollector {
* @throws IOException
*/
@NonNull
public String collectLogFile(@NonNull Context context, @NonNull String fileName, int numberOfLines) throws IOException {
return IOUtils.streamToString(getStream(context, fileName), numberOfLines);
public String collectLogFile(@NonNull Context context, @NonNull Directory directory, @NonNull String fileName, int numberOfLines) throws IOException {
return IOUtils.streamToString(getStream(context, directory, fileName), numberOfLines);
}

@NonNull
private static InputStream getStream(@NonNull Context context, @NonNull String fileName) {
try {
final FileInputStream inputStream;
if (fileName.startsWith("/")) {
// Absolute path
inputStream = new FileInputStream(fileName);
} else if (fileName.contains("/")) {
// Relative path from the application files folder (ie a sub folder)
inputStream = new FileInputStream(new File(context.getFilesDir(), fileName));
} else {
// A file directly contained within the application files folder.
inputStream = context.openFileInput(fileName);
private static InputStream getStream(@NonNull Context context, @NonNull Directory directory, @NonNull String fileName) {
if (directory == Directory.FILES_LEGACY) {
directory = fileName.startsWith("/") ? Directory.ROOT : Directory.FILES;
}
final File dir;
switch (directory) {
case FILES:
dir = context.getFilesDir();
break;
case EXTERNAL_FILES:
dir = context.getExternalFilesDir(null);
break;
case CACHE:
dir = context.getCacheDir();
break;
case EXTERNAL_CACHE:
dir = context.getExternalCacheDir();
break;
case NO_BACKUP_FILES:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dir = context.getNoBackupFilesDir();
} else {
dir = context.getFilesDir();
}
break;
case EXTERNAL_STORAGE:
dir = Environment.getExternalStorageDirectory();
break;
case ROOT:
default:
dir = new File("/");
break;
}
final File file = new File(dir, fileName);
if (!file.exists()) {
if (ACRA.DEV_LOGGING)
ACRA.log.d(LOG_TAG, "Log file '" + file.getPath() + "' does not exist");
} else if (file.isDirectory()) {
ACRA.log.e(LOG_TAG, "Log file '" + file.getPath() + "' is a directory");
} else if (!file.canRead()) {
ACRA.log.e(LOG_TAG, "Log file '" + file.getPath() + "' can't be read");
} else {
try {
return new FileInputStream(file);
} catch (IOException e) {
ACRA.log.e(LOG_TAG, "Could not open stream for log file '" + file.getPath() + "'");
}
return inputStream;
} catch (FileNotFoundException e) {
ACRA.log.e(LOG_TAG, "Cannot find application log file : '" + fileName + '\'');
return new ByteArrayInputStream(new byte[0]);
}
return new ByteArrayInputStream(new byte[0]);
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/acra/config/ACRAConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.acra.builder.ReportPrimer;
import org.acra.collections.ImmutableList;
import org.acra.dialog.BaseCrashReportDialog;
import org.acra.file.Directory;
import org.acra.security.KeyStoreFactory;
import org.acra.sender.HttpSender.Method;
import org.acra.sender.HttpSender.Type;
Expand Down Expand Up @@ -101,6 +102,7 @@ public final class ACRAConfiguration implements Serializable {
private final Class buildConfigClass;
private final String applicationLogFile;
private final int applicationLogFileLines;
private final Directory applicationLogFileDir;

private final Method httpMethod;
private final Type reportType;
Expand Down Expand Up @@ -156,6 +158,7 @@ public final class ACRAConfiguration implements Serializable {
buildConfigClass = builder.buildConfigClass();
applicationLogFile = builder.applicationLogFile();
applicationLogFileLines = builder.applicationLogFileLines();
applicationLogFileDir = builder.applicationLogFileDir();
reportDialogClass = builder.reportDialogClass();
reportPrimerClass = builder.reportPrimerClass();
httpMethod = builder.httpMethod();
Expand Down Expand Up @@ -374,6 +377,11 @@ public int applicationLogFileLines() {
return applicationLogFileLines;
}

@NonNull
public Directory applicationLogFileDir() {
return applicationLogFileDir;
}

@NonNull
public Class<? extends BaseCrashReportDialog> reportDialogClass() {
return reportDialogClass;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/acra/config/ConfigurationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.acra.builder.ReportPrimer;
import org.acra.dialog.BaseCrashReportDialog;
import org.acra.dialog.CrashReportDialog;
import org.acra.file.Directory;
import org.acra.security.KeyStoreFactory;
import org.acra.security.NoKeyStoreFactory;
import org.acra.sender.DefaultReportSenderFactory;
Expand Down Expand Up @@ -108,6 +109,7 @@ public final class ConfigurationBuilder {
private Class buildConfigClass;
private String applicationLogFile;
private Integer applicationLogFileLines;
private Directory applicationLogFileDir;

private Method httpMethod;
private Type reportType;
Expand Down Expand Up @@ -173,6 +175,7 @@ public ConfigurationBuilder(@NonNull Application app) {
buildConfigClass = annotationConfig.buildConfigClass();
applicationLogFile = annotationConfig.applicationLogFile();
applicationLogFileLines = annotationConfig.applicationLogFileLines();
applicationLogFileDir = annotationConfig.applicationLogFileDir();
reportDialogClass = annotationConfig.reportDialogClass();
reportPrimerClass = annotationConfig.reportPrimerClass();
httpMethod = annotationConfig.httpMethod();
Expand Down Expand Up @@ -722,6 +725,18 @@ public ConfigurationBuilder setApplicationLogFileLines(int applicationLogFileLin
return this;
}

/**
* @param directory The directory in which the application log file will be searched,
* to be used with {@link ReportField#APPLICATION_LOG} and
* {@link ReportsCrashes#applicationLogFile()}
* @return this instance
*/
@NonNull
public ConfigurationBuilder setApplicationLogFileDir(@NonNull Directory directory){
this.applicationLogFileDir = directory;
return this;
}

/**
* @param httpMethod The method to be used to send data to the server.
* @return this instance
Expand Down Expand Up @@ -1136,6 +1151,14 @@ int applicationLogFileLines() {
return DEFAULT_APPLICATION_LOGFILE_LINES;
}

@NonNull
Directory applicationLogFileDir() {
if(applicationLogFileDir != null){
return applicationLogFileDir;
}
return Directory.FILES_LEGACY;
}

@NonNull
Class<? extends BaseCrashReportDialog> reportDialogClass() {
if (reportDialogClass != null) {
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/acra/file/Directory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acra.file;

import android.content.Context;
import android.os.Environment;

/**
* @author F43nd1r
* @since 31.08.2016
*/
public enum Directory {
/**
* Legacy behaviour:
* If the string starts with a path separator, this behaves like {@link #ROOT}.
* Otherwise it behaves like {@link #FILES}.
*/
FILES_LEGACY,
/**
* Directory returned by {@link Context#getFilesDir()}
*/
FILES,
/**
* Directory returned by {@link Context#getExternalFilesDir(String)}
*/
EXTERNAL_FILES,
/**
* Directory returned by {@link Context#getCacheDir()}
*/
CACHE,
/**
* Directory returned by {@link Context#getExternalCacheDir()}
*/
EXTERNAL_CACHE,
/**
* Directory returned by {@link Context#getNoBackupFilesDir()}
* Will fall back to {@link Context#getFilesDir()} on API < 21
*/
NO_BACKUP_FILES,
/**
* Directory returned by {@link Environment#getExternalStorageDirectory()}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Add comment for permissions. Users must manage file/storage read permissions themselves.

*/
EXTERNAL_STORAGE,
/**
* Root Directory, paths in this directory are absolute paths
*/
ROOT
}