2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
24
import java.io.BufferedWriter;
25
import java.io.FileWriter;
26
import java.io.PrintWriter;
27
import java.io.Serializable;
28
import java.io.StringWriter;
29
import java.lang.management.ManagementFactory;
30
import java.lang.management.ThreadMXBean;
31
import java.text.SimpleDateFormat;
32
import java.util.Date;
33
import java.util.logging.FileHandler;
34
import java.util.logging.Handler;
35
import java.util.logging.Level;
36
import java.util.logging.Logger;
37
import java.util.logging.SimpleFormatter;
40
* A helper class for debug output, logging, clocking, etc.
42
* @author fracpete (fracpete at waikato dot ac dot nz)
43
* @version $Revision: 1.7 $
46
implements Serializable {
48
/** for serialization */
49
private static final long serialVersionUID = 66171861743328020L;
51
/** the log level All */
52
public final static Level ALL = Level.ALL;
53
/** the log level Vonfig */
54
public final static Level CONFIG = Level.CONFIG;
55
/** the log level Fine */
56
public final static Level FINE = Level.FINE;
57
/** the log level Finer */
58
public final static Level FINER = Level.FINER;
59
/** the log level Finest */
60
public final static Level FINEST = Level.FINEST;
61
/** the log level Info */
62
public final static Level INFO = Level.INFO;
63
/** the log level Off - i.e., no logging */
64
public final static Level OFF = Level.OFF;
65
/** the log level Severe */
66
public final static Level SEVERE = Level.SEVERE;
67
/** the log level Warning */
68
public final static Level WARNING = Level.WARNING;
70
/** whether logging is enabled */
71
protected boolean m_Enabled = true;
77
protected Clock m_Clock = new Clock();
80
* A little helper class for clocking and outputting times. It measures the
81
* CPU time if possible, otherwise it's just based on the system time. In
82
* case one just wants to measure time (e.g., database queries don't take up
83
* much CPU time, but still might take a long time to finish), then one can
84
* disable the use of CPU time as well.
86
* @author FracPete (fracpete at waikato dot ac dot nz)
87
* @version $Revision: 1.7 $
88
* @see ThreadMXBean#isThreadCpuTimeEnabled()
90
public static class Clock
91
implements Serializable {
93
/** for serialization */
94
private static final long serialVersionUID = 4622161807307942201L;
96
/** the output format in milli-seconds */
97
public final static int FORMAT_MILLISECONDS = 0;
99
/** the output format in seconds, with fraction of msecs */
100
public final static int FORMAT_SECONDS = 1;
102
/** the output format in hours:minutes:seconds, with fraction of msecs */
103
public final static int FORMAT_HHMMSS = 2;
105
/** the output formats */
106
public static final Tag[] TAGS_FORMAT = {
107
new Tag(FORMAT_MILLISECONDS, "milli-seconds"),
108
new Tag(FORMAT_SECONDS, "seconds"),
109
new Tag(FORMAT_HHMMSS, "hh:mm:ss")
112
/** the format of the output */
113
public int m_OutputFormat = FORMAT_SECONDS;
115
/** the start time */
116
protected long m_Start;
119
protected long m_Stop;
121
/** whether the time is still clocked */
122
protected boolean m_Running;
125
protected long m_ThreadID;
127
/** whether the system can measure the CPU time */
128
protected boolean m_CanMeasureCpuTime;
130
/** whether to use the CPU time (by default TRUE) */
131
protected boolean m_UseCpuTime;
133
/** the thread monitor, if the system can measure the CPU time */
134
protected transient ThreadMXBean m_ThreadMonitor;
137
* automatically starts the clock with FORMAT_SECONDS format and CPU
140
* @see #m_OutputFormat
147
* automatically starts the clock with the given output format and CPU
150
* @param format the output format
151
* @see #m_OutputFormat
153
public Clock(int format) {
158
* starts the clock depending on <code>start</code> immediately with the
159
* FORMAT_SECONDS output format and CPU time if available
161
* @param start whether to start the clock immediately
162
* @see #m_OutputFormat
164
public Clock(boolean start) {
165
this(start, FORMAT_SECONDS);
169
* starts the clock depending on <code>start</code> immediately, using
170
* CPU time if available
172
* @param start whether to start the clock immediately
173
* @param format the format
174
* @see #m_OutputFormat
176
public Clock(boolean start, int format) {
181
setOutputFormat(format);
188
* initializes the clocking, ensure to get the correct thread ID.
190
protected void init() {
191
m_ThreadMonitor = null;
192
m_ThreadMonitor = getThreadMonitor();
194
// can we measure cpu time?
195
m_CanMeasureCpuTime = m_ThreadMonitor.isThreadCpuTimeSupported();
199
* whether the measurement is based on the msecs returned from the System
200
* class or on the more accurate CPU time. Also depends on whether the
201
* usage of the CPU time was disabled or enabled.
203
* @return true if the more accurate CPU time of the thread is
204
* used and the use of CPU time hasn't been disabled
205
* @see System#currentTimeMillis()
206
* @see ThreadMXBean#isThreadCpuTimeEnabled()
207
* @see #getUseCpuTime()
209
public boolean isCpuTime() {
210
return m_UseCpuTime && m_CanMeasureCpuTime;
214
* enables/disables the use of CPU time (if measurement of CPU time is
215
* available). The actual use of CPU time still depends on whether the
216
* system supports it. Resets the current timer, if running.
218
* @param value if true the CPU time is used (if possible)
220
public void setUseCpuTime(boolean value) {
221
m_UseCpuTime = value;
223
// we have to re-initialize the start time, otherwise we get bogus
232
* returns whether the use of CPU is time is enabled/disabled (regardless
233
* whether the system supports it or not)
235
* @return true the CPU time is used (if possible)
237
public boolean getUseCpuTime() {
242
* Returns a new thread monitor if the current one is null (e.g., due to
243
* serialization) or the currently set one. The thread ID is also updated
246
* @return the thread monitor to use
248
protected ThreadMXBean getThreadMonitor() {
249
if (m_ThreadMonitor == null) {
250
m_ThreadMonitor = ManagementFactory.getThreadMXBean();
251
if (!m_ThreadMonitor.isThreadCpuTimeEnabled())
252
m_ThreadMonitor.setThreadCpuTimeEnabled(true);
253
m_ThreadID = Thread.currentThread().getId();
256
return m_ThreadMonitor;
260
* returns the current time in msec
262
* @return the current time
264
protected long getCurrentTime() {
268
result = getThreadMonitor().getThreadUserTime(m_ThreadID) / 1000000;
270
result = System.currentTimeMillis();
276
* saves the current system time (or CPU time) in msec as start time
280
public void start() {
281
// make sure that we get the right thread ID!
284
m_Start = getCurrentTime();
290
* saves the current system (or CPU time) in msec as stop time
295
m_Stop = getCurrentTime();
300
* returns the start time
302
* @return the start time
304
public long getStart() {
309
* returns the stop time or, if still running, the current time
311
* @return the stop time
313
public long getStop() {
317
result = getCurrentTime();
325
* whether the time is still being clocked
327
* @return true if the time is still being clocked
329
public boolean isRunning() {
334
* sets the format of the output
336
* @param value the format of the output
337
* @see #m_OutputFormat
339
public void setOutputFormat(int value) {
340
if (value == FORMAT_MILLISECONDS)
341
m_OutputFormat = value;
342
else if (value == FORMAT_SECONDS)
343
m_OutputFormat = value;
344
else if (value == FORMAT_HHMMSS)
345
m_OutputFormat = value;
347
System.out.println("Format '" + value + "' is not recognized!");
351
* returns the output format
353
* @return the output format
354
* @see #m_OutputFormat
356
public int getOutputFormat() {
357
return m_OutputFormat;
361
* returns the elapsed time, getStop() - getStart(), as string
363
* @return the elapsed time as string
367
public String toString() {
376
elapsed = getStop() - getStart();
378
switch (getOutputFormat()) {
380
hours = elapsed / (3600 * 1000);
381
elapsed = elapsed % (3600 * 1000);
382
mins = elapsed / (60 * 1000);
383
elapsed = elapsed % (60 * 1000);
384
secs = elapsed / 1000;
385
msecs = elapsed % 1000;
388
result += "" + hours + ":";
391
result += "0" + mins + ":";
393
result += "" + mins + ":";
396
result += "0" + secs + ".";
398
result += "" + secs + ".";
400
result += Utils.doubleToString(
401
(double) msecs / (double) 1000, 3).replaceAll(".*\\.", "");
405
result = Utils.doubleToString((double) elapsed / (double) 1000, 3) + "s";
408
case FORMAT_MILLISECONDS:
409
result = "" + elapsed + "ms";
413
result = "<unknown time format>";
421
* A class that can be used for timestamps in files, The toString() method
422
* simply returns the associated Date object in a timestamp format. For
423
* formatting options, see java.text.SimpleDateFormat.
425
* @author FracPete (fracpete at waikato dot ac dot nz)
426
* @version $Revision: 1.7 $
427
* @see SimpleDateFormat
429
public static class Timestamp
430
implements Serializable {
432
/** for serialization */
433
private static final long serialVersionUID = -6099868388466922753L;
435
/** the default format */
436
public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
438
/** the actual date */
439
protected Date m_Stamp;
441
/** the format of the timestamp */
442
protected String m_Format;
444
/** handles the format of the output */
445
protected SimpleDateFormat m_Formatter;
448
* creates a timestamp with the current date and time and the default
452
this(DEFAULT_FORMAT);
456
* creates a timestamp with the current date and time and the specified
459
* @param format the format of the timestamp
460
* @see SimpleDateFormat
462
public Timestamp(String format) {
463
this(new Date(), format);
467
* creates a timestamp with the given date and the default format.
469
* @param stamp the associated date/time for the timestamp
471
public Timestamp(Date stamp) {
472
this(stamp, DEFAULT_FORMAT);
476
* creates a timestamp with the given date and format.
478
* @param stamp the associated date/time for the timestamp
479
* @param format the format of the timestamp
480
* @see SimpleDateFormat
482
public Timestamp(Date stamp, String format) {
490
* sets the format for the timestamp
492
* @param value the format string
493
* @see SimpleDateFormat
495
public void setFormat(String value) {
497
m_Formatter = new SimpleDateFormat(value);
500
catch (Exception e) {
501
m_Formatter = new SimpleDateFormat(DEFAULT_FORMAT);
502
m_Format = DEFAULT_FORMAT;
507
* returns the current timestamp format
509
* @return the current format
511
public String getFormat() {
516
* returns the associated date/time
518
* @return the timestamp value
520
public Date getStamp() {
525
* returns the timestamp as string in the specified format
527
* @return the timestamp as string
529
public String toString() {
530
return m_Formatter.format(getStamp());
535
* A little, simple helper class for logging stuff. Uses simple file access
536
* and not the java.util.logging stuff (see Log for that). Uses the
537
* writeToFile methods of the Debug class.
540
* @see Debug#writeToFile(String, String)
541
* @see Debug#writeToFile(String, String, boolean)
543
public static class SimpleLog
544
implements Serializable {
546
/** for serialization */
547
private static final long serialVersionUID = -2671928223819510830L;
549
/** the file to write to (if null then only stdout is used) */
550
protected String m_Filename = null;
553
* default constructor, uses only stdout
560
* Creates a logger that writes into the specified file. Appends to the
563
* @param filename the file to write to, if null then only stdout is used
565
public SimpleLog(String filename) {
566
this(filename, true);
570
* Creates a logger that writes into the specified file. Appends to the
573
* @param filename the file to write to, if null then only stdout is used
574
* @param append if false, the file will be deleted first
576
public SimpleLog(String filename, boolean append) {
579
m_Filename = filename;
581
Debug.writeToFile(m_Filename, "--> Log started", append);
585
* returns the filename of the log, can be null
587
* @return the filename of the log
589
public String getFilename() {
594
* logs the given message to the file
596
* @param message the message to log
598
public void log(String message) {
601
log = new Timestamp() + " " + message;
603
if (getFilename() != null)
604
Debug.writeToFile(getFilename(), log);
606
System.out.println(log);
610
* a convenience method for dumping the current system info in the
615
public void logSystemInfo() {
616
log("SystemInfo:\n" + new SystemInfo().toString());
620
* returns a string representation of the logger
622
* @return a string representation of the logger
624
public String toString() {
627
result = "Filename: " + getFilename();
634
* A helper class for logging stuff. Uses the java.util.logging
635
* package. If this approach seems an "overkill" (it can create quite a few
636
* log files if used in different threads), one can use the
637
* Debug.SimpleLog class.
639
* @author FracPete (fracpete at waikato dot ac dot nz)
640
* @version $Revision: 1.7 $
641
* @see Debug.SimpleLog
643
public static class Log
644
implements Serializable {
646
/** for serialization */
647
private static final long serialVersionUID = 1458435732111675823L;
649
/** the actual logger, if null only stdout is used */
650
protected transient Logger m_Logger = null;
652
/** the filename, if any */
653
protected String m_Filename = null;
655
/** the size of the file (in bytes) */
656
protected int m_Size;
658
/** the number of files for rotating the logs */
659
protected int m_NumFiles;
661
/** whether the initialization of the logger failed */
662
protected boolean m_LoggerInitFailed = false;
665
* default constructor, uses only stdout
672
* creates a logger that logs into the specified file, if null then only
673
* stdout is used. It uses 1,000,000 bytes for file size and 1 file.
675
* @param filename the file to log into
677
public Log(String filename) {
678
this(filename, 1000000, 1);
682
* creates a logger that logs into the specified file, if null then only
685
* @param filename the file to log into
686
* @param size the size of the files in bytes
687
* @param numFiles the number of files for rotating
689
public Log(String filename, int size, int numFiles) {
690
m_Filename = filename;
692
m_NumFiles = numFiles;
696
* initializes and returns the logger if necessary (e.g., due to
699
* @return the logger, can be null, e.g., if no filename provided
701
protected Logger getLogger() {
702
if ( (m_Logger == null) && (!m_LoggerInitFailed) ) {
703
if (m_Filename != null) {
704
m_Logger = Logger.getLogger(m_Filename);
707
fh = new FileHandler(m_Filename, m_Size, m_NumFiles);
708
fh.setFormatter(new SimpleFormatter());
709
m_Logger.addHandler(fh);
710
m_LoggerInitFailed = false;
713
System.out.println("Cannot init fileHandler for logger:" + e.toString());
715
m_LoggerInitFailed = true;
724
* turns the string representing a level, e.g., "FINE" or "ALL" into
725
* the corresponding level (case-insensitive). The default is ALL.
727
* @param level the string to return a level for
728
* @return the corresponding level or the default
730
public static Level stringToLevel(String level) {
733
if (level.equalsIgnoreCase("ALL"))
735
else if (level.equalsIgnoreCase("CONFIG"))
737
else if (level.equalsIgnoreCase("FINE"))
739
else if (level.equalsIgnoreCase("FINER"))
741
else if (level.equalsIgnoreCase("FINEST"))
743
else if (level.equalsIgnoreCase("INFO"))
745
else if (level.equalsIgnoreCase("OFF"))
747
else if (level.equalsIgnoreCase("SEVERE"))
749
else if (level.equalsIgnoreCase("WARNING"))
758
* returns the filename of the log, can be null
760
* @return the filename of the log
762
public String getFilename() {
767
* returns the size of the files
769
* @return the size of a file
771
public int getSize() {
776
* returns the number of files being used
778
* @return the number of files
780
public int getNumFiles() {
785
* logs the given message
787
* @param level the level of severity
788
* @param message the message to log
790
public void log(Level level, String message) {
791
log(level, "", message);
795
* prints the given message with the specified level
797
* @param level the level of logging
798
* @param sourceclass the class that logs the message
799
* @param message the message to print
801
public void log(Level level, String sourceclass, String message) {
802
log(level, sourceclass, "", message);
806
* prints the given message with the specified level
808
* @param level the level of logging
809
* @param sourceclass the class that logs the message
810
* @param sourcemethod the method that logs the message
811
* @param message the message to print
813
public void log(Level level, String sourceclass, String sourcemethod, String message) {
816
logger = getLogger();
819
logger.logp(level, sourceclass, sourcemethod, message);
821
System.out.println(message);
825
* a convenience method for dumping the current system info in the
830
public void logSystemInfo() {
831
log(INFO, "SystemInfo:\n" + new SystemInfo().toString());
835
* returns a string representation of the logger
837
* @return a string representation of the logger
839
public String toString() {
842
result = "Filename: " + getFilename() + ", "
843
+ "Size: " + getSize() + ", "
844
+ "# Files: " + getNumFiles();
851
* This extended Random class enables one to print the generated random
852
* numbers etc., before they are returned. It can either use stdout (default)
853
* for outputting the logging information or a Log object (level is then
856
* @author FracPete (fracpete at waikato dot ac dot nz)
857
* @version $Revision: 1.7 $
859
public static class Random
860
extends java.util.Random
861
implements Serializable {
863
/** for serialization */
864
private static final long serialVersionUID = 1256846887618333956L;
866
/** whether to output debug information */
867
protected boolean m_Debug = false;
869
/** the unique ID for this number generator */
872
/** for keeping track of unique IDs */
873
protected static long m_CurrentID;
875
/** the log to use for outputting the data, otherwise just stdout */
876
protected Log m_Log = null;
879
* Creates a new random number generator. With no debugging.
886
* Creates a new random number generator using a single long seed.
889
* @param seed the seed value
891
public Random(long seed) {
896
* Creates a new random number generator. With optional debugging.
898
* @param debug if true, debugging output is enabled
900
public Random(boolean debug) {
909
* Creates a new random number generator using a single long seed.
910
* With optional debugging
912
* @param seed the seed value
913
* @param debug if true, debugging output is enabled
915
public Random(long seed, boolean debug) {
924
* sets whether to print the generated random values or not
926
* @param value if true debugging output is enabled
928
public void setDebug(boolean value) {
933
* returns whether to print the generated random values or not
935
* @return true if debugging output is enabled
937
public boolean getDebug() {
942
* the log to use, if it is null then stdout is used
944
* @param value the log to use
946
public void setLog(Log value) {
951
* the currently used log, if null then stdout is used for outputting
952
* the debugging information
954
* @return the log, can be null
956
public Log getLog() {
961
* returns the next unique ID for a number generator
963
* @return the next unique ID
965
protected static long nextID() {
972
* returns the unique ID of this number generator
974
* @return the unique ID of this number generator
976
public long getID() {
981
* prints the given message only if m_Debug is TRUE
983
* @param msg the message to print
986
protected void println(String msg) {
988
if (getLog() != null)
989
getLog().log(Level.INFO, m_ID + ": " + msg);
991
System.out.println(m_ID + ": " + msg);
996
* prints the current stacktrace
998
public void printStackTrace() {
1000
StringWriter writer;
1002
writer = new StringWriter();
1004
// generate stacktrace
1005
t = new Throwable();
1006
t.fillInStackTrace();
1007
t.printStackTrace(new PrintWriter(writer));
1009
println(writer.toString());
1013
* Returns the next pseudorandom, uniformly distributed boolean value from
1014
* this random number generator's sequence.
1016
* @return random boolean
1018
public boolean nextBoolean() {
1019
boolean result = super.nextBoolean();
1020
println("nextBoolean=" + result);
1025
* Generates random bytes and places them into a user-supplied byte array.
1027
* @param bytes array to fill with random bytes
1029
public void nextBytes(byte[] bytes) {
1030
super.nextBytes(bytes);
1031
println("nextBytes=" + Utils.arrayToString(bytes));
1035
* Returns the next pseudorandom, uniformly distributed double value between
1036
* 0.0 and 1.0 from this random number generator's sequence.
1038
* @return random double
1040
public double nextDouble() {
1041
double result = super.nextDouble();
1042
println("nextDouble=" + result);
1047
* Returns the next pseudorandom, uniformly distributed float value between
1048
* 0.0 and 1.0 from this random number generator's sequence.
1050
* @return random float
1052
public float nextFloat() {
1053
float result = super.nextFloat();
1054
println("nextFloat=" + result);
1059
* Returns the next pseudorandom, Gaussian ("normally") distributed double
1060
* value with mean 0.0 and standard deviation 1.0 from this random number
1061
* generator's sequence.
1063
* @return random double, gaussian distributed
1065
public double nextGaussian() {
1066
double result = super.nextGaussian();
1067
println("nextGaussian=" + result);
1072
* Returns the next pseudorandom, uniformly distributed int value from this
1073
* random number generator's sequence.
1075
* @return random int
1077
public int nextInt() {
1078
int result = super.nextInt();
1079
println("nextInt=" + result);
1084
* Returns a pseudorandom, uniformly distributed int value between 0
1085
* (inclusive) and the specified value (exclusive), drawn from this random
1086
* number generator's sequence.
1088
* @param n the upper limit (exclusive)
1089
* @return random int
1091
public int nextInt(int n) {
1092
int result = super.nextInt(n);
1093
println("nextInt(" + n + ")=" + result);
1098
* Returns the next pseudorandom, uniformly distributed long value from this
1099
* random number generator's sequence.
1101
* @return random long
1103
public long nextLong() {
1104
long result = super.nextLong();
1105
println("nextLong=" + result);
1110
* Sets the seed of this random number generator using a single long seed.
1112
* @param seed the seed value
1114
public void setSeed(long seed) {
1115
super.setSeed(seed);
1116
println("setSeed(" + seed + ")");
1120
* returns a string representation of this number generator
1122
* @return a string representation
1124
public String toString() {
1125
return this.getClass().getName() + ": " + getID();
1129
* contains debug methods
1131
* @author Gabi Schmidberger (gabi at cs dot waikato dot ac dot nz)
1132
* @version $Revision: 1.7 $
1134
public static class DBO
1135
implements Serializable {
1137
/** for serialization */
1138
static final long serialVersionUID = -5245628124742606784L;
1140
/** enables/disables output of debug information */
1141
public boolean m_verboseOn = false;
1143
/** range of outputtyp */
1144
public Range m_outputTypes = new Range();
1147
* Set the verbose on flag on
1149
public void setVerboseOn() {
1154
* Initialize ranges, upper limit must be set
1156
* @param upper upper limit
1158
public void initializeRanges(int upper) {
1159
m_outputTypes.setUpper(upper);
1163
* Return true if the outputtype is set
1165
* @param num value that is reserved for a specific outputtype
1166
* @return return true if the output type is set
1168
public boolean outputTypeSet(int num) {
1169
return (m_outputTypes.isInRange(num));
1173
* Return true if the debug level is set
1174
* same method as outpuTypeSet but better name
1176
* @param num value that is reserved for a specific outputtype
1177
* @return return true if the debug level is set
1179
public boolean dl(int num) {
1180
return (outputTypeSet(num));
1184
* Switches the outputs on that are requested from the option O
1186
* @param list list of integers, all are used for an output type
1188
public void setOutputTypes(String list) {
1189
if (list.length() > 0) {
1192
m_outputTypes.setRanges(list);
1193
m_outputTypes.setUpper(30);
1198
* Gets the current output type selection
1200
* @return a string containing a comma separated list of ranges
1202
public String getOutputTypes() {
1203
return m_outputTypes.getRanges();
1207
* prints out text + endofline if verbose is on.
1208
* helps to make debug output commands more visible in text
1210
* @param text the text to print
1212
public void dpln(String text) {
1214
System.out.println(text);
1219
* prints out text + endofline but only if parameter debug type is set.
1220
* helps to make debug output commands more visible in text
1222
* @param debugType the type of the output
1223
* @param text the text to print
1225
public void dpln(int debugType, String text) {
1226
if (outputTypeSet(debugType)) {
1227
System.out.println(text);
1232
* prints out text if verbose is on.
1233
* helps to make debug output commands more visible in text
1235
* @param text the text to print
1237
public void dp(String text) {
1239
System.out.print(text);
1244
* prints out text but only if debug level is set.
1245
* helps to make debug output commands more visible in text
1247
* @param debugType the type of the output
1248
* @param text the text to print
1250
public void dp(int debugType, String text) {
1251
if (outputTypeSet(debugType)) {
1252
System.out.print(text);
1257
* prints out text + endofline.
1258
* helps to make debug output commands more visible in text
1260
* @param text the text to print
1262
public static void pln(String text) {
1263
System.out.println(text);
1268
* helps to make debug output commands more visible in text
1270
* @param text the text to print
1272
public static void p (String text) {
1273
System.out.print(text);
1278
* default constructor, prints only to stdout
1285
* logs the output to the specified file (and stdout). Size is 1,000,000 bytes
1288
* @param filename the name of the log
1290
public Debug(String filename) {
1291
this(filename, 1000000, 1);
1297
* @param filename the name of the log
1298
* @param size the size of the files in bytes
1299
* @param numFiles the number of files for rotating
1301
public Debug(String filename, int size, int numFiles) {
1304
m_Log = newLog(filename, size, numFiles);
1308
* turns the string representing a level, e.g., "FINE" or "ALL" into
1309
* the corresponding level (case-insensitive). The default is ALL.
1311
* @param level the string to return a level for
1312
* @return the corresponding level or the default
1314
public static Level stringToLevel(String level) {
1315
return Log.stringToLevel(level);
1319
* returns a new Log instance
1321
* @param filename the name of the log
1322
* @param size the size of the files in bytes
1323
* @param numFiles the number of files for rotating
1324
* @return the log instance
1326
public static Log newLog(String filename, int size, int numFiles) {
1327
return new Log(filename, size, numFiles);
1331
* prints the given message with level INFO
1333
* @param message the message to print
1335
public void log(String message) {
1340
* prints the given message with the specified level and an empty sourceclass
1342
* @param level the level of logging
1343
* @param message the message to print
1345
public void log(Level level, String message) {
1346
log(level, "", message);
1350
* prints the given message with the specified level
1352
* @param level the level of logging
1353
* @param sourceclass the class that logs the message
1354
* @param message the message to print
1356
public void log(Level level, String sourceclass, String message) {
1357
log(level, sourceclass, "", message);
1361
* prints the given message with the specified level
1363
* @param level the level of logging
1364
* @param sourceclass the class that logs the message
1365
* @param sourcemethod the method that logs the message
1366
* @param message the message to print
1368
public void log(Level level, String sourceclass, String sourcemethod, String message) {
1370
m_Log.log(level, sourceclass, sourcemethod, message);
1374
* sets whether the logging is enabled or not
1376
* @param value if true logging will be enabled
1378
public void setEnabled(boolean value) {
1383
* returns whether the logging is enabled
1385
* @return true if the logging is enabled
1387
public boolean getEnabled() {
1392
* returns a new instance of a clock
1394
* @return a new instance of a Clock
1396
public static Clock newClock() {
1401
* returns the instance of the Clock that is internally used
1403
* @return the clock that's being used
1405
public Clock getClock() {
1412
public void startClock() {
1417
* stops the clock and prints the message associated with the time, but only
1418
* if the logging is enabled.
1420
* @param message the message to print
1421
* @see #getEnabled()
1423
public void stopClock(String message) {
1424
log(message + ": " + m_Clock);
1428
* returns a default debug random object, with no particular seed and
1429
* debugging enabled.
1431
* @return a new instance of a Random object
1433
public static java.util.Random newRandom() {
1434
return new Random(true);
1438
* returns a debug random object with the specified seed and debugging
1441
* @param seed the seed value
1442
* @return a new instance of a Random object
1444
public static java.util.Random newRandom(int seed) {
1445
return new Random(seed, true);
1449
* returns a default timestamp for the current date/time
1451
* @return a new timestamp
1453
public static Timestamp newTimestamp() {
1454
return new Timestamp();
1458
* returns the system temp directory
1460
* @return the temp directory
1462
public static String getTempDir() {
1463
return System.getProperty("java.io.tmpdir");
1467
* returns the home directory of the user
1469
* @return the user's home directory
1471
public static String getHomeDir() {
1472
return System.getProperty("user.home");
1476
* returns the current working directory of the user
1478
* @return the user's current working directory
1480
public static String getCurrentDir() {
1481
return System.getProperty("user.dir");
1485
* Writes the given object to the specified file. The string representation
1486
* of the object is appended to the file.
1488
* @param filename the file to write to
1489
* @param obj the object to write to the file
1490
* @return true if writing was successful
1492
public static boolean writeToFile(String filename, Object obj) {
1493
return writeToFile(filename, obj, true);
1497
* Writes the given message to the specified file. The message is appended
1500
* @param filename the file to write to
1501
* @param message the message to write
1502
* @return true if writing was successful
1504
public static boolean writeToFile(String filename, String message) {
1505
return writeToFile(filename, message, true);
1509
* Writes the given object to the specified file. The string representation
1510
* of the object is either appended or replaces the current content of the
1513
* @param filename the file to write to
1514
* @param obj the object to write to the file
1515
* @param append whether to append the message or not
1516
* @return true if writing was successful
1518
public static boolean writeToFile(String filename, Object obj, boolean append) {
1519
return writeToFile(filename, obj.toString(), append);
1523
* Writes the given message to the specified file. The message is either
1524
* appended or replaces the current content of the file.
1526
* @param filename the file to write to
1527
* @param message the message to write
1528
* @param append whether to append the message or not
1529
* @return true if writing was successful
1531
public static boolean writeToFile(String filename, String message, boolean append) {
1533
BufferedWriter writer;
1536
writer = new BufferedWriter(new FileWriter(filename, append));
1537
writer.write(message);
1543
catch (Exception e) {
1551
* writes the serialized object to the speicified file
1553
* @param filename the file to serialize the object to
1554
* @param o the object to serialize
1555
* @return true if writing was successful
1557
public static boolean saveToFile(String filename, Object o) {
1560
if (SerializationHelper.isSerializable(o.getClass())) {
1562
SerializationHelper.write(filename, o);
1565
catch (Exception e) {
1577
* deserializes the content of the file and returns it, null if an error
1580
* @param filename the name of the file to deserialize
1581
* @return the deserialized content, null if problem occurred
1583
public static Object loadFromFile(String filename) {
1587
result = SerializationHelper.read(filename);
1589
catch (Exception e) {