评论

收藏

[Unix] 工具类之LogUtils,Android开发揭秘

服务系统 服务系统 发布于:2021-12-26 11:09 | 阅读数:322 | 评论:0

case ASSERT:
printDefault(logType, tag, headString + msg);
break;
case JSON:
printJson(tag, msg, headString);
break;
case XML:
printXml(tag, msg, headString);
break;
default:
break;
}
}
}
private static void printDefault(int type, String tag, String msg) {
if (TextUtils.isEmpty(tag)) {
tag = TAG;
}
int index = 0;
int maxLength = 4000;
int countOfSub = msg.length() / maxLength;
if (countOfSub > 0) {  // The log is so long
for (int i = 0; i < countOfSub; i++) {
String sub = msg.substring(index, index + maxLength);
printSub(type, tag, sub);
index += maxLength;
}
//printSub(type, msg.substring(index, msg.length()));
} else {
printSub(type, tag, msg);
}
}
private static void printSub(int type, String tag, String sub) {
if (tag == null) {
tag = TAG;
}
switch (type) {
case VERBOSE:
Log.v(tag, sub);
break;
case DEBUG:
Log.d(tag, sub);
break;
case INFO:
Log.i(tag, sub);
break;
case WARN:
Log.w(tag, sub);
break;
case ERROR:
Log.e(tag, sub);
break;
case ASSERT:
Log.wtf(tag, sub);
break;
}
}
private static void printJson(String tag, String json, String headString) {
if (TextUtils.isEmpty(json)) {
d("Empty/Null json content");
return;
}
if (TextUtils.isEmpty(tag)) {
tag = TAG;
}
String message;
try {
if (json.startsWith("{")) {
JSONObject jsonObject = new JSONObject(json);
message = jsonObject.toString(JSON_INDENT);
} else if (json.startsWith("[")) {
JSONArray jsonArray = new JSONArray(json);
message = jsonArray.toString(JSON_INDENT);
} else {
message = json;
}
} catch (JSONException e) {
message = json;
}
printLine(tag, true);
message = headString + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "|" + line);
}
printLine(tag, false);
}
private static void printXml(String tag, String xml, String headString) {
if (TextUtils.isEmpty(tag)) {
tag = TAG;
}
if (xml != null) {
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
xml = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (Exception e) {
e.printStackTrace();
}
xml = headString + "\n" + xml;
} else {
xml = headString + "Log with null object";
}
printLine(tag, true);
String[] lines = xml.split(LINE_SEPARATOR);
for (String line : lines) {
if (!TextUtils.isEmpty(line)) {
Log.d(tag, "|" + line);
}
}
printLine(tag, false);
}
private static String[] wrapperContent(String tag, Object... objects) {
if (TextUtils.isEmpty(tag)) {
tag = TAG;
}
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement targetElement = stackTrace[5];
String fileName = targetElement.getFileName();
String methodName = targetElement.getMethodName();
int lineNumber = targetElement.getLineNumber();
if (lineNumber < 0) {
lineNumber = 0;
}
String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
String msg = (objects == null) ? "Log with null object" : getObjectsString(objects);
//        String headString = "[(" + className + ":" + lineNumber + ")#" + methodNameShort + " ] ";
String headString = "[(" + fileName + ":" + lineNumber + ")#" + methodNameShort + " ] ";
return new String[]{tag, msg, headString};
}
private static String getObjectsString(Object... objects) {
if (objects.length > 1) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n");
for (int i = 0; i < objects.length; i++) {
Object object = objects;
if (object == null) {
stringBuilder.append("param").append("[").append(i).append("]").append(" = ").append("null").append("\n");
} else {
stringBuilder.append("param").append("[").append(i).append("]").append(" = ").append(object.toString()).append("\n");
}
}
return stringBuilder.toString();
} else {
Object object = objects[0];
return object == null ? "null" : object.toString();
最后
我见过很多技术leader在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了7、8年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。
其实30岁到40岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。
不断奔跑,你就知道学习的意义所在!
以上进阶BATJ大厂学习资料可以免费分享给大家,需要完整版的朋友,【点这里可以看到全部内容】。


关注下面的标签,发现更多相似文章