Link to home
Start Free TrialLog in
Avatar of joegood
joegood

asked on

Java Debug/Output Window

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
Avatar of heyhey_
heyhey_

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.
I mean - not perfect, but working code.
Avatar of joegood

ASKER

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
I found some stuff from some recent project. not perfect as I already said :)

I'll copy&paste several files here
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
/*
  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();
  }
}
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
Avatar of joegood

ASKER

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
Avatar of joegood

ASKER

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
/*
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();
    }
  }
}
Avatar of joegood

ASKER

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
/*
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);
}
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)
Avatar of Asta Cu
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:
https://www.experts-exchange.com/newtopics/Q.20276589.html

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

":0)
Asta