Solved

Java Debug/Output Window

Posted on 2001-09-05
14
453 Views
Last Modified: 2008-04-28
We are in need of a scrolling text window that can handle debug output.

This is how it needs to work:
1. Any object can "send" debug output to the window.

2. The output needs to have a time stamp, calling module file and line number.  The color of the output text can also be modified by the calling module.

3. Each debug call can have a id/priority parameter.  This id/priority can be filtered on from the debug window.

Does anything like this exist?

Thanks in advance
0
Comment
Question by:joegood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
14 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457147
well, I have coded such stuff several times, it will took you no more than 4 hours to code it yourslef (without the color stuff).

I can dig some code if you really need it.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457151
I mean - not perfect, but working code.
0
 

Author Comment

by:joegood
ID: 6457183
I would be willing to look at it. Ofcourse there would have to be no strings attached.  No license fees are anything like that.  Fair trade, points for code.  Also, this code will have to be close to what I am looking for.

Thanks in advance
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 16

Expert Comment

by:heyhey_
ID: 6457205
I found some stuff from some recent project. not perfect as I already said :)

I'll copy&paste several files here
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 300 total points
ID: 6457206
/*
 main class
*/

package com.heyhey1.app.base.common.util;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class logg
{
  public static final long MODE_ERROR = 1;
  public static final long MODE_WARN = 2;
  public static final long MODE_INFO = 4;
  public static final long MODE_DEBUG = 8;
  public static final long MODE_MES = 16;
 
  private static logger theLogger = new deflogger();

  public static logger getLogger()
  {
    return theLogger;
  }
 
  public static void setLogger(logger _logger)
  {
//    new Throwable().printStackTrace();
    if (theLogger != null)
    {
      theLogger.destroy();
    }
    theLogger = _logger;
    theLogger.init();
  }

  public void log(int mode, String message)
  {
    theLogger.log(mode, message, null);
  }
 
  public void log(int mode, String message, Throwable x)
  {
    theLogger.log(mode, message, x);
  }
 
  public static void mes(String st)
  {
    theLogger.log(MODE_MES, st, null);
  }
 
  public static void log (String st)
  {
    theLogger.log(MODE_INFO, st, null);
  }
  public static void log (String st, Throwable x)
  {
    theLogger.log(MODE_INFO, st, x);
  }
 
  public static void debug (String st)
  {
    theLogger.log(MODE_DEBUG, st, null);
  }
 
  public static void debug (String st, Throwable x)
  {
    theLogger.log(MODE_DEBUG, st, x);
  }
 
  public static void err(String st)
  {
    theLogger.log(MODE_ERROR, st, null);
  }
 
  public static void err(String st, Throwable x)
  {
    theLogger.log(MODE_ERROR, st, x);
  }
 
  public static void warn(String st)
  {
    theLogger.log(MODE_WARN, st, null);
  }
 
  public static void warn(String st, Throwable x)
  {
    theLogger.log(MODE_WARN, st, x);
  }
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457210
/*
  default logger - uses system.out
*/

package com.heyhey1.app.base.common.util;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class deflogger implements logger
{
  protected static final Object[][] priorityStrings =
  {
    {new Long(logg.MODE_ERROR),  "error"},
    {new Long(logg.MODE_WARN),   "warn "},
    {new Long(logg.MODE_INFO),   "info "},
    {new Long(logg.MODE_DEBUG),  "debug"},
    {new Long(logg.MODE_MES),    " mes "},
    {new Long(-1),               "     "},
  };
 
  public static Hashtable priorities = new Hashtable();
  static
  {
    for (int i = 0; i < priorityStrings.length; i++)
    {
      priorities.put(priorityStrings[i][0], priorityStrings[i][1]);
    }
  }
 
  protected static SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, HH:mm:ss", java.util.Locale.US);
     
  public static String getFormattedDate(Date date)
  {
    if (date == null) date = new Date();
    return dateFormat.format(date);
  }

  public static String getPriorityString(long l)
  {
    String st = (String)priorities.get(new Long(l));
    if (st == null)
      st = (String)priorities.get(new Long(-1));
   
    return st;
  }
 
  public synchronized void log(long mode, String message, Throwable x)
  {
//    System.out.println("logg: "+ mode + " / " + message);
    if ((logMode & mode) != 0)
    {
      System.out.print("[" + getFormattedDate(null)  + "] ");
      System.out.print("[" + getPriorityString(mode)  + "] ");
      System.out.println("" + message);
      if (x != null)
        x.printStackTrace(System.out);
    }
  }

  protected long logMode = Long.MAX_VALUE;
  public void enableLogging(long l)
  {
    logMode = l;
  }

  public void init() {}
  public void destroy() {}
 
  public static String exceptionToString(Throwable x)
  {
    StringBuffer logBuffer = new StringBuffer();
    if (x != null)
    {
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      x.printStackTrace(new PrintStream(os));
      logBuffer.append("  [" + x.getClass().getName() + "]");
      logBuffer.append("\n  [" + x + "]");
     
      String buff = new String(os.toByteArray());
//      logBuffer.append("buff: " + buff.length() + " / " + buff.substring(0, 10));
      logBuffer.append("\nbuff: " + buff);
      logBuffer.append("\n");
    }
    return logBuffer.toString();
  }
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457221
you can create specialized logger objects that write the log messages in file, post them to the webserver (using HTTP POST) or show your own console.

I could not find PanelLog example in this project, give me 5 minutes to adapt it
0
 

Author Comment

by:joegood
ID: 6457226
I would be willing to look at it. Ofcourse there would have to be no strings attached.  No license fees are anything like that.  Fair trade, points for code.  Also, this code will have to be close to what I am looking for.

Thanks in advance
0
 

Author Comment

by:joegood
ID: 6457241
I would be willing to look at it. Ofcourse there would have to be no strings attached.  No license fees are anything like that.  Fair trade, points for code.  Also, this code will have to be close to what I am looking for.

Thanks in advance
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457249
/*
something like this should work for you - can't test it myself right now.
*/

package com.heyhey1.app.base.common.util;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class panellog extends deflogger implements ActionListener, WindowListener
{
  private StringBuffer buf = new StringBuffer();
  private boolean isDisabled = false;
 
  public synchronized void log(long mode, String message, Throwable x)
  {
    if ((logMode & mode) != 0)
    {
      String st = "[" + getFormattedDate(null)  + "] ";
      st += "[" + getPriorityString(mode)  + "] ";
      st += "" + message;
      if (x != null)
      {
        st += "\n";
        st += exceptionToString(x);
      }
      System.out.println("" + st);
      st += "\n";
      buf.append(st);
      if (buf.length() > 29000)
      {
        char[] chars = new char[23000];
        buf.getChars(buf.length() - 23000, buf.length(), chars, 0);
        buf = new StringBuffer();
        buf.append(chars);
       
        if (frame.isVisible() && !isDisabled)
        {
          textArea.setText(buf.toString());
        }
      }
      else
      {
        if (frame.isVisible() && !isDisabled)
        {
          textArea.appendText(st);
        }
      }
    }
  }  
  public panellog()
  {
    this("PanelLog");
  }
  public panellog(String _name)
  {
    name = _name;
    initUI();
    initListeners();
  }

  private void initUI()
  {
    clearButton = new Button("Clear");
    disableButton = new Button("Disable");
    hideButton = new Button("Hide");
    textArea = new TextArea();
    frame = new Frame(name);
   
    Font f = new Font("monospaced", Font.PLAIN, 12);
    textArea.setFont(f);
   
   
    Panel buttonPanel = new Panel();
    buttonPanel.setLayout(new GridLayout(1, 3));
    buttonPanel.add(clearButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(hideButton);
   
    frame.setLayout(new BorderLayout());
    frame.add(BorderLayout.CENTER, textArea);
    frame.add(BorderLayout.SOUTH, buttonPanel);
    frame.setBounds(150, 50, 1000, 650);
  }
  private void initListeners()
  {
    frame.addWindowListener(this);
    clearButton.addActionListener(this);
    disableButton.addActionListener(this);
    hideButton.addActionListener(this);
  }
 
  private String name;
  private Frame frame;
  private TextArea textArea;
  private Button clearButton, disableButton, hideButton;

  public void hideWindow()
  {
    frame.hide();
  }

  public void showWindow()
  {
    textArea.setText(buf.toString());
    frame.show();
  }
 
  public void windowOpened(WindowEvent e){}
  public void windowClosed(WindowEvent e){}
  public void windowIconified(WindowEvent e){}
  public void windowDeiconified(WindowEvent e){}
  public void windowActivated(WindowEvent e){}
  public void windowDeactivated(WindowEvent e){}
 
  public void windowClosing(WindowEvent e)
  {
    hideWindow();
  }
 
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == clearButton)
    {
      textArea.setText("");
    }
    else if (e.getSource() == disableButton)
    {
      isDisabled = !isDisabled;
      if (isDisabled)
        disableButton.setLabel("Enable");
      else
        disableButton.setLabel("Disable");
    }
    else if (e.getSource() == hideButton)
    {
      hideWindow();
    }
  }
}
0
 

Author Comment

by:joegood
ID: 6457253
I would be willing to look at it. Ofcourse there would have to be no strings attached.  No license fees are anything like that.  Fair trade, points for code.  Also, this code will have to be close to what I am looking for.

Thanks in advance
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457293
/*
forgot the logger interface :(

in your code you can use
  logg.log("normal event");
or
catch (Throwable x)
{
  logg.err("unexpected Throwable :(", x);
}
 
and current logger will log it (if filter allows it)

you can set your own logger object - to use panellog just use

pannellog panel = new pannellog()
logg.setLogger(panel);

*/


package com.heyhey1.app.base.common.util;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public interface logger
{
  public void init();
  public void destroy();
  public void log(long mode, String message, Throwable x);
  public void enableLogging(long mode);
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6457317
I use only five types of log data, because it's easier for me to type

logg.log("message");
then  
logg.log(MODE_INFO, "message");

as for the calling class, method & line number - you do not have preprocessor in Java so there are only two possible solutions:
1. always add them them as parameters when calling logg.log();
2. you can 'cheat' by using dummy Exceptions & stacktrace info (but this is quite unreliable & JavaVM dependent solution)
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 6931819
Hopefully you've already been helped with this question, but thought you'd appreciate knowing this.  It would be great if you could bring this question to a conclusion, awarding the experts above who helped you with points or a comment to them on your status today.

WindowsUpdate has new updates for .NET users; Details follow - Microsoft .NET Framework
The .NET Framework is a new feature of Windows. Applications built using the .NET Framework are more reliable and secure. You need to install the .NET Framework only if you have software that requires it.

For more information about the .NET Framework, see http://www.microsoft.com/net. (This site is in English.)

System Requirements
The .NET Framework can be installed on the following operating systems:
Windows 98
Windows 98 Second Edition (SE)
Windows Millennium Edition (Windows Me)
Windows NT 4.0® (Workstation or Server) with Service Pack 6.0a
Windows 2000 with the latest service pack installed (Professional, Server, Datacenter Server, or Advanced Server)
Windows XP (Home Edition and Professional)
You must be running Internet Explorer version 5.01 or later for all installations of the .NET Framework.

To install the .NET Framework, your computer must meet or exceed the following software and hardware requirements:

Software requirements for server operating systems:
MDAC 2.6
Hardware requirements:
For computers running only a .NET Framework application, Pentium 90 mHz CPU with 32 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 mHz CPU with 128 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
Recomended software:
MDAC 2.7 is recommended.
Recommended hardware: For computers running only a .NET Framework application, Pentium 90 MHz CPU with 96 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 MHz CPU with 256 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.

How to use -> Restart your computer to complete the installation. No other action is required to run .NET Framework applications. If you are developing applications using the .NET Framework, you can use the command-line compilers or you can use a development environment, such as Visual Studio .NET, that supports using the .NET Framework.

How to uninstall
To uninstall the .NET Framework: Click Start, point to Settings, and then click Control Panel (In Windows XP, click Start and then click Control Panel.).
Click Add/Remove Programs.
Click Microsoft .NET Framework (English) v1.0.3705 and then click Change/Remove.
More here  http://www.microsoft.com/net/

The .NET topic is being considered for addition to our All Topics link soon, so this may interest you as well:
http://www.experts-exchange.com/newtopics/Q.20276589.html

EXPERTS POINTS are waiting to be claimed here:  http://www.experts-exchange.com/commspt/Q.20277028.html

":0)
Asta


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question