📄 traceviewer.java
字号:
package debugging;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
* A class to visualize a debug trace.
*/
public class TraceViewer extends JFrame implements ListCellRenderer
{
/**
* The main() method reads the log output from a text file and
* then displays the logged invocations. The log file name is supplied
* as a command line argument.
*/
public static void main(String args[])
{
if (args.length<1) System.exit(0);
// -- get inocations from the log file --
Vector invocations=TraceViewer.getInvocations(args[0]);
// -- display them --
TraceViewer.displayInvocations(invocations);
}
/**
* This method reads a set of tagged lines, from the given file,
* which corresponding with method invocations.
*/
private static Vector getInvocations(String fileName)
{
Vector invocations=new Vector();
try
{
BufferedReader reader=new BufferedReader(new FileReader(fileName));
String thisLine=reader.readLine();
while (thisLine!=null)
{
InvocationRecord thisInvocation=null;
// -- create an invocation record from this line --
if (thisLine.length()>0)
thisInvocation=new InvocationRecord(thisLine);
// -- add it to the Vector --
if (thisInvocation!=null)
invocations.addElement(thisInvocation);
thisLine=reader.readLine();
}
reader.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return invocations;
}
/**
* Static method to create a TraceViewer instance with a set of
* invocations to be visualised.
*/
public static void displayInvocations(Vector invocations)
{
// -- call the TraceViewer constructor with a set of invocations --
TraceViewer traceViewer=new TraceViewer(invocations);
traceViewer.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent event) {System.exit(0);}
}
);
// -- display it --
traceViewer.setSize(500,500);
traceViewer.setVisible(true);
}
/**
* The constructor creates a JList with a set of invocations
* as the content to be visualised.
*/
public TraceViewer(Vector invocations)
{
JList traceList=new JList(invocations);
traceList.setCellRenderer(this);
getContentPane().add(new JScrollPane(traceList));
}
/**
* This method is called automatically, and repeatedly, to render
* element of the JList.
*/
public Component getListCellRendererComponent(JList list, Object value
, int index, boolean isSelected, boolean cellHasFocus)
{
// -- Here's the thing we're going to display --
InvocationRecord thisInvocation=(InvocationRecord) value;
// -- Create a Jpanel to hold everything --
JPanel invocationPanel=new JPanel();
invocationPanel.setLayout(new GridLayout(6,1));
Font labelFont=new Font("Courier",Font.PLAIN,11);
// -- Create a "sender" Label --
JLabel senderLabel=new JLabel("SENDER: " + thisInvocation.sender);
senderLabel.setFont(labelFont);
senderLabel.setForeground(Color.black);
// -- Create a "message" Label --
JLabel messageLabel=new JLabel(" MESSAGE: " + thisInvocation.message);
messageLabel.setFont(labelFont);
messageLabel.setForeground(Color.black);
// -- Create a "receiver" Label --
JLabel receiverLabel=new JLabel(" RECEIVER: " + thisInvocation.receiver);
receiverLabel.setFont(labelFont);
receiverLabel.setForeground(Color.black);
// -- Create a "thread" Label --
JLabel threadLabel=new JLabel(" THREAD: " + thisInvocation.thread);
threadLabel.setFont(labelFont);
threadLabel.setForeground(Color.black);
// -- Create a "duration" Label --
JLabel durationLabel=new JLabel(" DURATION: " + thisInvocation.duration);
durationLabel.setFont(labelFont);
durationLabel.setForeground(Color.black);
// -- Add everything to the panel --
invocationPanel.add(senderLabel);
invocationPanel.add(messageLabel);
invocationPanel.add(receiverLabel);
invocationPanel.add(threadLabel);
invocationPanel.add(durationLabel);
invocationPanel.add(new JLabel(""));
invocationPanel.setBackground(Color.white);
invocationPanel.setOpaque(true);
return invocationPanel;
}
}
/**
* This class represents an individual invocation record to be
* displayed.
*/
class InvocationRecord
{
public String sender="";
public String message="";
public String receiver="";
public String thread="";
public String duration="";
/**
* The constructor makes an invocation record from the
* tagged input.
*/
public InvocationRecord(String taggedText)
{
sender=InvocationRecord.getField(taggedText,"sender");
message=InvocationRecord.getField(taggedText,"message");
receiver=InvocationRecord.getField(taggedText,"receiver");
thread=InvocationRecord.getField(taggedText,"thread");
duration=InvocationRecord.getField(taggedText,"duration");
if (duration.length()>0)
duration=duration+"ms";
}
/**
* This method extracts the content of a given tag.
*/
public static String getField(String taggedText, String tag)
{
String fieldValue="";
int startPos=taggedText.indexOf("<"+tag+">");
int endPos=taggedText.indexOf("</"+tag+">");
if ( (startPos>=0) && (endPos>startPos) )
{
startPos=startPos+tag.length()+2;
fieldValue=taggedText.substring(startPos,endPos);
}
return fieldValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -