📄 debug.java
字号:
/** * 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) */ public static class SimpleLog implements Serializable { /** for serialization */ private static final long serialVersionUID = -2671928223819510830L; /** the file to write to (if null then only stdout is used) */ protected String m_Filename = null; /** * default constructor, uses only stdout */ 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 */ 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 */ 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 */ public String getFilename() { return m_Filename; } /** * logs the given message to the file * * @param message the message to log */ 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 */ public void logSystemInfo() { log("SystemInfo:\n" + new SystemInfo().toString()); } /** * returns a string representation of the logger * * @return a string representation of the logger */ public String toString() { String result; result = "Filename: " + getFilename(); return result; } } /** * 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.7 $ * @see Debug.SimpleLog */ public static class Log implements Serializable { /** for serialization */ private static final long serialVersionUID = 1458435732111675823L; /** the actual logger, if null only stdout is used */ protected transient Logger m_Logger = null; /** the filename, if any */ protected String m_Filename = null; /** the size of the file (in bytes) */ protected int m_Size; /** the number of files for rotating the logs */ protected int m_NumFiles; /** whether the initialization of the logger failed */ protected boolean m_LoggerInitFailed = false; /** * default constructor, uses only stdout */ 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 */ 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 */ 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 */ 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 */ 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 */ public String getFilename() { return m_Filename; } /** * returns the size of the files * * @return the size of a file */ public int getSize() { return m_Size; } /** * returns the number of files being used * * @return the number of files */ public int getNumFiles() { return m_NumFiles; } /** * logs the given message * * @param level the level of severity * @param message the message to log */ 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 */ 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 */ 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 */ public void logSystemInfo() { log(INFO, "SystemInfo:\n" + new SystemInfo().toString()); } /** * returns a string representation of the logger * * @return a string representation of the logger */ public String toString() { String result; result = "Filename: " + getFilename() + ", " + "Size: " + getSize() + ", " + "# Files: " + getNumFiles(); return result; } } /** * 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.7 $ */ public static class Random extends java.util.Random implements Serializable { /** for serialization */ private static final long serialVersionUID = 1256846887618333956L; /** whether to output debug information */ protected boolean m_Debug = false; /** the unique ID for this number generator */ protected long m_ID; /** for keeping track of unique IDs */ protected static long m_CurrentID; /** the log to use for outputting the data, otherwise just stdout */ protected Log m_Log = null; /** * Creates a new random number generator. With no debugging. */ public Random() { this(false); } /** * Creates a new random number generator using a single long seed. * With no debugging * * @param seed the seed value */ public Random(long seed) { this(seed, false); } /** * Creates a new random number generator. With optional debugging. * * @param debug if true, debugging output is enabled */ 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 */ 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 */ 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 */ public boolean getDebug() { return m_Debug; } /** * the log to use, if it is null then stdout is used * * @param value the log to use */ 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 */ public Log getLog() { return m_Log; } /** * returns the next unique ID for a number generator * * @return the next unique ID */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -