-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buffer logging in memory to send it out with a feedback message.
- Loading branch information
Showing
5 changed files
with
86 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
app/src/main/java/com/pr0gramm/app/util/CrashlyticsLogHandler.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |