Logo Search packages:      
Sourcecode: weka version File versions

Debug.java

/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Debug.java
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 */

package weka.core;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * A helper class for debug output, logging, clocking, etc.
 * 
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision: 1.8 $
 */
00045 public class Debug
  implements Serializable, RevisionHandler {

  /** for serialization */
00049   private static final long serialVersionUID = 66171861743328020L;
  
  /** the log level All */
00052   public final static Level ALL = Level.ALL;
  /** the log level Vonfig */
00054   public final static Level CONFIG = Level.CONFIG;
  /** the log level Fine */
00056   public final static Level FINE = Level.FINE;
  /** the log level Finer */
00058   public final static Level FINER = Level.FINER;
  /** the log level Finest */
00060   public final static Level FINEST = Level.FINEST;
  /** the log level Info */
00062   public final static Level INFO = Level.INFO;
  /** the log level Off - i.e., no logging */
00064   public final static Level OFF = Level.OFF;
  /** the log level Severe */
00066   public final static Level SEVERE = Level.SEVERE;
  /** the log level Warning */
00068   public final static Level WARNING = Level.WARNING;

  /** whether logging is enabled */
00071   protected boolean m_Enabled = true;
  
  /** for logging */
00074   protected Log m_Log;
  
  /** for clocking */
00077   protected Clock m_Clock = new Clock();
  
  /**
   * A little helper class for clocking and outputting times. It measures the
   * CPU time if possible, otherwise it's just based on the system time. In 
   * case one just wants to measure time (e.g., database queries don't take up
   * much CPU time, but still might take a long time to finish), then one can
   * disable the use of CPU time as well.
   *
   * @author FracPete (fracpete at waikato dot ac dot nz)
   * @version $Revision: 1.8 $ 
   * @see ThreadMXBean#isThreadCpuTimeEnabled()
   */
00090   public static class Clock 
    implements Serializable, RevisionHandler {
    
    /** for serialization */
00094     private static final long serialVersionUID = 4622161807307942201L;
    
    /** the output format in milli-seconds */
00097     public final static int FORMAT_MILLISECONDS = 0;
    
    /** the output format in seconds, with fraction of msecs */
00100     public final static int FORMAT_SECONDS = 1;
    
    /** the output format in hours:minutes:seconds, with fraction of msecs */
00103     public final static int FORMAT_HHMMSS = 2;
    
    /** the output formats */
00106     public static final Tag[] TAGS_FORMAT = {
      new Tag(FORMAT_MILLISECONDS, "milli-seconds"),
      new Tag(FORMAT_SECONDS, "seconds"),
      new Tag(FORMAT_HHMMSS, "hh:mm:ss")
    };
    
    /** the format of the output */
00113     public int m_OutputFormat = FORMAT_SECONDS;
    
    /** the start time */
00116     protected long m_Start;
    
    /** the end time */
00119     protected long m_Stop;
    
    /** whether the time is still clocked */
00122     protected boolean m_Running;
    
    /** the thread ID */
00125     protected long m_ThreadID;
    
    /** whether the system can measure the CPU time */
00128     protected boolean m_CanMeasureCpuTime;
    
    /** whether to use the CPU time (by default TRUE) */
00131     protected boolean m_UseCpuTime;
    
    /** the thread monitor, if the system can measure the CPU time */
00134     protected transient ThreadMXBean m_ThreadMonitor;
    
    /**
     * automatically starts the clock with FORMAT_SECONDS format and CPU
     * time if available
     * 
     * @see       #m_OutputFormat
     */
00142     public Clock() {
      this(true);
    }
    
    /**
     * automatically starts the clock with the given output format and CPU
     * time if available
     * 
     * @param format    the output format
     * @see       #m_OutputFormat
     */
00153     public Clock(int format) {
      this(true, format);
    }
    
    /**
     * starts the clock depending on <code>start</code> immediately with the
     * FORMAT_SECONDS output format and CPU time if available
     * 
     * @param start     whether to start the clock immediately
     * @see       #m_OutputFormat
     */
00164     public Clock(boolean start) {
      this(start, FORMAT_SECONDS);
    }
    
    /**
     * starts the clock depending on <code>start</code> immediately, using
     * CPU time if available
     * 
     * @param start     whether to start the clock immediately
     * @param format    the format
     * @see       #m_OutputFormat
     */
00176     public Clock(boolean start, int format) {
      m_Running    = false;
      m_Start      = 0;
      m_Stop       = 0;
      m_UseCpuTime = true;
      setOutputFormat(format);

      if (start)
      start();
    }
    
    /**
     * initializes the clocking, ensure to get the correct thread ID.
     */
00190     protected void init() {
      m_ThreadMonitor = null;
      m_ThreadMonitor = getThreadMonitor();

      // can we measure cpu time?
      m_CanMeasureCpuTime = m_ThreadMonitor.isThreadCpuTimeSupported();
    }
    
    /**
     * whether the measurement is based on the msecs returned from the System
     * class or on the more accurate CPU time. Also depends on whether the 
     * usage of the CPU time was disabled or enabled.
     * 
     * @return          true if the more accurate CPU time of the thread is 
     *                  used and the use of CPU time hasn't been disabled
     * @see System#currentTimeMillis()
     * @see ThreadMXBean#isThreadCpuTimeEnabled()
     * @see #getUseCpuTime()
     */
00209     public boolean isCpuTime() {
      return m_UseCpuTime && m_CanMeasureCpuTime;
    }

    /**
     * enables/disables the use of CPU time (if measurement of CPU time is 
     * available). The actual use of CPU time still depends on whether the 
     * system supports it. Resets the current timer, if running.
     * 
     * @param value     if true the CPU time is used (if possible)
     */
00220     public void setUseCpuTime(boolean value) {
      m_UseCpuTime = value;
      
      // we have to re-initialize the start time, otherwise we get bogus
      // results
      if (m_Running) {
      stop();
      start();
      }
    }

    /**
     * returns whether the use of CPU is time is enabled/disabled (regardless
     * whether the system supports it or not)
     * 
     * @return          true the CPU time is used (if possible)
     */
00237     public boolean getUseCpuTime() {
      return m_UseCpuTime;
    }
    
    /**
     * Returns a new thread monitor if the current one is null (e.g., due to
     * serialization) or the currently set one. The thread ID is also updated
     * if necessary.
     * 
     * @return          the thread monitor to use
     */
00248     protected ThreadMXBean getThreadMonitor() {
      if (m_ThreadMonitor == null) {
      m_ThreadMonitor = ManagementFactory.getThreadMXBean();
      if (!m_ThreadMonitor.isThreadCpuTimeEnabled())
        m_ThreadMonitor.setThreadCpuTimeEnabled(true);
      m_ThreadID = Thread.currentThread().getId();
      }
      
      return m_ThreadMonitor;
    }
    
    /**
     * returns the current time in msec
     * 
     * @return          the current time
     */
00264     protected long getCurrentTime() {
      long  result;
      
      if (isCpuTime())
      result = getThreadMonitor().getThreadUserTime(m_ThreadID) / 1000000;
      else
      result = System.currentTimeMillis();
      
      return result;
    }
    
    /**
     * saves the current system time (or CPU time) in msec as start time
     * 
     * @see       #m_Start
     */
00280     public void start() {
      // make sure that we get the right thread ID!
      init();

      m_Start   = getCurrentTime();
      m_Stop    = m_Start;
      m_Running = true;
    }
    
    /**
     * saves the current system (or CPU time) in msec as stop time
     * 
     * @see       #m_Stop
     */
00294     public void stop() {
      m_Stop    = getCurrentTime();
      m_Running = false;
    }
    
    /**
     * returns the start time
     * 
     * @return    the start time
     */
00304     public long getStart() {
      return m_Start;
    }
    
    /**
     * returns the stop time or, if still running, the current time
     * 
     * @return    the stop time
     */
00313     public long getStop() {
      long  result;
      
      if (isRunning())
      result = getCurrentTime();
      else
      result = m_Stop;
      
      return result;
    }
    
    /**
     * whether the time is still being clocked
     * 
     * @return          true if the time is still being clocked
     */
00329     public boolean isRunning() {
      return m_Running;
    }
    
    /**
     * sets the format of the output
     * 
     * @param value       the format of the output
     * @see               #m_OutputFormat
     */
00339     public void setOutputFormat(int value) {
      if (value == FORMAT_MILLISECONDS)
      m_OutputFormat = value;
      else if (value == FORMAT_SECONDS)
      m_OutputFormat = value;
      else if (value == FORMAT_HHMMSS)
      m_OutputFormat = value;
      else
      System.out.println("Format '" + value + "' is not recognized!");
    }
    
    /**
     * returns the output format
     * 
     * @return          the output format
     * @see       #m_OutputFormat
     */
00356     public int getOutputFormat() {
      return m_OutputFormat;
    }
    
    /**
     * returns the elapsed time, getStop() - getStart(), as string
     * 
     * @return    the elapsed time as string
     * @see       #getStart()
     * @see       #getStop()
     */
00367     public String toString() {
      String    result;
      long      elapsed;
      long      hours;
      long      mins;
      long      secs;
      long      msecs;
      
      result  = "";
      elapsed = getStop() - getStart();
      
      switch (getOutputFormat()) {
      case FORMAT_HHMMSS:
        hours   = elapsed / (3600 * 1000);
        elapsed = elapsed % (3600 * 1000);
        mins    = elapsed / (60 * 1000);
        elapsed = elapsed % (60 * 1000);
        secs    = elapsed / 1000;
        msecs   = elapsed % 1000;
        
        if (hours > 0)
          result += "" + hours + ":";
        
        if (mins < 10)
          result += "0" + mins + ":";
        else
          result += ""  + mins + ":";
        
        if (secs < 10)
          result += "0" + secs + ".";
        else
          result += "" + secs + ".";
        
        result += Utils.doubleToString(
            (double) msecs / (double) 1000, 3).replaceAll(".*\\.", "");
        break;
        
      case FORMAT_SECONDS:
        result = Utils.doubleToString((double) elapsed / (double) 1000, 3) + "s";
        break;
        
      case FORMAT_MILLISECONDS:
        result = "" + elapsed + "ms";
        break;
        
      default:
        result = "<unknown time format>";
      }
      
      return result;
    }
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
00424     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }
  
  /**
   * A class that can be used for timestamps in files, The toString() method
   * simply returns the associated Date object in a timestamp format. For
   * formatting options, see java.text.SimpleDateFormat.
   *
   * @author FracPete (fracpete at waikato dot ac dot nz)
   * @version $Revision: 1.8 $ 
   * @see SimpleDateFormat
   */
00438   public static class Timestamp
    implements Serializable, RevisionHandler {
    
    /** for serialization */
00442     private static final long serialVersionUID = -6099868388466922753L;

    /** the default format */
00445     public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    /** the actual date */
00448     protected Date m_Stamp;
    
    /** the format of the timestamp */
00451     protected String m_Format;
    
    /** handles the format of the output */
00454     protected SimpleDateFormat m_Formatter;
    
    /**
     * creates a timestamp with the current date and time and the default
     * format.
     */
00460     public Timestamp() {
      this(DEFAULT_FORMAT);
    }
    
    /**
     * creates a timestamp with the current date and time and the specified
     * format.
     * 
     * @param format    the format of the timestamp
     * @see       SimpleDateFormat
     */
00471     public Timestamp(String format) {
      this(new Date(), format);
    }
    
    /**
     * creates a timestamp with the given date and the default format.
     * 
     * @param stamp     the associated date/time for the timestamp
     */
00480     public Timestamp(Date stamp) {
      this(stamp, DEFAULT_FORMAT);
    }
    
    /**
     * creates a timestamp with the given date and format.
     * 
     * @param stamp     the associated date/time for the timestamp
     * @param format    the format of the timestamp
     * @see       SimpleDateFormat
     */
00491     public Timestamp(Date stamp, String format) {
      super();
      
      m_Stamp = stamp;
      setFormat(format);
    }
    
    /**
     * sets the format for the timestamp
     * 
     * @param value     the format string
     * @see       SimpleDateFormat
     */
00504     public void setFormat(String value) {
      try {
      m_Formatter = new SimpleDateFormat(value);
      m_Format    = value;
      }
      catch (Exception e) {
      m_Formatter = new SimpleDateFormat(DEFAULT_FORMAT);
      m_Format    = DEFAULT_FORMAT;
      }
    }
    
    /**
     * returns the current timestamp format
     * 
     * @return          the current format
     */
00520     public String getFormat() {
      return m_Format;
    }
    
    /**
     * returns the associated date/time
     * 
     * @return          the timestamp value
     */
00529     public Date getStamp() {
      return m_Stamp;
    }
    
    /**
     * returns the timestamp as string in the specified format
     * 
     * @return          the timestamp as string
     */
00538     public String toString() {
      return m_Formatter.format(getStamp());
    }
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
00547     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }
  
  /**
   * A little, simple helper class for logging stuff. Uses simple file access
   * and not the java.util.logging stuff (see Log for that). Uses the 
   * writeToFile methods of the Debug class.
   * 
   * @see Debug.Log
   * @see Debug#writeToFile(String, String)
   * @see Debug#writeToFile(String, String, boolean)
   */
00561   public static class SimpleLog 
    implements Serializable, RevisionHandler {
    
    /** for serialization */
00565     private static final long serialVersionUID = -2671928223819510830L;
    
    /** the file to write to (if null then only stdout is used) */
00568     protected String m_Filename = null;
    
    /**
     * default constructor, uses only stdout
     */
00573     public SimpleLog() {
      this(null);
    }
    
    /**
     * Creates a logger that writes into the specified file. Appends to the 
     * file by default.
     * 
     * @param filename  the file to write to, if null then only stdout is used
     */
00583     public SimpleLog(String filename) {
      this(filename, true);
    }
    
    /**
     * Creates a logger that writes into the specified file. Appends to the 
     * file by default.
     * 
     * @param filename  the file to write to, if null then only stdout is used
     * @param append    if false, the file will be deleted first
     */
00594     public SimpleLog(String filename, boolean append) {
      super();
      
      m_Filename = filename;
      
      Debug.writeToFile(m_Filename, "--> Log started", append);
    }
    
    /**
     * returns the filename of the log, can be null
     * 
     * @return          the filename of the log
     */
00607     public String getFilename() {
      return m_Filename;
    }
    
    /**
     * logs the given message to the file
     * 
     * @param message   the message to log
     */
00616     public void log(String message) {
      String      log;
      
      log = new Timestamp() + " " + message;
      
      if (getFilename() != null)
      Debug.writeToFile(getFilename(), log);

      System.out.println(log);
    }
    
    /**
     * a convenience method for dumping the current system info in the 
     * log file
     * 
     * @see SystemInfo
     */
00633     public void logSystemInfo() {
      log("SystemInfo:\n" + new SystemInfo().toString());
    }
    
    /**
     * returns a string representation of the logger
     * 
     * @return          a string representation of the logger
     */
00642     public String toString() {
      String      result;
      
      result =   "Filename: " + getFilename();
      
      return result;
    }
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
00655     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }
  
  /**
   * A helper class for logging stuff. Uses the java.util.logging
   * package. If this approach seems an "overkill" (it can create quite a few 
   * log files if used in different threads), one can use the 
   * Debug.SimpleLog class.
   *
   * @author FracPete (fracpete at waikato dot ac dot nz)
   * @version $Revision: 1.8 $ 
   * @see Debug.SimpleLog
   */
00670   public static class Log
    implements Serializable, RevisionHandler {
    
    /** for serialization */
00674     private static final long serialVersionUID = 1458435732111675823L;

    /** the actual logger, if null only stdout is used */
00677     protected transient Logger m_Logger = null;
    
    /** the filename, if any */
00680     protected String m_Filename = null;
    
    /** the size of the file (in bytes) */
00683     protected int m_Size;
    
    /** the number of files for rotating the logs */
00686     protected int m_NumFiles;

    /** whether the initialization of the logger failed */
00689     protected boolean m_LoggerInitFailed = false;
    
    /**
     * default constructor, uses only stdout
     */
00694     public Log() {
      this(null);
    }
    
    /**
     * creates a logger that logs into the specified file, if null then only
     * stdout is used. It uses 1,000,000 bytes for file size and 1 file.
     * 
     * @param filename  the file to log into
     */
00704     public Log(String filename) {
      this(filename, 1000000, 1);
    }
    
    /**
     * creates a logger that logs into the specified file, if null then only
     * stdout is used.
     * 
     * @param filename  the file to log into
     * @param size      the size of the files in bytes
     * @param numFiles  the number of files for rotating
     */
00716     public Log(String filename, int size, int numFiles) {
      m_Filename = filename;
      m_Size     = size;
      m_NumFiles = numFiles;
    }
    
    /**
     * initializes and returns the logger if necessary (e.g., due to 
     * serialization).
     * 
     * @return          the logger, can be null, e.g., if no filename provided
     */
00728     protected Logger getLogger() {
      if ( (m_Logger == null) && (!m_LoggerInitFailed) ) {
      if (m_Filename != null) {
        m_Logger = Logger.getLogger(m_Filename);
        Handler fh = null;
        try{           
          fh = new FileHandler(m_Filename, m_Size, m_NumFiles);
          fh.setFormatter(new SimpleFormatter());
          m_Logger.addHandler(fh);      
          m_LoggerInitFailed = false;
        }
        catch(Exception e) {
          System.out.println("Cannot init fileHandler for logger:" + e.toString());
          m_Logger = null;
          m_LoggerInitFailed = true;
        }  
      }
      }
      
      return m_Logger;
    }
    
    /**
     * turns the string representing a level, e.g., "FINE" or "ALL" into
     * the corresponding level (case-insensitive). The default is ALL.
     * 
     * @param level     the string to return a level for
     * @return          the corresponding level or the default
     */
00757     public static Level stringToLevel(String level) {
      Level result;
      
      if (level.equalsIgnoreCase("ALL"))
        result = ALL;
      else if (level.equalsIgnoreCase("CONFIG"))
        result = CONFIG;
      else if (level.equalsIgnoreCase("FINE"))
        result = FINE;
      else if (level.equalsIgnoreCase("FINER"))
        result = FINER;
      else if (level.equalsIgnoreCase("FINEST"))
        result = FINEST;
      else if (level.equalsIgnoreCase("INFO"))
        result = INFO;
      else if (level.equalsIgnoreCase("OFF"))
        result = OFF;
      else if (level.equalsIgnoreCase("SEVERE"))
        result = SEVERE;
      else if (level.equalsIgnoreCase("WARNING"))
        result = WARNING;
      else
        result = ALL;
      
      return result;
    }
    
    /**
     * returns the filename of the log, can be null
     * 
     * @return          the filename of the log
     */
00789     public String getFilename() {
      return m_Filename;
    }
    
    /**
     * returns the size of the files
     * 
     * @return          the size of a file
     */
00798     public int getSize() {
      return m_Size;
    }
    
    /**
     * returns the number of files being used
     * 
     * @return          the number of files
     */
00807     public int getNumFiles() {
      return m_NumFiles;
    }

    /**
     * logs the given message
     * 
     * @param level     the level of severity
     * @param message   the message to log
     */
00817     public void log(Level level, String message) {
      log(level, "", message);
    }
    
    /**
     * prints the given message with the specified level
     * 
     * @param level     the level of logging
     * @param sourceclass     the class that logs the message
     * @param message   the message to print
     */
00828     public void log(Level level, String sourceclass, String message) {
      log(level, sourceclass, "", message);
    }
    
    /**
     * prints the given message with the specified level
     * 
     * @param level           the level of logging
     * @param sourceclass           the class that logs the message
     * @param sourcemethod    the method that logs the message
     * @param message         the message to print
     */
00840     public void log(Level level, String sourceclass, String sourcemethod, String message) {
      Logger      logger;
      
      logger = getLogger();
      
      if (logger != null)
        logger.logp(level, sourceclass, sourcemethod, message);
      else
      System.out.println(message);
    }
    
    /**
     * a convenience method for dumping the current system info in the 
     * log file
     * 
     * @see SystemInfo
     */
00857     public void logSystemInfo() {
      log(INFO, "SystemInfo:\n" + new SystemInfo().toString());
    }
    
    /**
     * returns a string representation of the logger
     * 
     * @return          a string representation of the logger
     */
00866     public String toString() {
      String      result;
      
      result =   "Filename: " + getFilename() + ", "
                   + "Size: " + getSize() + ", "
                   + "# Files: " + getNumFiles();
      
      return result;
    }
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
00881     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }

  /**
   * This extended Random class enables one to print the generated random
   * numbers etc., before they are returned. It can either use stdout (default)
   * for outputting the logging information or a Log object (level is then 
   * INFO).
   *
   * @author  FracPete (fracpete at waikato dot ac dot nz)
   * @version $Revision: 1.8 $
   */
00895   public static class Random
    extends java.util.Random
    implements Serializable, RevisionHandler {

    /** for serialization */
00900     private static final long serialVersionUID = 1256846887618333956L;

    /** whether to output debug information */
00903     protected boolean m_Debug = false;

    /** the unique ID for this number generator */
00906     protected long m_ID;
    
    /** for keeping track of unique IDs */
00909     protected static long m_CurrentID;
    
    /** the log to use for outputting the data, otherwise just stdout */
00912     protected Log m_Log = null;
    
    /**
     * Creates a new random number generator. With no debugging.
     */
00917     public Random() {
      this(false);
    }

    /**
     * Creates a new random number generator using a single long seed.
     * With no debugging
     * 
     * @param seed      the seed value
     */
00927     public Random(long seed) {
      this(seed, false);
    }

    /**
     * Creates a new random number generator. With optional debugging.
     * 
     * @param debug     if true, debugging output is enabled
     */
00936     public Random(boolean debug) {
      super();
      setDebug(debug);
      m_ID = nextID();
      if (getDebug())
        printStackTrace();
    }

    /**
     * Creates a new random number generator using a single long seed.
     * With optional debugging
     * 
     * @param seed      the seed value
     * @param debug     if true, debugging output is enabled
     */
00951     public Random(long seed, boolean debug) {
      super(seed);
      setDebug(debug);
      m_ID = nextID();
      if (getDebug())
        printStackTrace();
    }

    /**
     * sets whether to print the generated random values or not
     * 
     * @param value     if true debugging output is enabled
     */
00964     public void setDebug(boolean value) {
      m_Debug = value;
    }

    /**
     * returns whether to print the generated random values or not
     * 
     * @return          true if debugging output is enabled
     */
00973     public boolean getDebug() {
      return m_Debug;
    }
    
    /**
     * the log to use, if it is null then stdout is used
     * 
     * @param value     the log to use
     */
00982     public void setLog(Log value) {
      m_Log = value;
    }
    
    /**
     * the currently used log, if null then stdout is used for outputting
     * the debugging information
     * 
     * @return          the log, can be null
     */
00992     public Log getLog() {
      return m_Log;
    }

    /**
     * returns the next unique ID for a number generator
     * 
     * @return          the next unique ID
     */
01001     protected static long nextID() {
      m_CurrentID++;
      
      return m_CurrentID;
    }

    /**
     * returns the unique ID of this number generator
     * 
     * @return          the unique ID of this number generator
     */
01012     public long getID() {
      return m_ID;
    }

    /**
     * prints the given message only if m_Debug is TRUE
     * 
     * @param msg the message to print
     * @see             #m_Debug
     */
01022     protected void println(String msg) {
      if (getDebug()) {
      if (getLog() != null)
        getLog().log(Level.INFO, m_ID + ": " + msg);
      else
        System.out.println(m_ID + ": " + msg);
      }
    }

    /**
     * prints the current stacktrace
     */
01034     public void printStackTrace() {
      Throwable         t;
      StringWriter      writer;

      writer = new StringWriter();
      
      // generate stacktrace
      t = new Throwable();
      t.fillInStackTrace();
      t.printStackTrace(new PrintWriter(writer));

      println(writer.toString());
    }

    /**
     * Returns the next pseudorandom, uniformly distributed boolean value from
     * this random number generator's sequence.
     * 
     * @return          random boolean
     */
01054     public boolean nextBoolean() {
      boolean result = super.nextBoolean();
      println("nextBoolean=" + result);
      return result;
    }

    /**
     * Generates random bytes and places them into a user-supplied byte array.
     * 
     * @param bytes     array to fill with random bytes
     */
01065     public void nextBytes(byte[] bytes) {
      super.nextBytes(bytes);
      println("nextBytes=" + Utils.arrayToString(bytes));
    }

    /**
     * Returns the next pseudorandom, uniformly distributed double value between
     * 0.0 and 1.0 from this random number generator's sequence.
     * 
     * @return          random double
     */
01076     public double nextDouble() {
      double result = super.nextDouble();
      println("nextDouble=" + result);
      return result;
    }

    /**
     * Returns the next pseudorandom, uniformly distributed float  value between
     * 0.0 and 1.0 from this random number generator's sequence.
     * 
     * @return          random float
     */
01088     public float nextFloat() {
      float result = super.nextFloat();
      println("nextFloat=" + result);
      return result;
    }

    /**
     * Returns the next pseudorandom, Gaussian ("normally") distributed double
     * value with mean 0.0 and standard deviation 1.0 from this random number
     * generator's sequence.
     * 
     * @return          random double, gaussian distributed
     */
01101     public double nextGaussian() {
      double result = super.nextGaussian();
      println("nextGaussian=" + result);
      return result;
    }

    /**
     * Returns the next pseudorandom, uniformly distributed int  value from this
     * random number generator's sequence.
     * 
     * @return          random int
     */
01113     public int nextInt() {
      int result = super.nextInt();
      println("nextInt=" + result);
      return result;
    }

    /**
     * Returns a pseudorandom, uniformly distributed int value between 0
     * (inclusive) and the specified value (exclusive), drawn from this random
     * number generator's sequence.
     * 
     * @param n         the upper limit (exclusive)
     * @return          random int
     */
01127     public int nextInt(int n) {
      int result = super.nextInt(n);
      println("nextInt(" + n + ")=" + result);
      return result;
    }

    /**
     * Returns the next pseudorandom, uniformly distributed long  value from this
     * random number generator's sequence.
     * 
     * @return          random long
     */
01139     public long nextLong() {
      long result = super.nextLong();
      println("nextLong=" + result);
      return result;
    }

    /**
     * Sets the seed of this random number generator using a single long seed.
     * 
     * @param seed      the seed value
     */
01150     public void setSeed(long seed) {
      super.setSeed(seed);
      println("setSeed(" + seed + ")");
    }

    /**
     * returns a string representation of this number generator
     * 
     * @return          a string representation
     */
01160     public String toString() {
      return this.getClass().getName() + ": " + getID();
    }
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
01169     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }
  /**
   * contains debug methods
   *
   * @author Gabi Schmidberger (gabi at cs dot waikato dot ac dot nz)
   * @version $Revision: 1.8 $
   */
01179   public static class DBO 
    implements Serializable, RevisionHandler {

    /** for serialization */
01183     static final long serialVersionUID = -5245628124742606784L;  

    /** enables/disables output of debug information */
01186     public boolean m_verboseOn = false;

    /** range of outputtyp */
01189     public Range m_outputTypes = new Range();

    /** 
     * Set the verbose on flag on
     */
01194     public void setVerboseOn() {
      m_verboseOn = true;
    }

    /** 
     * Initialize ranges, upper limit must be set
     * 
     * @param upper upper limit
     */
01203     public void initializeRanges(int upper) {
      m_outputTypes.setUpper(upper);
    }

    /**
     * Return true if the outputtype is set
     * 
     * @param num value that is reserved for a specific outputtype
     * @return return true if the output type is set
     */
01213     public boolean outputTypeSet(int num) {
      return (m_outputTypes.isInRange(num));
    }

     /**
     * Return true if the debug level is set
     * same method as outpuTypeSet but better name
     * 
     * @param num value that is reserved for a specific outputtype
     * @return return true if the debug level is set
     */
01224     public boolean dl(int num) {
      return (outputTypeSet(num));
    }

   /**
     * Switches the outputs on that are requested from the option O
     * 
     * @param list list of integers, all are used for an output type
     */
01233     public void setOutputTypes(String list) {
      if (list.length() > 0) {
        m_verboseOn = true; 

        m_outputTypes.setRanges(list);
        m_outputTypes.setUpper(30);
      }
    }

    /**
     * Gets the current output type selection
     *
     * @return a string containing a comma separated list of ranges
     */
01247     public String getOutputTypes() {
      return m_outputTypes.getRanges();
    }

    /**
     * prints out text + endofline if verbose is on.
     * helps to make debug output commands more visible in text
     * 
     * @param text the text to print
     */
01257     public void dpln(String text) {
      if (m_verboseOn) {
        System.out.println(text);
      }
    } 

    /**
     * prints out text + endofline but only if parameter debug type is set.
     * helps to make debug output commands more visible in text
     *
     * @param debugType the type of the output
     * @param text the text to print
     */
01270     public void dpln(int debugType, String text) {
      if (outputTypeSet(debugType)) {
        System.out.println(text);
      }
    } 

     /**
     * prints out text  if verbose is on.
     * helps to make debug output commands more visible in text
     * 
     * @param text the text to print
     */
01282     public void dp(String text) {
      if (m_verboseOn) {
        System.out.print(text);
      }
    } 

   /**
     * prints out text but only if debug level is set.
     * helps to make debug output commands more visible in text
     *
     * @param debugType the type of the output
     * @param text the text to print
     */
01295     public void dp(int debugType, String text) {
     if (outputTypeSet(debugType)) {
        System.out.print(text);
      }
    } 

    /**
     * prints out text + endofline.
     * helps to make debug output commands more visible in text
     * 
     * @param text the text to print
     */
01307     public static void pln(String text) {
      System.out.println(text);
    } 

    /**
     * prints out text.
     * helps to make debug output commands more visible in text
     * 
     * @param text the text to print
     */
01317     public static void p (String text) {
      System.out.print(text);
    } 
    
    /**
     * Returns the revision string.
     * 
     * @return          the revision
     */
01326     public String getRevision() {
      return RevisionUtils.extract("$Revision: 1.8 $");
    }
  }
  
  /**
   * default constructor, prints only to stdout
   */
01334   public Debug() {
    this(null);
  }
  
  /**
   * logs the output to the specified file (and stdout). Size is 1,000,000 bytes 
   * and 1 file.
   * 
   * @param filename    the name of the log
   */
01344   public Debug(String filename) {
    this(filename, 1000000, 1);
  }
  
  /**
   * logs the output
   * 
   * @param filename    the name of the log
   * @param size  the size of the files in bytes
   * @param numFiles    the number of files for rotating
   */
01355   public Debug(String filename, int size, int numFiles) {
    super();
    
    m_Log = newLog(filename, size, numFiles);
  }
  
  /**
   * turns the string representing a level, e.g., "FINE" or "ALL" into
   * the corresponding level (case-insensitive). The default is ALL.
   * 
   * @param level the string to return a level for
   * @return            the corresponding level or the default
   */
01368   public static Level stringToLevel(String level) {
    return Log.stringToLevel(level);
  }
  
  /**
   * returns a new Log instance
   * 
   * @param filename    the name of the log
   * @param size  the size of the files in bytes
   * @param numFiles    the number of files for rotating
   * @return            the log instance
   */
01380   public static Log newLog(String filename, int size, int numFiles) {
    return new Log(filename, size, numFiles);
  }
  
  /**
   * prints the given message with level INFO
   * 
   * @param message     the message to print
   */
01389   public void log(String message) {
    log(INFO, message);
  }
  
  /**
   * prints the given message with the specified level and an empty sourceclass
   * 
   * @param level the level of logging
   * @param message     the message to print
   */
01399   public void log(Level level, String message) {
    log(level, "", message);
  }
  
  /**
   * prints the given message with the specified level
   * 
   * @param level the level of logging
   * @param sourceclass the class that logs the message
   * @param message     the message to print
   */
01410   public void log(Level level, String sourceclass, String message) {
    log(level, sourceclass, "", message);
  }
  
  /**
   * prints the given message with the specified level
   * 
   * @param level       the level of logging
   * @param sourceclass       the class that logs the message
   * @param sourcemethod      the method that logs the message
   * @param message           the message to print
   */
01422   public void log(Level level, String sourceclass, String sourcemethod, String message) {
    if (getEnabled())
      m_Log.log(level, sourceclass, sourcemethod, message);
  }
  
  /**
   * sets whether the logging is enabled or not
   * 
   * @param value if true logging will be enabled
   */
01432   public void setEnabled(boolean value) {
    m_Enabled = value;
  }
  
  /**
   * returns whether the logging is enabled
   * 
   * @return            true if the logging is enabled
   */
01441   public boolean getEnabled() {
    return m_Enabled;
  }
  
  /**
   * returns a new instance of a clock
   * 
   * @return            a new instance of a Clock
   */
01450   public static Clock newClock() {
    return new Clock();
  }
  
  /**
   * returns the instance of the Clock that is internally used
   * 
   * @return            the clock that's being used
   */
01459   public Clock getClock() {
    return m_Clock;
  }
  
  /**
   * starts the clock
   */
01466   public void startClock() {
    m_Clock.start();
  }
  
  /**
   * stops the clock and prints the message associated with the time, but only
   * if the logging is enabled.
   * 
   * @param message     the message to print
   * @see         #getEnabled()
   */
01477   public void stopClock(String message) {
    log(message + ": " + m_Clock);
  }
  
  /**
   * returns a default debug random object, with no particular seed and 
   * debugging enabled.
   * 
   * @return            a new instance of a Random object
   */
01487   public static java.util.Random newRandom() {
    return new Random(true);
  }
  
  /**
   * returns a debug random object with the specified seed and debugging 
   * enabled.
   * 
   * @param seed  the seed value
   * @return            a new instance of a Random object
   */
01498   public static java.util.Random newRandom(int seed) {
    return new Random(seed, true);
  }

  /**
   * returns a default timestamp for the current date/time
   * 
   * @return            a new timestamp
   */
01507   public static Timestamp newTimestamp() {
    return new Timestamp();
  }
  
  /**
   * returns the system temp directory
   * 
   * @return            the temp directory
   */
01516   public static String getTempDir() {
    return System.getProperty("java.io.tmpdir");
  }
  
  /**
   * returns the home directory of the user
   * 
   * @return            the user's home directory
   */
01525   public static String getHomeDir() {
    return System.getProperty("user.home");
  }
  
  /**
   * returns the current working directory of the user
   * 
   * @return            the user's current working directory
   */
01534   public static String getCurrentDir() {
    return System.getProperty("user.dir");
  }
  
  /**
   * Writes the given object to the specified file. The string representation
   * of the object is appended to the file.
   * 
   * @param filename    the file to write to
   * @param obj         the object to write to the file
   * @return            true if writing was successful
   */
01546   public static boolean writeToFile(String filename, Object obj) {
    return writeToFile(filename, obj, true);
  }
  
  /**
   * Writes the given message to the specified file. The message is appended 
   * to the file.
   * 
   * @param filename    the file to write to
   * @param message     the message to write
   * @return            true if writing was successful
   */
01558   public static boolean writeToFile(String filename, String message) {
    return writeToFile(filename, message, true);
  }
  
  /**
   * Writes the given object to the specified file. The string representation 
   * of the object is either appended or replaces the current content of the 
   * file.
   * 
   * @param filename    the file to write to
   * @param obj         the object to write to the file
   * @param append      whether to append the message or not
   * @return            true if writing was successful
   */
01572   public static boolean writeToFile(String filename, Object obj, boolean append) {
    return writeToFile(filename, obj.toString(), append);
  }
  
  /**
   * Writes the given message to the specified file. The message is either 
   * appended or replaces the current content of the file.
   * 
   * @param filename    the file to write to
   * @param message     the message to write
   * @param append      whether to append the message or not
   * @return            true if writing was successful
   */
01585   public static boolean writeToFile(String filename, String message, boolean append) {
    boolean       result;
    BufferedWriter      writer;
    
    try {
      writer = new BufferedWriter(new FileWriter(filename, append));
      writer.write(message);
      writer.newLine();
      writer.flush();
      writer.close();
      result = true;
    }
    catch (Exception e) {
      result = false;
    }
    
    return result;
  }
  
  /**
   * writes the serialized object to the speicified file
   * 
   * @param filename    the file to serialize the object to
   * @param o           the object to serialize
   * @return            true if writing was successful
   */
01611   public static boolean saveToFile(String filename, Object o) {
    boolean       result;
    
    if (SerializationHelper.isSerializable(o.getClass())) {
      try {
      SerializationHelper.write(filename, o);
      result = true;
      }
      catch (Exception e) {
      result = false;
      }
    }
    else {
      result = false;
    }
    
    return result;
  }
  
  /**
   * deserializes the content of the file and returns it, null if an error
   * occurred.
   * 
   * @param filename    the name of the file to deserialize
   * @return            the deserialized content, null if problem occurred
   */
01637   public static Object loadFromFile(String filename) {
    Object  result;
    
    try {
      result = SerializationHelper.read(filename);
    }
    catch (Exception e) {
      result = null;
    }
    
    return result;
  }
  
  /**
   * Returns the revision string.
   * 
   * @return            the revision
   */
01655   public String getRevision() {
    return RevisionUtils.extract("$Revision: 1.8 $");
  }
}

Generated by  Doxygen 1.6.0   Back to index