trunk/com/so/news/Debug.java
author chris <chris@marvin>
Tue, 20 Jan 2009 10:21:03 +0100
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
     1 /*
     2  *   StarOffice News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package com.so.news;
    20 
    21 import java.io.FileOutputStream;
    22 import java.io.IOException;
    23 import java.io.PrintStream;
    24 import java.util.Date;
    25 
    26 /**
    27  * Provides logging and debugging methods.
    28  * @author Christian Lins
    29  */
    30 public class Debug
    31 {
    32   private static Debug instance = null;
    33   
    34   /**
    35    * Returns the singelton instance of this class.
    36    */
    37   public static Debug getInstance()
    38   {
    39     if(instance == null)
    40       instance = new Debug();
    41     
    42     return instance;
    43   }
    44   
    45   private PrintStream out = System.err;
    46   
    47   /**
    48    * This class is a singelton class. The constructor is private to prevent
    49    * the creation of more than one instance.
    50    */
    51   private Debug()
    52   {
    53     try
    54     {
    55       String filename = Config.getInstance().get(Config.CONFIG_N3TPD_LOGFILE, "n3tpd.log");
    56       
    57       this.out = new PrintStream(new FileOutputStream(filename));
    58     }
    59     catch(IOException e)
    60     {
    61       e.printStackTrace();
    62     }
    63   }
    64   
    65   /**
    66    * Returns the debug output PrintStream. By default this is System.err.
    67    */
    68   public PrintStream getStream()
    69   {
    70     return out;
    71   }
    72   
    73   /**
    74    * Writes the given message to the debug output.
    75    * @param msg A String message or an object.
    76    */
    77   public void log(Object msg)
    78   {
    79     log(out, msg);
    80     log(System.out, msg);
    81   }
    82   
    83   /**
    84    * Writes the given debug message to the given PrintStream.
    85    * @param out
    86    * @param msg
    87    */
    88   public void log(PrintStream out, Object msg)
    89   {
    90     out.print(new Date().toString());
    91     out.print(": ");
    92     out.println(msg.toString());
    93     out.flush();
    94   }
    95 }