Dear James,

thank you for your quick response. I will check wether it works. 
As I just notice, there is a comercial GUI viewer available, basing on
log4J. I will check this, too.

Thilo

-----Original Message-----
From: James House [mailto:[EMAIL PROTECTED]]
Sent: Donnerstag, 14. Juni 2001 19:59
To: LOG4J Users Mailing List
Subject: Re: Missing classes (LogTextPanel / TextPanelAppender)



>I tried to compile the the class:
>LogTextPanelExample.java which is located under this path:
>
>C:\Java\jakarta-log4j-1.1.2\contribs\JamesHouse\
>
>I moved the java files to folders that should represent the package
>statement, but when compiling, there was a reference to the class:
>
>import org.apache.log4j.helpers.TracerPrintWriter;
>
>But this class is not included in the log4j.jar (version 1.12), neither can
>the class be found anywhere in the log4J installtion directory.

Hi,

I'm the author of the LogTextPanel / LogPanelAppender.
I posted a mail about this a few weeks back (when 1.10 was released).

This class was written for Log4j 1.0.4 that is why it does not
work... However, the new version of it included in this e-mail
(below) does work with Log4j 1.1x (I actually haven't tried
it against the last release).

Also keep in mind that this was written as a proto-type,
and it has some glitches.  -- mainly with not always
correctly showing the last line, and not handling resizing
of the panel -- these can fairly easily be fixed, but I didn't
include the math/logic necessary for handling these
because after all, it was a quick proto-type.

Ceki:  Maybe you want to include this new version with
the next release - so that people at least have a compilable
version...

Anyone: Perhaps someone would like to finish this the
right way??? -- I don't have the time - or at least wont
for some number of weeks.

James




===========================================
TextPanelAppender.java
===========================================

package org.apache.log4j.gui;


import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Hashtable;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.apache.log4j.*;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.QuietWriter;
import org.apache.log4j.helpers.OptionConverter;


/**
  *
  * @author James House, based on TextPaneAppender by Sven Reimers
  */

public class TextPanelAppender extends AppenderSkeleton {

   LogTextPanel logTextPanel;
   LogPublishingThread logPublisher;

   final String COLOR_OPTION_FATAL = "Color.Fatal";
   final String COLOR_OPTION_ERROR = "Color.Error";
   final String COLOR_OPTION_WARN = "Color.Warn";
   final String COLOR_OPTION_INFO = "Color.Info";
   final String COLOR_OPTION_DEBUG = "Color.Debug";
   final String COLOR_OPTION_BACKGROUND = "Color.Background";
   final String FONT_NAME_OPTION = "Font.Name";
   final String FONT_SIZE_OPTION = "Font.Size";
   final String EVENT_BUFFER_SIZE_OPTION = "EventBuffer.Size";

   public TextPanelAppender(Layout layout, String name) {
     this.layout = layout;
     this.name = name;
     setLogTextPanel(new LogTextPanel());
     logPublisher = new LogPublishingThread(logTextPanel, Priority.ERROR,
500);
     //logPublisher = new LogPublishingThread(logTextPanel, null, 500);
   }

   public
   void close() {
   }


   public void append(LoggingEvent event) {

     String text = this.layout.format(event);

     // Print Stacktrace
     // Quick Hack maybe there is a better/faster way?
     String[] stkTrace = event.getThrowableStrRep();
     if(stkTrace != null) {
       for(int i=0; i < stkTrace.length; i++) {
         StringBuffer line = new StringBuffer();
         for(int j=0; j < stkTrace[i].length(); j++) {
           if(stkTrace[i].charAt(j) == '\t')
             line.append("    ");
           else
             line.append(stkTrace[i].charAt(j));
         }
         text += line.toString();
       }
     }
     else
       if(!text.endsWith("\n"))
         text += "\n";

     logPublisher.publishEvent(event.priority, text);
   }

   public
   JPanel getLogTextPanel() {
     return logTextPanel;
   }

   public
   String[] getOptionStrings() {
     return new String[] { COLOR_OPTION_FATAL, COLOR_OPTION_ERROR,
          COLOR_OPTION_WARN, COLOR_OPTION_INFO, COLOR_OPTION_DEBUG,
          COLOR_OPTION_BACKGROUND, FONT_NAME_OPTION, FONT_SIZE_OPTION};
   }


   public
   void setName(String name) {
     this.name = name;
   }

   protected
   void setLogTextPanel(LogTextPanel logTextPanel) {
     this.logTextPanel = logTextPanel;
     logTextPanel.setTextBackground(Color.white);
   }

   public
   void setOption(String option, String value) {
     if (option.equalsIgnoreCase(COLOR_OPTION_FATAL))
       logTextPanel.setTextColor(Priority.FATAL,value);
     if (option.equalsIgnoreCase(COLOR_OPTION_ERROR))
       logTextPanel.setTextColor(Priority.ERROR,value);
     if (option.equalsIgnoreCase(COLOR_OPTION_WARN))
       logTextPanel.setTextColor(Priority.WARN,value);
     if (option.equalsIgnoreCase(COLOR_OPTION_INFO))
       logTextPanel.setTextColor(Priority.INFO,value);
     if (option.equalsIgnoreCase(COLOR_OPTION_DEBUG))
       logTextPanel.setTextColor(Priority.DEBUG,value);
     if (option.equalsIgnoreCase(COLOR_OPTION_BACKGROUND))
       logTextPanel.setTextBackground(value);
     if (option.equalsIgnoreCase(FONT_SIZE_OPTION))
       logTextPanel.setTextFontSize(Integer.parseInt(value));
     if (option.equalsIgnoreCase(FONT_NAME_OPTION))
       logTextPanel.setTextFontName(value);
     if (option.equalsIgnoreCase(EVENT_BUFFER_SIZE_OPTION))
       logTextPanel.setEventBufferSize(Integer.parseInt(value));
     return;
   }

   public
   boolean requiresLayout() {
     return true;
   }



   class LogPublishingThread extends Thread {

     LogTextPanel logTextPanel;
     ArrayList evts;
     Priority triggerPrio;
     long pubInterval;

     public LogPublishingThread(LogTextPanel logTextPanel, Priority 
triggerPrio, long pubInterval) {
       this.logTextPanel = logTextPanel;
       this.evts = new ArrayList(1000);
       this.triggerPrio = triggerPrio;
       this.pubInterval = pubInterval;
       //this.setPriority(Thread.NORM_PRIORITY - 1);
       this.start();
     }

     public void run() {
       while(true) {
         synchronized(evts) {
           try {
             evts.wait(pubInterval);
           }
           catch(InterruptedException e) {}

           logTextPanel.newEvents((EventBufferElement[])evts.toArray(new 
EventBufferElement[evts.size()]));

           evts.clear();
         }
       }

     }

     public void publishEvent(Priority prio, String text) {
       synchronized(evts) {
         evts.add(new EventBufferElement(prio, text));
         if(triggerPrio != null && prio.isGreaterOrEqual(triggerPrio))
           evts.notify();
       }
     }
   }

} // TextPaneAppender

class EventBufferElement {

   public String text;
   public Priority prio;
   public int numLines;

   EventBufferElement(Priority prio, String text) {
     this.prio = prio;
     this.text = text;
     numLines = 1;
     int pos = pos = text.indexOf('\n', 0);
     int len = text.length() - 1;

     while( (pos > 0) && (pos < len) )
       numLines++;
       pos = text.indexOf('\n', pos + 1);
   }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to