1
0
mirror of https://github.com/etesync/android synced 2025-02-02 10:51:10 +00:00

Import latest logger code from davdroid.

This commit is contained in:
Tom Hacohen 2019-03-14 18:38:21 +00:00
parent b382b8515c
commit a7aed85a4c
4 changed files with 43 additions and 50 deletions

View File

@ -161,7 +161,7 @@ class App : Application() {
rootLogger.useParentHandlers = false rootLogger.useParentHandlers = false
for (handler in rootLogger.handlers) for (handler in rootLogger.handlers)
rootLogger.removeHandler(handler) rootLogger.removeHandler(handler)
rootLogger.addHandler(LogcatHandler.INSTANCE) rootLogger.addHandler(LogcatHandler)
val nm = NotificationManagerCompat.from(this) val nm = NotificationManagerCompat.from(this)
// log to external file according to preferences // log to external file according to preferences

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2013 2016 Ricki Hirner (bitfire web engineering). * Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0 * are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -16,7 +16,9 @@ import java.util.logging.Handler
import java.util.logging.Level import java.util.logging.Level
import java.util.logging.LogRecord import java.util.logging.LogRecord
class LogcatHandler private constructor() : Handler() { object LogcatHandler: Handler() {
private const val MAX_LINE_LENGTH = 3000
init { init {
formatter = PlainTextFormatter.LOGCAT formatter = PlainTextFormatter.LOGCAT
@ -31,28 +33,18 @@ class LogcatHandler private constructor() : Handler() {
var pos = 0 var pos = 0
while (pos < end) { while (pos < end) {
val line = text.substring(pos, NumberUtils.min(pos + MAX_LINE_LENGTH, end)) val line = text.substring(pos, NumberUtils.min(pos + MAX_LINE_LENGTH, end))
when {
if (level >= Level.SEVERE.intValue()) level >= Level.SEVERE.intValue() -> Log.e(r.loggerName, line)
Log.e(r.loggerName, line) level >= Level.WARNING.intValue() -> Log.w(r.loggerName, line)
else if (level >= Level.WARNING.intValue()) level >= Level.CONFIG.intValue() -> Log.i(r.loggerName, line)
Log.w(r.loggerName, line) level >= Level.FINER.intValue() -> Log.d(r.loggerName, line)
else if (level >= Level.CONFIG.intValue()) else -> Log.v(r.loggerName, line)
Log.i(r.loggerName, line) }
else if (level >= Level.FINER.intValue())
Log.d(r.loggerName, line)
else
Log.v(r.loggerName, line)
pos += MAX_LINE_LENGTH pos += MAX_LINE_LENGTH
} }
} }
override fun flush() {} override fun flush() {}
override fun close() {} override fun close() {}
companion object {
private val MAX_LINE_LENGTH = 3000
val INSTANCE = LogcatHandler()
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2013 2016 Ricki Hirner (bitfire web engineering). * Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0 * are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -11,32 +11,41 @@ package com.etesync.syncadapter.log
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.exception.ExceptionUtils import org.apache.commons.lang3.exception.ExceptionUtils
import org.apache.commons.lang3.time.DateFormatUtils import org.apache.commons.lang3.time.DateFormatUtils
import java.util.logging.Formatter import java.util.logging.Formatter
import java.util.logging.LogRecord import java.util.logging.LogRecord
class PlainTextFormatter private constructor(private val logcat: Boolean) : Formatter() { class PlainTextFormatter private constructor(
private val logcat: Boolean
): Formatter() {
companion object {
val LOGCAT = PlainTextFormatter(true)
val DEFAULT = PlainTextFormatter(false)
const val MAX_MESSAGE_LENGTH = 20000
}
override fun format(r: LogRecord): String { override fun format(r: LogRecord): String {
val builder = StringBuilder() val builder = StringBuilder()
if (!logcat) if (!logcat)
builder.append(DateFormatUtils.format(r.millis, "yyyy-MM-dd HH:mm:ss")) builder .append(DateFormatUtils.format(r.millis, "yyyy-MM-dd HH:mm:ss"))
.append(" ").append(r.threadID).append(" ") .append(" ").append(r.threadID).append(" ")
if (r.sourceClassName.replaceFirst("\\$.*".toRegex(), "") != r.loggerName) val className = shortClassName(r.sourceClassName)
builder.append("[").append(shortClassName(r.sourceClassName)).append("] ") if (className != r.loggerName)
builder.append("[").append(className).append("] ")
builder.append(r.message) builder.append(StringUtils.abbreviate(r.message, MAX_MESSAGE_LENGTH))
if (r.thrown != null) r.thrown?.let {
builder.append("\nEXCEPTION ") builder .append("\nEXCEPTION ")
.append(ExceptionUtils.getStackTrace(r.thrown)) .append(ExceptionUtils.getStackTrace(it))
}
if (r.parameters != null) { r.parameters?.let {
var idx = 1 for ((idx, param) in it.withIndex())
for (param in r.parameters) builder.append("\n\tPARAMETER #").append(idx).append(" = ").append(param)
builder.append("\n\tPARAMETER #").append(idx++).append(" = ").append(param)
} }
if (!logcat) if (!logcat)
@ -45,14 +54,8 @@ class PlainTextFormatter private constructor(private val logcat: Boolean) : Form
return builder.toString() return builder.toString()
} }
private fun shortClassName(className: String): String? { private fun shortClassName(className: String) = className
val s = StringUtils.replace(className, "com.etesync.syncadapter.", "") .replace(Regex("^at\\.bitfire\\.(dav|cert4an|dav4an|ical4an|vcard4an)droid\\."), "")
return StringUtils.replace(s, "at.bitfire.", "") .replace(Regex("\\$.*$"), "")
}
companion object {
val LOGCAT = PlainTextFormatter(true)
val DEFAULT = PlainTextFormatter(false)
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2013 2016 Ricki Hirner (bitfire web engineering). * Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0 * are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -11,9 +11,9 @@ package com.etesync.syncadapter.log
import java.util.logging.Handler import java.util.logging.Handler
import java.util.logging.LogRecord import java.util.logging.LogRecord
class StringHandler : Handler() { class StringHandler: Handler() {
internal var builder = StringBuilder() val builder = StringBuilder()
init { init {
formatter = PlainTextFormatter.DEFAULT formatter = PlainTextFormatter.DEFAULT
@ -24,10 +24,8 @@ class StringHandler : Handler() {
} }
override fun flush() {} override fun flush() {}
override fun close() {} override fun close() {}
override fun toString(): String { override fun toString() = builder.toString()
return builder.toString()
}
} }