Skip to content

Commit

Permalink
feat: 命令运行时添加错误日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiaoyong committed Jun 28, 2024
1 parent 8fdaf61 commit 02a12fa
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main/kotlin/io/github/jixiaoyong/utils/RunCommandUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object RunCommandUtil {
return try {
val process = Runtime.getRuntime().exec(command)
val logBuffer = StringBuffer()
val errBuffer = StringBuffer()

if (printLog) {
BufferedReader(InputStreamReader(process.inputStream)).useLines {
Expand All @@ -37,11 +38,20 @@ object RunCommandUtil {
}
}

if (printError) {
BufferedReader(InputStreamReader(process.errorStream)).useLines {
it.forEach { line ->
Logger.error("${logTag}$line")
errBuffer.append(line).append("\n")
}
}
}

val result = process.waitFor()
if (result == 0) {
null
} else {
Exception("${logTag}exit code: $result\n${logBuffer}")
Exception("${logTag}exit code: $result\n${logBuffer}\nerr:${errBuffer}")
}

} catch (e: Exception) {
Expand All @@ -63,6 +73,7 @@ object RunCommandUtil {
return try {
val process = Runtime.getRuntime().exec(command)
val logBuffer = StringBuffer()
val errBuffer = StringBuffer()

if (printLog) {
BufferedReader(InputStreamReader(process.inputStream)).useLines {
Expand All @@ -73,11 +84,20 @@ object RunCommandUtil {
}
}

if (printError) {
BufferedReader(InputStreamReader(process.errorStream)).useLines {
it.forEach { line ->
Logger.error("${logTag}$line")
errBuffer.append(line).append("\n")
}
}
}

val result = process.waitFor()
if (result == 0) {
CommandResult.Success(logBuffer.toString())
} else {
CommandResult.Error("${logTag}exit code: $result\n${logBuffer}")
CommandResult.Error("${logTag}exit code: $result\n${logBuffer}\nerr:${errBuffer}")
}

} catch (e: Exception) {
Expand Down

0 comments on commit 02a12fa

Please sign in to comment.