/*
** Module   : LogHelper.java
** Abstract : static utility methods to assist logging 
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090212 Static utility methods to assist logging.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.util;

/**
 * Static utility methods to assist logging.
 */
public class LogHelper
{
   /** Platform-specific line separator. */
   private static final String SEP = System.getProperty("line.separator");
   
   /**
    * Instances of this class cannot be created.
    */
   private LogHelper()
   {
   }
   
   /**
    * Dump all details of the throwable into a string. Includes exception type,
    * message, complete stack trace, and chained throwables, if any.
    *
    * @param    t
    *           The throwable to dump.
    * 
    * @return   String containing throwable details.
    */
   public static String dumpThrowable(Throwable t)
   {
      StringBuilder sb = new StringBuilder();
      dumpThrowable(t, sb);
      
      return sb.toString();
   }

   /**
    * Dump all details of the throwable into the given buffer. Includes
    * exception type, message, complete stack trace, and chained throwables,
    * if any.
    *
    * @param    t
    *           The throwable to dump.
    * @param    buf
    *           Buffer into which details are written.
    *
    * @return   The given buffer that was modified. This allows chaining.
    */
   public static StringBuilder dumpThrowable(Throwable t, StringBuilder buf)
   {
      Throwable cause = t;
      
      do
      {
         if (cause != t)
         {
            buf.append("Caused by: ");
         }
         
         buf.append(cause.getClass().getName());
         
         String msg = cause.getMessage();
         
         if (msg != null && msg.trim().length() > 0)
         {
            buf.append(": ");
            buf.append(msg);
         }
         buf.append(SEP);
         
         StackTraceElement[] trace = cause.getStackTrace();
         
         for (int i = 0; i < trace.length; i++)
         {
            buf.append("        at ");
            buf.append(trace[i]);
            buf.append(SEP);
         }
         
         cause = cause.getCause();
      }
      while (cause != null);
      
      return buf;
   }
}
