Created
October 26, 2018 08:03
-
-
Save hwd6190128/30c2527a75ce889e7157a39dbe3ddda0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Howard Chang on 2018/09/13. | |
*/ | |
class HttpLogger : HttpLoggingInterceptor.Logger { | |
private var sBuilder = StringBuilder() | |
override fun log(message: String) { | |
var mMessage: String = message | |
if (mMessage.startsWith("--> POST")) { | |
sBuilder.setLength(0) | |
} | |
// detect json patten by {} or [], parser json | |
if ((mMessage.startsWith("{") && mMessage.endsWith("}")) | |
|| (mMessage.startsWith("[") && mMessage.endsWith("]"))) { | |
mMessage = formatJson(decodeUnicode(mMessage)) | |
} | |
sBuilder.append("$mMessage\n") | |
// end to print | |
if (mMessage.startsWith("<-- END HTTP")) { | |
Logger.d(sBuilder.toString()) | |
sBuilder.setLength(0) | |
} | |
} | |
/** | |
* parser json string | |
* | |
* @param jsonStr target | |
* @return result | |
*/ | |
private fun formatJson(jsonStr: String): String { | |
if (jsonStr.isBlank()) return "" | |
val sb = StringBuilder() | |
var last: Char | |
var current = '\u0000' | |
var indent = 0 | |
for (i: Int in 0 until jsonStr.length) { | |
last = current | |
current = jsonStr[i] | |
when (current) { | |
'{', '[' -> { | |
// line break if { or [, newline Indent | |
sb.append(current) | |
sb.append('\n') | |
indent++ | |
addIndentBlank(sb, indent) | |
} | |
'}', ']' -> { | |
// line break if { or [, current Indent | |
sb.append('\n') | |
indent-- | |
addIndentBlank(sb, indent) | |
sb.append(current) | |
} | |
',' -> { | |
// line break if , | |
if (last != '\\') { | |
sb.append('\n') | |
addIndentBlank(sb, indent) | |
} | |
} | |
else -> { | |
sb.append(current) | |
} | |
} | |
} | |
return sb.toString() | |
} | |
/** | |
* add space | |
*/ | |
private fun addIndentBlank(sb: StringBuilder, indent: Int) { | |
for (i: Int in 0 until indent) { | |
sb.append('\t') | |
} | |
} | |
private fun decodeUnicode(theString: String): String { | |
var aChar: Char | |
val len: Int = theString.length | |
val outBuffer = StringBuffer(len) | |
var x = 0 | |
while (x < len) { | |
aChar = theString[x++] | |
if (aChar == '\\') { | |
aChar = theString[x++] | |
if (aChar == 'u') { | |
var value = 0 | |
for (i: Int in 0..3) char@ { | |
aChar = theString[x++] | |
try { | |
value = when (aChar) { | |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> (value shl 4) + aChar.toInt() - '0'.toInt() | |
'a', 'b', 'c', 'd', 'e', 'f' -> (value shl 4) + 10 + aChar.toInt() - 'a'.toInt() | |
'A', 'B', 'C', 'D', 'E', 'F' -> (value shl 4) + 10 + aChar.toInt() - 'A'.toInt() | |
else -> throw IllegalArgumentException( | |
"Malformed \\uxxxx encoding.") | |
} | |
} catch (e: IllegalArgumentException) { | |
e.printStackTrace() | |
return@char | |
} | |
} | |
outBuffer.append(value.toChar()) | |
} else { | |
when (aChar) { | |
't' -> aChar = '\t' | |
'r' -> aChar = '\r' | |
'n' -> aChar = '\n' | |
'f' -> aChar = '\u000C' | |
} | |
outBuffer.append(aChar) | |
} | |
} else | |
outBuffer.append(aChar) | |
} | |
return outBuffer.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment