1   /**
2    * QJ-Pro is a static code analyzer for Java.
3    * Copyright (c) 2004, http://qjpro.sourceforge.net
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package com.qasystems.debug;
20  
21  import com.qasystems.qstudio.java.Version;
22  
23  import java.io.PrintStream;
24  import java.io.PrintWriter;
25  
26  /**
27   * This class implements a writer specialized in printing debug information.
28   * Each write action has a verbose level. If this level is smaller or equal to
29   * the maximum verbose level, then the output is written.
30   *
31   * @author Sweder Schellens
32   * @version %full_filespec: DebugWriter.java,12:java:1 %
33   */
34  public class DebugWriter {
35    /** Write nothing */
36    public final static int VERBOSE_NOTHING = 0;
37  
38    /** Write exception stack traces only */
39    public final static int VERBOSE_EXCEPTIONS = 1;
40  
41    /** Write anything */
42    public final static int VERBOSE_ANYTHING = 2;
43    private int maxVerboseLevel =VERBOSE_ANYTHING ;
44      //Version.isDevelopment() ? VERBOSE_ANYTHING : VERBOSE_NOTHING;
45    private PrintWriter outWriter = null;
46    private PrintStream outStream = null;
47  
48    /**
49     * Default constructor for writing to standard error.
50     */
51    public DebugWriter() {
52      super();
53    }
54  
55    /**
56     * Default constructor for writing via a print writer.
57     *
58     * @param out the print writer
59     */
60    public DebugWriter(PrintWriter out) {
61      this();
62      setOutWriter(out);
63    }
64  
65    /**
66     * Default constructor for writing via a print stream.
67     *
68     * @param out the print stream
69     */
70    public DebugWriter(PrintStream out) {
71      this();
72      setOutStream(out);
73    }
74  
75    /**
76     * Write an exception's stack trace with level VERBOSE_EXCEPTIONS.
77     *
78     * @param ex the throwable object
79     * @param source the object that caught the exception
80     */
81    public void writeException(Throwable ex, Object source) {
82      writeException(ex, source, VERBOSE_EXCEPTIONS);
83    }
84  
85    /**
86     * Write an exception's stack trace with given level.
87     *
88     * @param ex the throwable object
89     * @param source the object that caught the exception
90     * @param level the verbose level
91     */
92    public void writeException(Throwable ex, Object source, int level) {
93      writeMessage(ex.getClass().getName() + " in class " + source, level);
94  
95      if (level <= maxVerboseLevel) {
96        if (outWriter != null) {
97          ex.printStackTrace(outWriter);
98        } else if (outStream != null) {
99          ex.printStackTrace(outStream);
100       } else {
101         ex.printStackTrace();
102       }
103     }
104   }
105 
106   /**
107    * Write a message with level VERBOSE_ANYTHING.
108    *
109    * @param msg the message
110    */
111   public void writeMessage(String msg) {
112     writeMessage(msg, VERBOSE_ANYTHING);
113   }
114 
115   /**
116    * Write a message with given level.
117    *
118    * @param msg the message
119    * @param level the verbose level
120    */
121   public void writeMessage(String msg, int level) {
122     if (level <= maxVerboseLevel) {
123       if (outWriter != null) {
124         outWriter.println(msg);
125       } else if (outStream != null) {
126         outStream.println(msg);
127       } else {
128         System.err.println(msg);
129       }
130     }
131   }
132 
133   /**
134    * Returns <class_name>@<object_hashcode>.</tt>.
135    *
136    * @return the string
137    */
138   public String toString() {
139     return (getClass().getName() + "@" + Integer.toHexString(hashCode()));
140   }
141 
142   /**
143    * Sets the maximum verbose level.
144    *
145    * @param level the maximum verbose level
146    */
147   public synchronized void setMaxVerboseLevel(int newMaxVerboseLevel) {
148     maxVerboseLevel = newMaxVerboseLevel;
149   }
150 
151   /**
152    * Gets the maximum verbose level.
153    *
154    * @return the maximum verbose level
155    */
156   public int getMaxVerboseLevel() {
157     return maxVerboseLevel;
158   }
159 
160   private synchronized void setOutWriter(PrintWriter newOutWriter) {
161     outWriter = newOutWriter;
162   }
163 
164   private synchronized PrintWriter getOutWriter() {
165     return outWriter;
166   }
167 
168   private synchronized void setOutStream(PrintStream newOutStream) {
169     outStream = newOutStream;
170   }
171 
172   private synchronized PrintStream getOutStream() {
173     return outStream;
174   }
175 }
176