📄 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.7 $ */public class Debug implements Serializable { /** for serialization */ private static final long serialVersionUID = 66171861743328020L; /** the log level All */ public final static Level ALL = Level.ALL; /** the log level Vonfig */ public final static Level CONFIG = Level.CONFIG; /** the log level Fine */ public final static Level FINE = Level.FINE; /** the log level Finer */ public final static Level FINER = Level.FINER; /** the log level Finest */ public final static Level FINEST = Level.FINEST; /** the log level Info */ public final static Level INFO = Level.INFO; /** the log level Off - i.e., no logging */ public final static Level OFF = Level.OFF; /** the log level Severe */ public final static Level SEVERE = Level.SEVERE; /** the log level Warning */ public final static Level WARNING = Level.WARNING; /** whether logging is enabled */ protected boolean m_Enabled = true; /** for logging */ protected Log m_Log; /** for clocking */ 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.7 $ * @see ThreadMXBean#isThreadCpuTimeEnabled() */ public static class Clock implements Serializable { /** for serialization */ private static final long serialVersionUID = 4622161807307942201L; /** the output format in milli-seconds */ public final static int FORMAT_MILLISECONDS = 0; /** the output format in seconds, with fraction of msecs */ public final static int FORMAT_SECONDS = 1; /** the output format in hours:minutes:seconds, with fraction of msecs */ public final static int FORMAT_HHMMSS = 2; /** the output formats */ 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 */ public int m_OutputFormat = FORMAT_SECONDS; /** the start time */ protected long m_Start; /** the end time */ protected long m_Stop; /** whether the time is still clocked */ protected boolean m_Running; /** the thread ID */ protected long m_ThreadID; /** whether the system can measure the CPU time */ protected boolean m_CanMeasureCpuTime; /** whether to use the CPU time (by default TRUE) */ protected boolean m_UseCpuTime; /** the thread monitor, if the system can measure the CPU time */ protected transient ThreadMXBean m_ThreadMonitor; /** * automatically starts the clock with FORMAT_SECONDS format and CPU * time if available * * @see #m_OutputFormat */ 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 */ 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 */ 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 */ 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. */ 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() */ 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) */ 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) */ 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 */ 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 */ 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 */ 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 */ public void stop() { m_Stop = getCurrentTime(); m_Running = false; } /** * returns the start time * * @return the start time */ public long getStart() { return m_Start; } /** * returns the stop time or, if still running, the current time * * @return the stop time */ 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 */ public boolean isRunning() { return m_Running; } /** * sets the format of the output * * @param value the format of the output * @see #m_OutputFormat */ 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 */ public int getOutputFormat() { return m_OutputFormat; } /** * returns the elapsed time, getStop() - getStart(), as string * * @return the elapsed time as string * @see #getStart() * @see #getStop() */ 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; } } /** * 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.7 $ * @see SimpleDateFormat */ public static class Timestamp implements Serializable { /** for serialization */ private static final long serialVersionUID = -6099868388466922753L; /** the default format */ public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** the actual date */ protected Date m_Stamp; /** the format of the timestamp */ protected String m_Format; /** handles the format of the output */ protected SimpleDateFormat m_Formatter; /** * creates a timestamp with the current date and time and the default * format. */ 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 */ 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 */ 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 */ 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 */ 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 */ public String getFormat() { return m_Format; } /** * returns the associated date/time * * @return the timestamp value */ public Date getStamp() { return m_Stamp; } /** * returns the timestamp as string in the specified format * * @return the timestamp as string */ public String toString() { return m_Formatter.format(getStamp()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -