Moved getCallingClass function to ClassUtil
This commit is contained in:
parent
c5c135320f
commit
8ce73e3f50
3 changed files with 24 additions and 16 deletions
|
|
@ -27,6 +27,7 @@ package zutil;
|
|||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
|
|
@ -162,4 +163,23 @@ public class ClassUtil {
|
|||
}
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the first class in the stack that do not match the filter
|
||||
*/
|
||||
public static String getCallingClass(Class... filter){
|
||||
ArrayList filterStr = new ArrayList();
|
||||
for (Class clazz : filter)
|
||||
filterStr.add(clazz.getName());
|
||||
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
for(int i=1; i<stackTraceElements.length ;++i){
|
||||
String className = stackTraceElements[i].getClassName();
|
||||
if( !filterStr.contains(className) ){
|
||||
return className;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.log;
|
||||
|
||||
import zutil.ClassUtil;
|
||||
import zutil.io.file.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -45,21 +46,7 @@ public class LogUtil {
|
|||
* @return a new Logger for the calling class
|
||||
*/
|
||||
public static Logger getLogger(){
|
||||
return Logger.getLogger(getCallingClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the parent class other than Logger in the stack
|
||||
*/
|
||||
public static String getCallingClass(){
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
for(int i=1; i<stackTraceElements.length ;++i){
|
||||
String name = stackTraceElements[i].getClassName();
|
||||
if( !name.equals( LogUtil.class.getName() ) ){
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Logger.getLogger(ClassUtil.getCallingClass(LogUtil.class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.net.ssdp;
|
||||
|
||||
import zutil.ClassUtil;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -36,7 +37,7 @@ import java.util.logging.Level;
|
|||
public class SSDPClientTest {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.out.println(LogUtil.getCallingClass());
|
||||
System.out.println(ClassUtil.getCallingClass());
|
||||
LogUtil.setGlobalLevel(Level.FINEST);
|
||||
SSDPClient ssdp = new SSDPClient();
|
||||
ssdp.requestService("upnp:rootdevice");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue