/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.util;

import java.io.*;
import java.text.*;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.iface.*;

public class SimpleMemoryLog implements DynamoLog
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static DateFormat s_fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  private static final String S_DEBUG = "DEBUG";
  private static final String S_ERROR = "ERROR";
  private static final String S_FATAL = "FATAL";
  private static final String S_INFO  = "INFO ";
  private static final String S_WARN  = "WARN ";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private LinkedList m_scroll = new LinkedList();

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public SimpleMemoryLog()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void output(String s)
  {
    if (s==null)
      return;
    String[] lines = StringUtils.split(s,"\r\n");
    for (int i=0; i<lines.length; i++)
      m_scroll.addLast(lines[i]);

  } // end output

  private final void stackTrace(Throwable t)
  {
    if (t==null)
      return;
    StringWriter wr = new StringWriter();
    PrintWriter pwr = new PrintWriter(wr);
    t.printStackTrace(pwr);
    pwr.flush();
    output(wr.toString());

  } // end stackTrace

  private final void doMessage(String level, Object data)
  {
    StringBuffer buf = new StringBuffer(s_fmt.format(new java.util.Date()));
    buf.append(" ").append(level).append(" ").append(data);
    output(buf.toString());

  } // end doMessage

  /*--------------------------------------------------------------------------------
   * Implementations from interface DynamoLog
   *--------------------------------------------------------------------------------
   */

  public void debug(Object message)
  {
    doMessage(S_DEBUG,message);

  } // end debug

  public void debug(Object message, Throwable t)
  {
    doMessage(S_DEBUG,message);
    stackTrace(t);

  } // end debug

  public void error(Object message)
  {
    doMessage(S_ERROR,message);

  } // end error

  public void error(Object message, Throwable t)
  {
    doMessage(S_ERROR,message);
    stackTrace(t);

  } // end error

  public void fatal(Object message)
  {
    doMessage(S_FATAL,message);

  } // end fatal

  public void fatal(Object message, Throwable t)
  {
    doMessage(S_FATAL,message);
    stackTrace(t);

  } // end fatal

  public void info(Object message)
  {
    doMessage(S_INFO,message);

  } // end info

  public void info(Object message, Throwable t)
  {
    doMessage(S_INFO,message);
    stackTrace(t);

  } // end info

  public void warn(Object message)
  {
    doMessage(S_WARN,message);

  } // end warn

  public void warn(Object message, Throwable t)
  {
    doMessage(S_WARN,message);
    stackTrace(t);

  } // end warn

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public List getLog()
  {
    ArrayList rc = new ArrayList(m_scroll);
    return Collections.unmodifiableList(rc);

  } // end getLog

  public void clear()
  {
    m_scroll.clear();

  } // end clear

} // end class SimpleMemoryLog
