From 6ef78085bd1f1fb1e72abf8280b4888a34866d81 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sat, 5 Jan 2019 11:45:17 +0000 Subject: [PATCH] Kotlin: more kotlin migration. --- .../syncadapter/log/LogcatHandler.java | 59 ----------------- .../etesync/syncadapter/log/LogcatHandler.kt | 58 +++++++++++++++++ .../syncadapter/log/PlainTextFormatter.java | 64 ------------------- .../syncadapter/log/PlainTextFormatter.kt | 58 +++++++++++++++++ .../syncadapter/log/StringHandler.java | 40 ------------ .../etesync/syncadapter/log/StringHandler.kt | 33 ++++++++++ 6 files changed, 149 insertions(+), 163 deletions(-) delete mode 100644 app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.java create mode 100644 app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt delete mode 100644 app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.java create mode 100644 app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt delete mode 100644 app/src/main/java/com/etesync/syncadapter/log/StringHandler.java create mode 100644 app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt diff --git a/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.java b/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.java deleted file mode 100644 index 5f682a6c..00000000 --- a/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright © 2013 – 2016 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 - * http://www.gnu.org/licenses/gpl.html - */ - -package com.etesync.syncadapter.log; - -import android.util.Log; - -import org.apache.commons.lang3.math.NumberUtils; - -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.LogRecord; - -public class LogcatHandler extends Handler { - private static final int MAX_LINE_LENGTH = 3000; - public static final LogcatHandler INSTANCE = new LogcatHandler(); - - private LogcatHandler() { - super(); - setFormatter(PlainTextFormatter.LOGCAT); - setLevel(Level.ALL); - } - - @Override - public void publish(LogRecord r) { - String text = getFormatter().format(r); - int level = r.getLevel().intValue(); - - int end = text.length(); - for (int pos = 0; pos < end; pos += MAX_LINE_LENGTH) { - String line = text.substring(pos, NumberUtils.min(pos + MAX_LINE_LENGTH, end)); - - if (level >= Level.SEVERE.intValue()) - Log.e(r.getLoggerName(), line); - else if (level >= Level.WARNING.intValue()) - Log.w(r.getLoggerName(), line); - else if (level >= Level.CONFIG.intValue()) - Log.i(r.getLoggerName(), line); - else if (level >= Level.FINER.intValue()) - Log.d(r.getLoggerName(), line); - else - Log.v(r.getLoggerName(), line); - } - } - - @Override - public void flush() { - } - - @Override - public void close() { - } - -} diff --git a/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt b/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt new file mode 100644 index 00000000..acfe08ca --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/log/LogcatHandler.kt @@ -0,0 +1,58 @@ +/* + * Copyright © 2013 – 2016 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 + * http://www.gnu.org/licenses/gpl.html + */ + +package com.etesync.syncadapter.log + +import android.util.Log + +import org.apache.commons.lang3.math.NumberUtils + +import java.util.logging.Handler +import java.util.logging.Level +import java.util.logging.LogRecord + +class LogcatHandler private constructor() : Handler() { + + init { + formatter = PlainTextFormatter.LOGCAT + level = Level.ALL + } + + override fun publish(r: LogRecord) { + val text = formatter.format(r) + val level = r.level.intValue() + + val end = text.length + 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) + 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.java b/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.java deleted file mode 100644 index 5b7def8a..00000000 --- a/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright © 2013 – 2016 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 - * http://www.gnu.org/licenses/gpl.html - */ - -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; - -public class PlainTextFormatter extends Formatter { - public final static PlainTextFormatter - LOGCAT = new PlainTextFormatter(true), - DEFAULT = new PlainTextFormatter(false); - - private final boolean logcat; - - private PlainTextFormatter(boolean onLogcat) { - this.logcat = onLogcat; - } - - @Override - @SuppressWarnings("ThrowableResultOfMethodCallIgnored") - public String format(LogRecord r) { - StringBuilder builder = new StringBuilder(); - - if (!logcat) - builder .append(DateFormatUtils.format(r.getMillis(), "yyyy-MM-dd HH:mm:ss")) - .append(" ").append(r.getThreadID()).append(" "); - - if (!r.getSourceClassName().replaceFirst("\\$.*", "").equals(r.getLoggerName())) - builder.append("[").append(shortClassName(r.getSourceClassName())).append("] "); - - builder.append(r.getMessage()); - - if (r.getThrown() != null) - builder .append("\nEXCEPTION ") - .append(ExceptionUtils.getStackTrace(r.getThrown())); - - if (r.getParameters() != null) { - int idx = 1; - for (Object param : r.getParameters()) - builder.append("\n\tPARAMETER #").append(idx++).append(" = ").append(param); - } - - if (!logcat) - builder.append("\n"); - - return builder.toString(); - } - - private String shortClassName(String className) { - String s = StringUtils.replace(className, "com.etesync.syncadapter.", ""); - return StringUtils.replace(s, "at.bitfire.", ""); - } - -} diff --git a/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt b/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt new file mode 100644 index 00000000..adea8044 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/log/PlainTextFormatter.kt @@ -0,0 +1,58 @@ +/* + * Copyright © 2013 – 2016 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 + * http://www.gnu.org/licenses/gpl.html + */ + +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() { + + override fun format(r: LogRecord): String { + val builder = StringBuilder() + + if (!logcat) + 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("] ") + + builder.append(r.message) + + if (r.thrown != null) + builder.append("\nEXCEPTION ") + .append(ExceptionUtils.getStackTrace(r.thrown)) + + if (r.parameters != null) { + var idx = 1 + for (param in r.parameters) + builder.append("\n\tPARAMETER #").append(idx++).append(" = ").append(param) + } + + if (!logcat) + builder.append("\n") + + 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) + } + +} diff --git a/app/src/main/java/com/etesync/syncadapter/log/StringHandler.java b/app/src/main/java/com/etesync/syncadapter/log/StringHandler.java deleted file mode 100644 index 839d599b..00000000 --- a/app/src/main/java/com/etesync/syncadapter/log/StringHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright © 2013 – 2016 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 - * http://www.gnu.org/licenses/gpl.html - */ - -package com.etesync.syncadapter.log; - -import java.util.logging.Handler; -import java.util.logging.LogRecord; - -public class StringHandler extends Handler { - - StringBuilder builder = new StringBuilder(); - - public StringHandler() { - super(); - setFormatter(PlainTextFormatter.DEFAULT); - } - - @Override - public void publish(LogRecord record) { - builder.append(getFormatter().format(record)); - } - - @Override - public void flush() { - } - - @Override - public void close() { - } - - @Override - public String toString() { - return builder.toString(); - } -} diff --git a/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt b/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt new file mode 100644 index 00000000..95b393c6 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/log/StringHandler.kt @@ -0,0 +1,33 @@ +/* + * Copyright © 2013 – 2016 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 + * http://www.gnu.org/licenses/gpl.html + */ + +package com.etesync.syncadapter.log + +import java.util.logging.Handler +import java.util.logging.LogRecord + +class StringHandler : Handler() { + + internal var builder = StringBuilder() + + init { + formatter = PlainTextFormatter.DEFAULT + } + + override fun publish(record: LogRecord) { + builder.append(formatter.format(record)) + } + + override fun flush() {} + + override fun close() {} + + override fun toString(): String { + return builder.toString() + } +}