mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
refactor(logger): store priority char in ILogger.Priority enum
This commit is contained in:
@@ -58,7 +58,6 @@ public abstract class ILogger {
|
||||
private static final List<LogListener> logListeners = new ArrayList<>();
|
||||
private static final Map<String, ILogger> cachedLoggers = new WeakHashMap<>();
|
||||
|
||||
private static ILogger instance;
|
||||
protected final String TAG;
|
||||
|
||||
protected boolean isEnabled = true;
|
||||
@@ -91,23 +90,6 @@ public abstract class ILogger {
|
||||
return createInstance(tag);
|
||||
}
|
||||
|
||||
public static Priority priority(char priorityChar) {
|
||||
for (var priority : Priority.values()) {
|
||||
if (priorityChar(priority) == priorityChar) {
|
||||
return priority;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid priority character: " + priorityChar);
|
||||
}
|
||||
|
||||
public static char priorityChar(Priority priority) {
|
||||
return Character.toUpperCase(priorityText(priority).charAt(0));
|
||||
}
|
||||
|
||||
public static String priorityText(Priority priority) {
|
||||
return priority.name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log error messages.
|
||||
*
|
||||
@@ -255,7 +237,29 @@ public abstract class ILogger {
|
||||
* Logging priority.
|
||||
*/
|
||||
public enum Priority {
|
||||
DEBUG, WARNING, ERROR, INFO, VERBOSE
|
||||
|
||||
DEBUG('D'),
|
||||
WARNING('W'),
|
||||
ERROR('E'),
|
||||
INFO('I'),
|
||||
VERBOSE('V');
|
||||
|
||||
public final char priorityChar;
|
||||
|
||||
Priority(char priorityChar) {
|
||||
this.priorityChar = priorityChar;
|
||||
}
|
||||
|
||||
public static Priority forChar(char c) {
|
||||
c = Character.toUpperCase(c);
|
||||
for (Priority value : values()) {
|
||||
if (value.priorityChar == c) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Invalid priority char " + c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,7 +40,7 @@ public class JvmLogger extends ILogger {
|
||||
System.err.printf(
|
||||
"%-25s %-2s %s%n",
|
||||
trimTagIfNeeded(TAG, 25),
|
||||
ILogger.priorityChar(priority),
|
||||
priority.priorityChar,
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package com.itsaky.androidide.models
|
||||
import com.itsaky.androidide.utils.DefaultRecyclable
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
import com.itsaky.androidide.utils.LogTagUtils
|
||||
import com.itsaky.androidide.utils.RecyclableObjectPool
|
||||
import com.itsaky.androidide.utils.newRecyclableObjectPool
|
||||
|
||||
class LogLine private constructor() : DefaultRecyclable() {
|
||||
@@ -41,7 +40,7 @@ class LogLine private constructor() : DefaultRecyclable() {
|
||||
|
||||
fun toSimpleString(): String {
|
||||
return if (formatted) String.format(
|
||||
"%-25s %-2s %s", LogTagUtils.trimTagIfNeeded(tag, 25), ILogger.priorityChar(priority),
|
||||
"%-25s %-2s %s", LogTagUtils.trimTagIfNeeded(tag, 25), priority?.priorityChar ?: 'U',
|
||||
message) else unformatted!!
|
||||
}
|
||||
|
||||
@@ -70,7 +69,7 @@ class LogLine private constructor() : DefaultRecyclable() {
|
||||
override fun toString(): String {
|
||||
return if (formatted) String.format(
|
||||
"%s %s %s %s %-2s %-25s %s",
|
||||
date, time, pid, tid, ILogger.priorityChar(priority), LogTagUtils.trimTagIfNeeded(tag, 25),
|
||||
date, time, pid, tid, priority?.priorityChar ?: 'U', LogTagUtils.trimTagIfNeeded(tag, 25),
|
||||
message) else unformatted!!
|
||||
}
|
||||
|
||||
@@ -133,7 +132,7 @@ class LogLine private constructor() : DefaultRecyclable() {
|
||||
val logLine = logLinePool.obtain()
|
||||
try {
|
||||
val split = log.split("\\s".toRegex(), limit = 7).toTypedArray()
|
||||
logLine.priority = ILogger.priority(split[4][0].uppercaseChar())
|
||||
logLine.priority = ILogger.Priority.forChar(split[4][0])
|
||||
logLine.date = split[0]
|
||||
logLine.time = split[1] // time
|
||||
logLine.pid = split[2] // process id
|
||||
|
||||
Reference in New Issue
Block a user