<commit_before>/*
 * Copyright 2015 <NAME> <<EMAIL>>
 *
 * 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 com.gmail.walles.johan.batterylogger;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;

import timber.log.Timber;

/**
 * Functionality for recording and accessing events logged by the app.
 */
public class LogCollector {
    /**
     * Make sure logs are being collected. Call periodically or at app /service startup.
     */
    public static void keepAlive(Context context) {
        if (isAlive(context)) {
            return;
        }

        // Kill any defunct log collecting processes before starting a new one
        kill(context);

        File logfile = getLogFile(context);
        try {
            Runtime.getRuntime().exec(createLogcatCommandLine(context));

            Timber.i("Background logcat started, logging into %s", logfile.getParent());
        } catch (IOException e) {
            Timber.e(e, "Executing logcat failed");
        }
    }

    private static Date getLatestLogMessageTimestamp(Context context) {
        long newest = Long.MIN_VALUE;

        File[] logFiles = getLogDir(context).listFiles();
        for (File logfile : logFiles) {
            if (!logfile.isFile()) {
                continue;
            }

            if (!logfile.getName().startsWith("log")) {
                continue;
            }

            newest = Math.max(newest, logfile.lastModified());
        }

        return new Date(newest);
    }

    static boolean isAlive(Context context) {
        long latestLogMessageAgeMs =
                System.currentTimeMillis() - getLatestLogMessageTimestamp(context).getTime();
        if (latestLogMessageAgeMs < 10000) {
            Timber.v("Log collector last collected %dms ago, assuming alive", latestLogMessageAgeMs);<commit_msg>Fix LogCollector.isAlive() false positive<commit_after>/*
 * Copyright 2015 <NAME> <<EMAIL>>
 *
 * 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 com.gmail.walles.johan.batterylogger;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;

import timber.log.Timber;

/**
 * Functionality for recording and accessing events logged by the app.
 */
public class LogCollector {
    /**
     * Make sure logs are being collected. Call periodically or at app /service startup.
     */
    public static void keepAlive(Context context) {
        if (isAlive(context)) {
            return;
        }

        // Kill any defunct log collecting processes before starting a new one
        kill(context);

        File logfile = getLogFile(context);
        try {
            Runtime.getRuntime().exec(createLogcatCommandLine(context));

            Timber.i("Background logcat started, logging into %s", logfile.getParent());
        } catch (IOException e) {
            Timber.e(e, "Executing logcat failed");
        }
    }

    private static Date getLatestLogMessageTimestamp(Context context) {
        // 0 == not found == at the epoch
        long newest = 0;

        File[] logFiles = getLogDir(context).listFiles();
        for (File logfile : logFiles) {
            if (!logfile.isFile()) {
                continue;
            }

            if (!logfile.getName().startsWith("log")) {
                continue;
            }

            newest = Math.max(newest, logfile.lastModified());
        }

        return new Date(newest);
    }

    static boolean isAlive(Context context) {
        long latestLogMessageAgeMs =
                System.currentTimeMillis() - getLatestLogMessageTimestamp(context).getTime();
        if (latestLogMessageAgeMs < -1000) {
            Timber.w("Log collector last collected %dms in the future", -latestLogMessageAgeMs);
        } else if (latestLogMessageAgeMs < 10000) {
            Timber.v("Log collector last collected %dms ago, assuming alive", latestLogMessageAgeMs);
{'fraction_non_alphanumeric': 0.09553131597466573, 'fraction_numerical': 0.005629838142153413, 'mean_word_length': 2.825706594885599, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2405251', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1508, 'n_words': 483}