mirror of
https://github.com/etesync/android
synced 2024-12-23 07:08:16 +00:00
Kotlin: more kotlin migration.
This commit is contained in:
parent
4d516c5fe1
commit
6ef78085bd
@ -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() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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.", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user