diff --git a/app/src/main/java/com/etesync/syncadapter/App.kt b/app/src/main/java/com/etesync/syncadapter/App.kt index 24ae46bc..a972cec5 100644 --- a/app/src/main/java/com/etesync/syncadapter/App.kt +++ b/app/src/main/java/com/etesync/syncadapter/App.kt @@ -161,7 +161,7 @@ class App : Application() { rootLogger.useParentHandlers = false for (handler in rootLogger.handlers) rootLogger.removeHandler(handler) - rootLogger.addHandler(LogcatHandler.INSTANCE) + rootLogger.addHandler(LogcatHandler) val nm = NotificationManagerCompat.from(this) // log to external file according to preferences diff --git a/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt b/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt index acfe08ca..a83f5040 100644 --- a/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt +++ b/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt @@ -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 * are made available under the terms of the GNU Public License v3.0 * 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.LogRecord -class LogcatHandler private constructor() : Handler() { +object LogcatHandler: Handler() { + + private const val MAX_LINE_LENGTH = 3000 init { formatter = PlainTextFormatter.LOGCAT @@ -31,28 +33,18 @@ class LogcatHandler private constructor() : Handler() { var pos = 0 while (pos < end) { val line = text.substring(pos, NumberUtils.min(pos + MAX_LINE_LENGTH, end)) - - if (level >= Level.SEVERE.intValue()) - Log.e(r.loggerName, line) - else if (level >= Level.WARNING.intValue()) - Log.w(r.loggerName, line) - else if (level >= Level.CONFIG.intValue()) - Log.i(r.loggerName, line) - else if (level >= Level.FINER.intValue()) - Log.d(r.loggerName, line) - else - Log.v(r.loggerName, line) + when { + level >= Level.SEVERE.intValue() -> Log.e(r.loggerName, line) + level >= Level.WARNING.intValue() -> Log.w(r.loggerName, line) + level >= Level.CONFIG.intValue() -> Log.i(r.loggerName, line) + level >= Level.FINER.intValue() -> Log.d(r.loggerName, line) + else -> Log.v(r.loggerName, line) + } pos += MAX_LINE_LENGTH } } override fun flush() {} - override fun close() {} - companion object { - private val MAX_LINE_LENGTH = 3000 - val INSTANCE = LogcatHandler() - } - } diff --git a/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt b/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt index adea8044..d077759b 100644 --- a/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt +++ b/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt @@ -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 * are made available under the terms of the GNU Public License v3.0 * 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.exception.ExceptionUtils import org.apache.commons.lang3.time.DateFormatUtils - import java.util.logging.Formatter 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 { val builder = StringBuilder() 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(" ") - if (r.sourceClassName.replaceFirst("\\$.*".toRegex(), "") != r.loggerName) - builder.append("[").append(shortClassName(r.sourceClassName)).append("] ") + val className = shortClassName(r.sourceClassName) + 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) - builder.append("\nEXCEPTION ") - .append(ExceptionUtils.getStackTrace(r.thrown)) + r.thrown?.let { + builder .append("\nEXCEPTION ") + .append(ExceptionUtils.getStackTrace(it)) + } - if (r.parameters != null) { - var idx = 1 - for (param in r.parameters) - builder.append("\n\tPARAMETER #").append(idx++).append(" = ").append(param) + r.parameters?.let { + for ((idx, param) in it.withIndex()) + builder.append("\n\tPARAMETER #").append(idx).append(" = ").append(param) } if (!logcat) @@ -45,14 +54,8 @@ class PlainTextFormatter private constructor(private val logcat: Boolean) : Form return builder.toString() } - private fun shortClassName(className: String): String? { - val s = StringUtils.replace(className, "com.etesync.syncadapter.", "") - return StringUtils.replace(s, "at.bitfire.", "") - } - - companion object { - val LOGCAT = PlainTextFormatter(true) - val DEFAULT = PlainTextFormatter(false) - } + private fun shortClassName(className: String) = className + .replace(Regex("^at\\.bitfire\\.(dav|cert4an|dav4an|ical4an|vcard4an)droid\\."), "") + .replace(Regex("\\$.*$"), "") } diff --git a/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt b/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt index 95b393c6..35e228f3 100644 --- a/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt +++ b/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt @@ -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 * are made available under the terms of the GNU Public License v3.0 * 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.LogRecord -class StringHandler : Handler() { +class StringHandler: Handler() { - internal var builder = StringBuilder() + val builder = StringBuilder() init { formatter = PlainTextFormatter.DEFAULT @@ -24,10 +24,8 @@ class StringHandler : Handler() { } override fun flush() {} - override fun close() {} - override fun toString(): String { - return builder.toString() - } + override fun toString() = builder.toString() + }