Skip to content

Commit

Permalink
feat(std-wrapper): 使std wrapper显示调用处的类
Browse files Browse the repository at this point in the history
  • Loading branch information
XYZboom committed Feb 28, 2024
1 parent de9517a commit dcdd253
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/main/kotlin/com/github/tnoalex/utils/StdOutErrWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tnoalex.utils

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.PrintStream

Expand All @@ -11,87 +12,95 @@ object StdOutErrWrapper {
private val logger = LoggerFactory.getLogger(StdOutErrWrapper::class.java)

fun init() {
logger.warn("All stdOut and StdErr will be redirect to hear")
logger.warn("All stdOut and StdErr will be redirect to here")
val printStreamForOut = createLoggingWrapper(System.out, false)
val printStreamForErr = createLoggingWrapper(System.out, true)
System.setOut(printStreamForOut)
System.setErr(printStreamForErr)
}

private fun getCallerLogger(): Logger {
val stackTrace = Thread.currentThread().stackTrace
if (stackTrace.size < 6) {
return logger
}
return LoggerFactory.getLogger(stackTrace[5].className)
}

private fun createLoggingWrapper(
printStream: PrintStream,
isErr: Boolean
): PrintStream {
return object : PrintStream(printStream) {
override fun print(string: String?) {
if (!isErr) {
logger.info(string)
getCallerLogger().info(string)
} else {
logger.error(string)
getCallerLogger().error(string)
}
}

override fun print(b: Boolean) {
if (!isErr) {
logger.info(b.toString())
getCallerLogger().info(b.toString())
} else {
logger.error(b.toString())
getCallerLogger().error(b.toString())
}
}

override fun print(c: Char) {
if (!isErr) {
logger.info(c.toString())
getCallerLogger().info(c.toString())
} else {
logger.error(c.toString())
getCallerLogger().error(c.toString())
}
}

override fun print(i: Int) {
if (!isErr) {
logger.info(i.toString())
getCallerLogger().info(i.toString())
} else {
logger.error(i.toString())
getCallerLogger().error(i.toString())
}
}

override fun print(l: Long) {
if (!isErr) {
logger.info(l.toString())
getCallerLogger().info(l.toString())
} else {
logger.error(l.toString())
getCallerLogger().error(l.toString())
}
}

override fun print(f: Float) {
if (!isErr) {
logger.info(f.toString())
getCallerLogger().info(f.toString())
} else {
logger.error(f.toString())
getCallerLogger().error(f.toString())
}
}

override fun print(d: Double) {
if (!isErr) {
logger.info(d.toString())
getCallerLogger().info(d.toString())
} else {
logger.error(d.toString())
getCallerLogger().error(d.toString())
}
}

override fun print(x: CharArray) {
if (!isErr) {
logger.debug(String(x))
getCallerLogger().debug(String(x))
} else {
logger.error(String(x))
getCallerLogger().error(String(x))
}
}

override fun print(obj: Any?) {
if (!isErr) {
logger.info(obj?.toString())
getCallerLogger().info(obj?.toString())
} else {
logger.error(obj?.toString())
getCallerLogger().error(obj?.toString())
}
}
}
Expand Down

0 comments on commit dcdd253

Please sign in to comment.