Skip to content

Commit b87d71c

Browse files
committed
Show a stacktrace for every method call.
1 parent d9a468e commit b87d71c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/main/java/org/apache/hadoop/fs/glusterfs/GlusterDebugFileSystem.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,23 @@ public class GlusterDebugFileSystem extends GlusterFileSystem{
6363

6464
private static File logFile=null;
6565
private static final String LOG_PREFIX="/tmp/glusterfs";
66-
66+
private static boolean showStackTrace = true;
67+
68+
public static synchronized String getStackTrace(int stripTopElements){
69+
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
70+
String traceString = "";
71+
// remove the specified top elements of the stack trace (to avoid debug methods in the trace). the +1 is for this methods call.
72+
for(int i=stripTopElements+1;i<trace.length;i++){
73+
traceString += "\t[" + trace[i].getFileName() + "] " + trace[i].getClassName() + "." + trace[i].getMethodName() + "() line:" + trace[i].getLineNumber() + "\n";
74+
}
75+
76+
return traceString;
77+
}
78+
6779
public static synchronized void logMachine(String text){
6880

6981
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
7082
Date date=new Date();
71-
7283
if(logFile==null){
7384
for(int i=0;i<1000000;i++){
7485
logFile=new File(LOG_PREFIX+"-"+i+".log");
@@ -84,7 +95,9 @@ public static synchronized void logMachine(String text){
8495

8596
try{
8697
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
87-
out.write(dateFormat.format(date)+" : "+text+"\n");
98+
out.write("(" + dateFormat.format(date)+") : "+text+"\n");
99+
String stackTrace = GlusterDebugFileSystem.getStackTrace(3);
100+
if(showStackTrace) out.write(stackTrace);
88101
out.close();
89102
}catch (FileNotFoundException e){
90103
e.printStackTrace();

0 commit comments

Comments
 (0)