package com.herman.util;
import java.io.*;
/**
* @see 自定义日志工具类
* @author Herman.Xiong
* @date 2014年4月22日 14:20:15
* @version V1.0
* @since jdk 1.6
*/
public class LogUtil implements Serializable{
private static final long serialVersionUID = -3042686055658047285L;
private transient Object backtrace;
private String detailMessage;
private LogUtil cause = this;
private StackTraceElement[] stackTrace;
public LogUtil() {
fillInStackTrace();
}
public LogUtil(String message) {
fillInStackTrace();
detailMessage = message;
}
public LogUtil(String message, LogUtil cause) {
fillInStackTrace();
detailMessage = message;
this.cause = cause;
}
public LogUtil(LogUtil cause) {
fillInStackTrace();
detailMessage = (cause==null ? null : cause.toString());
this.cause = cause;
}
public String getMessage() {
return detailMessage;
}
public String getLocalizedMessage() {
return getMessage();
}
public LogUtil getCause() {
return (cause==this ? null : cause);
}
public synchronized LogUtil initCause(LogUtil cause) {
if (this.cause != this)
throw new IllegalStateException("Can't overwrite cause");
if (cause == this)
throw new IllegalArgumentException("Self-causation not permitted");
this.cause = cause;
return this;
}
public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
public void printStackTrace() {
printStackTrace(System.err);
}
public void printStackTrace(PrintStream s) {
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (int i=0; i < trace.length; i++){
s.println("\tat " + trace[i]);
}
LogUtil ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
private void printStackTraceAsCause(PrintStream s,
StackTraceElement[] causedTrace)
{
StackTraceElement[] trace = getOurStackTrace();
int m = trace.length-1, n = causedTrace.length-1;
while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
m--; n--;
}
int framesInCommon = trace.length - 1 - m;
s.println("Caused by: " + this);
for (int i=0; i <= m; i++)
s.println("\tat " + trace[i]);
if (framesInCommon != 0)
s.println("\t... " + framesInCommon + " more");
LogUtil ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
public void printStackTrace(PrintWriter s) {
synchronized (s) {
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (int i=0; i < trace.length; i++)
s.println("\tat " + trace[i]);
LogUtil ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
}
private void printStackTraceAsCause(PrintWriter s,
StackTraceElement[] causedTrace)
{
StackTraceElement[] trace = getOurStackTrace();
int m = trace.length-1, n = causedTrace.length-1;
while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
m--; n--;
}
int framesInCommon = trace.length - 1 - m;
s.println("Caused by: " + this);
for (int i=0; i <= m; i++)
s.println("\tat " + trace[i]);
if (framesInCommon != 0)
s.println("\t... " + framesInCommon + " more");
LogUtil ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
public synchronized native Throwable fillInStackTrace();
public StackTraceElement[] getStackTrace() {
return (StackTraceElement[]) getOurStackTrace().clone();
}
private synchronized StackTraceElement[] getOurStackTrace() {
if (stackTrace == null) {
int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
for (int i=0; i < depth; i++)
stackTrace[i] = getStackTraceElement(i);
}
return stackTrace;
}
public void setStackTrace(StackTraceElement[] stackTrace) {
StackTraceElement[] defensiveCopy =
(StackTraceElement[]) stackTrace.clone();
for (int i = 0; i < defensiveCopy.length; i++)
if (defensiveCopy[i] == null)
throw new NullPointerException("stackTrace[" + i + "]");
this.stackTrace = defensiveCopy;
}
private native int getStackTraceDepth();
private native StackTraceElement getStackTraceElement(int index);
private synchronized void writeObject(java.io.ObjectOutputStream s)
throws IOException
{
getOurStackTrace();
s.defaultWriteObject();
}
}
业余草
- 粉丝: 1w+
- 资源: 977
最新资源
- 小学三年级语文期末考真题
- RedisUtils redis工具类
- IDEABito-Jetbrains-PROD-1.1.9.zip
- 基于SpringBoot+Vue.JS前后端分离的网上订餐系统 源码+数据库+论文(毕业设计)
- Swift Playgrounds:交互式编程的乐园
- 基于SpringBoot+Vue.JS前后端分离的在线文档管理系统 源码+数据库+论文(毕业设计)
- PDManer-win-v4.7.0.exe
- 组织星球36氪:2024年TikTok电商白皮书-2024.pdf
- 让AI给你写代码(10.2)具备调用本地库能力的AI小助手代码介绍
- 基于SpringBoot+Vue.JS前后端分离的网上超市系统 源码+数据库+论文(毕业设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈