Skip to content

Commit

Permalink
Buffer logging in memory to send it out with a feedback message.
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsalarm committed Jul 17, 2017
1 parent 743c106 commit 5703d0a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public List<String> getQuestionableTags() {
return Arrays.asList(
"0815", "kann weg", "heil hitler", "ban pls", "deshalb",
"ab ins gas", "und weiter", "alles ist", "hure", "da drückste",
"pr0paganda", "pr0gida", "für mehr");
"pr0paganda", "pr0gida", "für mehr", "dein scheiß", "kann ich auch");
}

public enum AdType {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/pr0gramm/app/ApplicationClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.pr0gramm.app.sync.SyncJob
import com.pr0gramm.app.ui.ActivityErrorHandler
import com.pr0gramm.app.ui.dialogs.ErrorDialogFragment.Companion.globalErrorDialogHandler
import com.pr0gramm.app.util.AndroidUtility.buildVersionCode
import com.pr0gramm.app.util.CrashlyticsLogHandler
import com.pr0gramm.app.util.LogHandler
import com.thefinestartist.Base
import io.fabric.sdk.android.Fabric
import net.danlew.android.joda.JodaTimeAndroid
Expand Down Expand Up @@ -63,13 +63,13 @@ open class ApplicationClass : Application(), KodeinAware {

logger.info("Initialize fabric")
Fabric.with(this, Crashlytics())

LoggerConfiguration.configuration()
.removeRootLogcatHandler()
.setRootLogLevel(LogLevel.INFO)
.addHandlerToRootLogger(CrashlyticsLogHandler())
}

LoggerConfiguration.configuration()
.removeRootLogcatHandler()
.setRootLogLevel(LogLevel.INFO)
.addHandlerToRootLogger(LogHandler())

// initialize this to show errors always in the context of the current activity.
globalErrorDialogHandler = ActivityErrorHandler(this)

Expand Down
20 changes: 5 additions & 15 deletions app/src/main/java/com/pr0gramm/app/services/FeedbackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import android.util.Base64
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil
import com.google.common.base.Charsets
import com.google.common.base.Throwables
import com.google.common.io.CharStreams
import com.pr0gramm.app.BuildConfig
import com.pr0gramm.app.Nothing
import com.pr0gramm.app.Settings
import com.pr0gramm.app.util.AndroidUtility
import com.pr0gramm.app.util.LogHandler
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import retrofit2.Retrofit
Expand All @@ -22,7 +22,6 @@ import rx.Completable
import rx.Observable
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.lang.reflect.Modifier
import java.util.zip.DeflaterOutputStream
Expand Down Expand Up @@ -79,7 +78,7 @@ class FeedbackService(okHttpClient: OkHttpClient) {
add("memory info", this::appendMemoryInfo)
add("codec info", this::appendCodecInfo)
add("preferences", this::appendPreferences)
add("logcat", this::appendLogcat)
add("log", this::appendLogMessages)

// convert result to a string
return result.toString()
Expand Down Expand Up @@ -113,18 +112,9 @@ class FeedbackService(okHttpClient: OkHttpClient) {


@Throws(IOException::class)
private fun appendLogcat(result: StringBuilder) {
val process = Runtime.getRuntime().exec("logcat -d -v threadtime")
try {
CharStreams.asWriter(result).use { writer ->
val reader = InputStreamReader(process.inputStream, Charsets.UTF_8)
CharStreams.copy(reader, writer)
}
} finally {
try {
process.destroy()
} catch (ignored: Exception) {
}
private fun appendLogMessages(result: StringBuilder) {
LogHandler.recentMessages().forEach { message ->
result.append(message).append('\n')
}
}

Expand Down
28 changes: 0 additions & 28 deletions app/src/main/java/com/pr0gramm/app/util/CrashlyticsLogHandler.kt

This file was deleted.

74 changes: 74 additions & 0 deletions app/src/main/java/com/pr0gramm/app/util/LogHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.pr0gramm.app.util

import android.support.v4.util.CircularArray
import com.crashlytics.android.Crashlytics
import com.pr0gramm.app.BuildConfig
import pl.brightinventions.slf4android.MessageValueSupplier
import java.util.logging.Handler
import java.util.logging.Level

/**
*/
class LogHandler : Handler() {
private val messageValueSupplier = MessageValueSupplier()

private val crashlytics by lazy(LazyThreadSafetyMode.NONE) {
Crashlytics.getInstance().core
}

override fun publish(record: java.util.logging.LogRecord) {
if (record.level.intValue() <= Level.INFO.intValue()) {
val messageBuilder = StringBuilder()
val logRecord = pl.brightinventions.slf4android.LogRecord.fromRecord(record)
messageValueSupplier.append(logRecord, messageBuilder)

val formatted = messageBuilder.toString()

val tag = record.loggerName
val androidLogLevel = logRecord.logLevel.androidLevel

if (!BuildConfig.DEBUG) {
crashlytics.log(androidLogLevel, tag, formatted)
}

synchronized(BUFFER) {
// remove the oldest message if we've reached the limit
if (BUFFER.size() == MESSAGE_LIMIT) {
BUFFER.popLast()
}

// and add the new message to the buffer
BUFFER.addFirst(LogEntry(record.millis, record.level, tag, formatted))
}
}
}

override fun close() {}

override fun flush() {}

private data class LogEntry(val time: Long, val level: Level, val tag: String, val message: String)

companion object {
const val MESSAGE_LIMIT = 4096

private val BUFFER = CircularArray<LogEntry>(MESSAGE_LIMIT)

/**
* Returns a list of the recent messages
*/
fun recentMessages(): List<String> {
val entries = synchronized(BUFFER) {
(0..BUFFER.size() - 1).map { BUFFER.get(it) }
}

// get the most recent entry. should be the last one
val mostRecentEntry = entries.maxBy { it.time } ?: return emptyList()

// format each entry relative to the newest one
return entries.sortedBy { it.time }.map { (time, level, tag, message) ->
"%4.2fs [%5s] %s: %s".format(0.001f * (time - mostRecentEntry.time), level.name, tag, message)
}
}
}
}

0 comments on commit 5703d0a

Please sign in to comment.