001    package edu.rice.cs.cunit.util;
002    
003    import java.io.IOException;
004    import java.io.PrintStream;
005    import java.io.PrintWriter;
006    import java.io.FileWriter;
007    import java.util.Locale;
008    import java.util.HashMap;
009    
010    /**
011     * Debug output.
012     *
013     * @author Mathias Ricken
014     */
015    public class Debug extends PrintStream {
016        private boolean _debug = false;
017        private int _dotCount = 0;
018        private int _lineCount = 0;
019        public enum LogTarget { NONE, MAIN, FILE, BOTH };
020        private HashMap<String, LogTarget> _debugLogs = new HashMap<String, LogTarget>();
021    
022        public static final Debug out = new Debug(System.out);
023    
024        private PrintStream _output;
025    
026        public Debug(PrintStream output) {
027            super(System.out);
028            this._output = output;
029        }
030    
031        public void dot() {
032            ++_dotCount;
033            if (_dotCount == 100) {
034                _dotCount = 0;
035                print(".");
036                ++_lineCount;
037                if (_lineCount == 78) {
038                    _lineCount = 0;
039                    println("");
040                }
041            }
042        }
043    
044        public void endDot() {
045            if (_lineCount > 0) {
046                println("");
047            }
048            _dotCount = _lineCount = 0;
049        }
050    
051        public static class ProgressCounter {
052            public int cur;
053            public int max;
054            public byte lastPercentage;
055    
056            public ProgressCounter(int max) {
057                this.max = max;
058                this.cur = 0;
059                this.lastPercentage = 0;
060            }
061        }
062    
063        public ProgressCounter initPC(int max) {
064            print("  0%\r");
065            return new ProgressCounter(max);
066        }
067    
068        public void incPC(ProgressCounter pc) {
069            ++pc.cur;
070            byte perc = (byte)(100.0f / ((float)pc.max) * pc.cur);
071            if (perc > pc.lastPercentage) {
072                pc.lastPercentage = perc;
073                format("%3d%%\r", new Object[]{perc});
074            }
075        }
076    
077        public PrintStream getOutput() {
078            return _output;
079        }
080    
081        public void setOutput(PrintStream output) {
082            this._output = output;
083        }
084    
085        public boolean isDebug() {
086            return _debug;
087        }
088    
089        public void setDebug(boolean debug) {
090            _debug = debug;
091        }
092    
093        protected boolean debugOutput() {
094            return (_debug);
095        }
096    
097        public void write(int b) {
098            if (debugOutput()) {
099                _output.write(b);
100            }
101        }
102    
103        public void write(byte buf[], int off, int len) {
104            if (debugOutput()) {
105                _output.write(buf, off, len);
106            }
107        }
108    
109        public void print(boolean b) {
110            if (debugOutput()) {
111                _output.print(b);
112            }
113        }
114    
115        public void print(char c) {
116            if (debugOutput()) {
117                _output.print(c);
118            }
119        }
120    
121        public void print(int i) {
122            if (debugOutput()) {
123                _output.print(i);
124            }
125        }
126    
127        public void print(long l) {
128            if (debugOutput()) {
129                _output.print(l);
130            }
131        }
132    
133        public void print(float f) {
134            if (debugOutput()) {
135                _output.print(f);
136            }
137        }
138    
139        public void print(double d) {
140            if (debugOutput()) {
141                _output.print(d);
142            }
143        }
144    
145        public void print(char s[]) {
146            if (debugOutput()) {
147                _output.print(s);
148            }
149        }
150    
151        public void print(String s) {
152            if (debugOutput()) {
153                _output.print(s);
154            }
155        }
156    
157        public void print(Object obj) {
158            if (debugOutput()) {
159                _output.print(obj);
160            }
161        }
162    
163        public void println() {
164            if (debugOutput()) {
165                _output.println();
166            }
167        }
168    
169        public void println(boolean x) {
170            if (debugOutput()) {
171                _output.println(x);
172            }
173        }
174    
175        public void println(char x) {
176            if (debugOutput()) {
177                _output.println(x);
178            }
179        }
180    
181        public void println(int x) {
182            if (debugOutput()) {
183                _output.println(x);
184            }
185        }
186    
187        public void println(long x) {
188            if (debugOutput()) {
189                _output.println(x);
190            }
191        }
192    
193        public void println(float x) {
194            if (debugOutput()) {
195                _output.println(x);
196            }
197        }
198    
199        public void println(double x) {
200            if (debugOutput()) {
201                _output.println(x);
202            }
203        }
204    
205        public void println(char x[]) {
206            if (debugOutput()) {
207                _output.println(x);
208            }
209        }
210    
211        public void println(String x) {
212            if (debugOutput()) {
213                _output.println(x);
214            }
215        }
216    
217        public void println(Object x) {
218            if (debugOutput()) {
219                _output.println(x);
220            }
221        }
222    
223        public PrintStream printf(String format, Object... args) {
224            if (debugOutput()) {
225                _output.printf(format, args);
226            }
227            return this;
228        }
229    
230        public PrintStream printf(Locale l, String format, Object... args) {
231            if (debugOutput()) {
232                _output.printf(l, format, args);
233            }
234            return this;
235        }
236    
237        public PrintStream format(String format, Object... args) {
238            if (debugOutput()) {
239                _output.format(format, args);
240            }
241            return this;
242        }
243    
244        public PrintStream format(Locale l, String format, Object... args) {
245            if (debugOutput()) {
246                _output.format(l, format, args);
247            }
248            return this;
249        }
250    
251        public PrintStream append(CharSequence csq) {
252            if (debugOutput()) {
253                _output.append(csq);
254            }
255            return this;
256        }
257    
258        public PrintStream append(char c) {
259            if (debugOutput()) {
260                _output.append(c);
261            }
262            return this;
263        }
264    
265        public void write(byte b[]) throws IOException {
266            if (debugOutput()) {
267                _output.write(b);
268            }
269        }
270    
271        /**
272         * Set the log target of the debug log with the specified name,
273         * @param logName name of the log
274         * @param target target of the log
275         */
276        public void setLogTarget(String logName, LogTarget target) {
277            _debugLogs.put(logName, target);
278        }
279    
280        /**
281         * Return the log target of the specified debug log.
282         * @param logName name of the log
283         * @return log target
284         */
285        public LogTarget getLogTarget(String logName) {
286            LogTarget b = _debugLogs.get(logName);
287            return (b==null)?LogTarget.NONE:b;
288        }
289    
290        protected boolean isFileEnabled(String logName) {
291            LogTarget b = getLogTarget(logName);
292            return (b==LogTarget.FILE) || (b==LogTarget.BOTH);
293        }
294    
295        protected boolean isConsoleEnabled(String logName) {
296            LogTarget b = getLogTarget(logName);
297            return (b==LogTarget.MAIN) || (b==LogTarget.BOTH);
298        }
299    
300        public void print(String logName, boolean b) {
301            if (debugOutput() && isConsoleEnabled(logName)) {
302                _output.print(b);
303            }
304            if (debugOutput() && isFileEnabled(logName)) {
305                try {
306                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
307                    w.print(b);
308                    w.close();
309                }
310                catch(IOException e) {}
311            }
312        }
313    
314        public void print(String logName, char c) {
315            if (debugOutput() && isConsoleEnabled(logName)) {
316                _output.print(c);
317            }
318            if (debugOutput() && isFileEnabled(logName)) {
319                try {
320                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
321                    w.print(c);
322                    w.close();
323                }
324                catch(IOException e) {}
325            }
326        }
327    
328        public void print(String logName, int i) {
329            if (debugOutput() && isConsoleEnabled(logName)) {
330                _output.print(i);
331            }
332            if (debugOutput() && isFileEnabled(logName)) {
333                try {
334                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
335                    w.print(i);
336                    w.close();
337                }
338                catch(IOException e) {}
339            }
340        }
341    
342        public void print(String logName, long l) {
343            if (debugOutput() && isConsoleEnabled(logName)) {
344                _output.print(l);
345            }
346            if (debugOutput() && isFileEnabled(logName)) {
347                try {
348                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
349                    w.print(l);
350                    w.close();
351                }
352                catch(IOException e) {}
353            }
354        }
355    
356        public void print(String logName, float f) {
357            if (debugOutput() && isConsoleEnabled(logName)) {
358                _output.print(f);
359            }
360            if (debugOutput() && isFileEnabled(logName)) {
361                try {
362                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
363                    w.print(f);
364                    w.close();
365                }
366                catch(IOException e) {}
367            }
368        }
369    
370        public void print(String logName, double d) {
371            if (debugOutput() && isConsoleEnabled(logName)) {
372                _output.print(d);
373            }
374            if (debugOutput() && isFileEnabled(logName)) {
375                try {
376                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
377                    w.print(d);
378                    w.close();
379                }
380                catch(IOException e) {}
381            }
382        }
383    
384        public void print(String logName, char s[]) {
385            if (debugOutput() && isConsoleEnabled(logName)) {
386                _output.print(s);
387            }
388            if (debugOutput() && isFileEnabled(logName)) {
389                try {
390                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
391                    w.print(s);
392                    w.close();
393                }
394                catch(IOException e) {}
395            }
396        }
397    
398        public void print(String logName, String s) {
399            if (debugOutput() && isConsoleEnabled(logName)) {
400                _output.print(s);
401            }
402            if (debugOutput() && isFileEnabled(logName)) {
403                try {
404                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
405                    w.print(s);
406                    w.close();
407                }
408                catch(IOException e) {}
409            }
410        }
411    
412        public void print(String logName, Object obj) {
413            if (debugOutput() && isConsoleEnabled(logName)) {
414                _output.print(obj);
415            }
416            if (debugOutput() && isFileEnabled(logName)) {
417                try {
418                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
419                    w.print(obj);
420                    w.close();
421                }
422                catch(IOException e) {}
423            }
424        }
425    
426        public void println(String logName, boolean x) {
427            if (debugOutput() && isConsoleEnabled(logName)) {
428                _output.println(x);
429            }
430            if (debugOutput() && isFileEnabled(logName)) {
431                try {
432                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
433                    w.println(x);
434                    w.close();
435                }
436                catch(IOException e) {}
437            }
438        }
439    
440        public void println(String logName, char x) {
441            if (debugOutput() && isConsoleEnabled(logName)) {
442                _output.println(x);
443            }
444            if (debugOutput() && isFileEnabled(logName)) {
445                try {
446                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
447                    w.println(x);
448                    w.close();
449                }
450                catch(IOException e) {}
451            }
452        }
453    
454        public void println(String logName, int x) {
455            if (debugOutput() && isConsoleEnabled(logName)) {
456                _output.println(x);
457            }
458            if (debugOutput() && isFileEnabled(logName)) {
459                try {
460                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
461                    w.println(x);
462                    w.close();
463                }
464                catch(IOException e) {}
465            }
466        }
467    
468        public void println(String logName, long x) {
469            if (debugOutput() && isConsoleEnabled(logName)) {
470                _output.println(x);
471            }
472            if (debugOutput() && isFileEnabled(logName)) {
473                try {
474                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
475                    w.println(x);
476                    w.close();
477                }
478                catch(IOException e) {}
479            }
480        }
481    
482        public void println(String logName, float x) {
483            if (debugOutput() && isConsoleEnabled(logName)) {
484                _output.println(x);
485            }
486            if (debugOutput() && isFileEnabled(logName)) {
487                try {
488                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
489                    w.println(x);
490                    w.close();
491                }
492                catch(IOException e) {}
493            }
494        }
495    
496        public void println(String logName, double x) {
497            if (debugOutput() && isConsoleEnabled(logName)) {
498                _output.println(x);
499            }
500            if (debugOutput() && isFileEnabled(logName)) {
501                try {
502                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
503                    w.println(x);
504                    w.close();
505                }
506                catch(IOException e) {}
507            }
508        }
509    
510        public void println(String logName, char x[]) {
511            if (debugOutput() && isConsoleEnabled(logName)) {
512                _output.println(x);
513            }
514            if (debugOutput() && isFileEnabled(logName)) {
515                try {
516                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
517                    w.println(x);
518                    w.close();
519                }
520                catch(IOException e) {}
521            }
522        }
523    
524        public void println(String logName, String x) {
525            if (debugOutput() && isConsoleEnabled(logName)) {
526                _output.println(x);
527            }
528            if (debugOutput() && isFileEnabled(logName)) {
529                try {
530                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
531                    w.println(x);
532                    w.close();
533                }
534                catch(IOException e) {}
535            }
536        }
537    
538        public void println(String logName, Object x) {
539            if (debugOutput() && isConsoleEnabled(logName)) {
540                _output.println(x);
541            }
542            if (debugOutput() && isFileEnabled(logName)) {
543                try {
544                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"), true);
545                    w.println(x);
546                    w.close();
547                }
548                catch(IOException e) {}
549            }
550        }
551    
552        public PrintStream printf(String logName, String format, Object... args) {
553            if (debugOutput() && isConsoleEnabled(logName)) {
554                _output.printf(format, args);
555            }
556            if (debugOutput() && isFileEnabled(logName)) {
557                try {
558                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"));
559                    w.printf(format, args);
560                    w.close();
561                }
562                catch(IOException e) {}
563            }
564            return this;
565        }
566    
567        public PrintStream format(String logName, String format, Object... args) {
568            if (debugOutput() && isConsoleEnabled(logName)) {
569                _output.format(format, args);
570            }
571            if (debugOutput() && isFileEnabled(logName)) {
572                try {
573                    PrintWriter w = new PrintWriter(new FileWriter(logName+".log"));
574                    w.format(format, args);
575                    w.close();
576                }
577                catch(IOException e) {}
578            }
579            return this;
580        }
581    }