/*
 *  Copyright 2012 Kevin Gaudin
 *
 *  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.collector;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.acra.ACRA;
import org.acra.util.BoundedLinkedList;

import android.app.Application;
import android.content.Context;

import static org.acra.ACRA.LOG_TAG;

/**
 * Collects the N last lines of a text stream. Use this collector if your
 * application handles its own logging system.
 * 
 * @author Kevin Gaudin
 * 
 */
class LogFileCollector {

    /**
     * Private constructor to prevent instantiation.
     */
    private LogFileCollector() {
    };

    /**
     * Reads the last lines of a custom log file. The file name is assumed as
     * located in the {@link Application#getFilesDir()} directory if it does not
     * contain any path separator.
     * 
     * @param context
     * @param fileName
     * @param numberOfLines
     * @return
     * @throws IOException
     */
    public static String collectLogFile(Context context, String fileName, int numberOfLines) throws IOException {
        final BoundedLinkedList<String> resultBuffer = new BoundedLinkedList<String>(numberOfLines);
        final BufferedReader reader = getReader(context, fileName);
        try {
            String line = reader.readLine();
            while (line != null) {
                resultBuffer.add(line + "\n");
                line = reader.readLine();
            }
        } finally {
            CollectorUtil.safeClose(reader);
        }
        return resultBuffer.toString();
    }

    private static BufferedReader getReader(Context context, String fileName) {
        try {
            if (fileName.contains("/")) {
                return new BufferedReader(new InputStreamReader(new FileInputStream(fileName)), 1024);
            } else {
                return new BufferedReader(new InputStreamReader(context.openFileInput(fileName)), 1024);
            }
        } catch (FileNotFoundException e) {
            ACRA.log.e(LOG_TAG, "Cannot find application log file : '" + ACRA.getConfig().applicationLogFile() + "'");
            return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[0])));
        }
    }
}

@@ -2390,32 +2390,74 @@
 %0A        try %7B%0D%0A
+            FileInputStream inputStream;%0D%0A
             if (
@@ -2469,16 +2469,18 @@
 ame.
-contains
+startsWith
 (%22/%22
@@ -2505,39 +2505,121 @@
     
-return new BufferedReader(new I
+inputStream = new FileInputStream(fileName);%0D%0A            %7D else if (fileName.contains(%22/%22)) %7B%0D%0A                i
 nput
@@ -2620,31 +2620,27 @@
  inputStream
-Reader(
+ = 
 new FileInpu
@@ -2651,169 +2651,242 @@
 eam(
-fileName)), 1024);%0D%0A            %7D else %7B%0D%0A                return new BufferedReader(new InputStreamReader(context.openFileInput(fileName)), 1024);%0D%0A            %7D
+context.getFilesDir() + File.separator + fileName);%0D%0A            %7D else %7B%0D%0A                inputStream = context.openFileInput(fileName);%0D%0A            %7D%0D%0A            return new BufferedReader(new InputStreamReader(inputStream), 1024);
 %0D%0A  

Fix LogFileCollector to work with getFilesDir() sub-directories