🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #392

StackOverflowException with a logging class

#392
From: scott
Date: Fri, 27 May 2005 20:11
219 lines
7441 bytes
I've written a class to handle logging (haven't learned log4j yet).

It's designed so that any number of implementations can be added to the logger.  

The Logger class� constructor looks like this:

    public Logger(LoggerImpl impl) {
        startDate = new Date();
        lastMsg = new String();
        requirements = new Hashtable();
        attributes = new Hashtable();
        attributeValues = new Hashtable();
        traces = new Hashtable();
        otherObjects = new Hashtable();
        implementations = new Vector();
        addImplementation(impl);

    }

I�ve written one implementation for system.out and one that writes to a text file. 
The system.out code is below (text file just passes in a different out variable).

I think this is a basic Bridge (or maybe Strategy?) pattern.

The application that uses it scans about 4500 Caliber requirements, outputting lots 
of details along the way.  As it does, it calls methods that look like this:

    public void reportRequirement(Requirement req, String msg){
        if (!requirements.containsKey(req.getID())){
            requirements.put(req.getID(), req);
        }
        Iterator iter = implementations.iterator();
        while (iter.hasNext()){
            LoggerImpl impl = (LoggerImpl)iter.next();
            impl.reportRequirement(req, msg);
        }
    }

I'm getting Stack Overflow errors when I run the program and want to eliminate 
the logger as the cause.

Does anyone see any problem with this code?  Maybe the hashtables get too big?  


Thanks


package com.rps.caliber.loggers;

import com.starbase.caliber.*;
import com.starbase.caliber.attribute.Attribute;
import com.starbase.caliber.attribute.AttributeValue;
import com.starbase.caliber.server.RemoteServerException;
import java.io.PrintStream;
import java.io.FileOutputStream;

/**
 * <p>Title: PrintStreamLoggerImpl</p>
 *
 * <p>Description: An implementation for Logger that writes to any PrintStream.
 *   By default, System.out is used.</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Research Pharmaceutical Services</p>
 *
 * @author Scott Powell
 * @version 1.0
 */
public class PrintStreamLoggerImpl extends LoggerImpl {
    PrintStream out = System.out;


    public PrintStreamLoggerImpl() {
        super();
    }

    /**
     * Set the PrintStream to be used by this implementation.
     * @param stream, the PrintStream to use.
     * @return void
     */

    public void setOutputStream(PrintStream stream){
        this.out = stream;
    }
    /**
     * Returns the print stream used by this implementation.
     * @return the PrintStream
     */
    public PrintStream getPrintStream(){
        return this.out;
    }


    /**
     * Prints a summary of logged activities
     * @return void
     */
    public void printSummary() {
        out.println(logger.getSummary());
    }

    /**
     * reportAttribute
     *
     * @param attr Attribute
     * @param msg String
     */
    public void reportAttribute(Attribute attr, String msg) {
        out.println ("Attribute: " + attr.getName().toString() + ", " + msg +
                     ". (" + this.logger.getNumAttributes() + ") so far.");
    }

    /**
     * reportAttributeValue
     *
     * @param value AttributeValue
     * @param msg String
     */
    public void reportAttributeValue(AttributeValue value, String msg) {
        try {
            out.println("AttributeValue: " + value.getAttribute().getName() + ", " + msg +
                    ". (" + this.logger.getNumAttributeValues() + ") so far.");
        } catch (RemoteServerException ex) {
            logger.reportException(ex, "The AttributeValue could not be accessed.");
        }
    }



    /**
     * reportException
     *
     * @param e RemoteServerException
     */
    public void reportException(RemoteServerException e) {
        out.println("***** Start RemoteServerException *****");
        out.println("The following error has occurred: " + e.getMessage());
        e.printStackTrace(out);
        out.println(this.logger.getNumExceptions() + " exceptions so far.");
        out.println("***** End RemoteServerException *****");
    }

    /**
     * reportException
     *
     * @param e Exception
     * @param msg String
     */
    public void reportException(Exception e, String msg) {
        out.println("***** Start Exception *****");
        out.println("The following error has occurred: " + e.getMessage());
        e.printStackTrace(out);
        reportMessage(msg);
        out.println(this.logger.getNumExceptions() + " exceptions so far.");
        out.println("***** End Exception *****");
    }

    /**
     * reportException
     *
     * @param e Exception
     */
    public void reportException(Exception e) {
        out.println("***** Start Exception *****");
        out.println("The following error has occurred: " + e.getMessage());
        e.printStackTrace(out);
        out.println(this.logger.getNumExceptions() + " exceptions so far.");
        out.println("***** End Exception *****");
    }

    /**
     * reportMessage
     *
     * @param msg String The message
     */
    public void reportMessage(String msg) {
        out.println(msg);
    }

    /**
     * reportObject
     *
     * @param obj CaliberObject
     * @param msg String
     */
    public void reportObject(CaliberObject obj, String msg) {
        out.println("Caliber Object: " + obj.getClass().toString() + ", " + msg +
                    ". (" + this.logger.getNumOtherObjects() + ") so far.");
    }

    /**
     * reportRequirement
     *
     * @param req Requirement that's reported
     * @param msg String that is the message
     */
    public void reportRequirement(Requirement req, String msg) {
        String reqMsg = logger.getRequirementMsg(req);
        out.println ("Requirement: " + reqMsg + ", " + msg +
                     ". (" + this.logger.getNumRequirements() + ") so far.");
        out.flush();
    }

    /**
     * reportTrace
     *
     * @param trace Trace that's reported
     * @param msg String that is the message
     */
    public void reportTrace(Trace trace, String msg) {
        String trcMsg = logger.getTraceMsg(trace);
        out.println("Trace: " + trcMsg + ", " + msg +
                    ". (" + this.logger.getNumTraces() + ") so far.");

    }
}
Technical Details
Message-ID: <42977f0e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,1362cbd94d530932,start
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII
From: "scott" <spowell@rpsweb.com>
Sender: "scott" <spowell@rpsweb.com>
Reply-To: "scott" <spowell@rpsweb.com>
Subject: StackOverflowException with a logging class
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 64.80.153.236     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <42977f0e$1@newsgroups.borland.com>
Date: 27 May 2005 13:11:58 -0700
X-Trace: newsgroups.borland.com 1117224718 207.105.83.66 (27 May 2005 13:11:58 -0700)
Lines: 220
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail