View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.spi;
19  
20  import java.io.PrintStream;
21  
22  /**
23   * Used to store special log4j errors which cannot be logged using internal
24   * logging. Such errors include those occurring during the initial phases
25   * of log4j configuration or errors emanating from core components such as
26   * Logger or Hierarchy.
27   *
28   * @author Ceki Gulcu
29   */
30  public class ErrorItem {
31      /**
32       * Message.
33       */
34    String message;
35      /**
36       * Column.
37       */
38    int colNumber = -1;
39      /**
40       * Line number.
41       */
42    int lineNumber = -1;
43      /**
44       * Exception.
45       */
46    Throwable exception;
47  
48      /**
49       * Create new instance.
50       * @param message message
51       * @param e exception
52       */
53    public ErrorItem(final String message, final Exception e) {
54      super();
55      this.message = message;
56      exception = e;
57    }
58  
59      /**
60       * Creaet new instance.
61       * @param message message.
62       */
63    public ErrorItem(final String message) {
64      this(message, null);
65    }
66  
67      /**
68       * Get column number.
69       * @return column number.
70       */
71    public int getColNumber() {
72      return colNumber;
73    }
74  
75      /**
76       * Set column number.
77       * @param colNumber new column number.
78       */
79    public void setColNumber(int colNumber) {
80      this.colNumber = colNumber;
81    }
82  
83      /**
84       * Get exception.
85       * @return exception.
86       */
87    public Throwable getException() {
88      return exception;
89    }
90  
91      /**
92       * Set exception.
93       * @param exception exception
94       */
95    public void setException(final Throwable exception) {
96      this.exception = exception;
97    }
98  
99      /**
100      * Get line number.
101      * @return line number.
102      */
103   public int getLineNumber() {
104     return lineNumber;
105   }
106 
107     /**
108      * Set line number.
109      * @param lineNumber line number.
110      */
111   public void setLineNumber(final int lineNumber) {
112     this.lineNumber = lineNumber;
113   }
114 
115     /**
116      * Get message.
117      * @return message.
118      */
119   public String getMessage() {
120     return message;
121   }
122 
123     /**
124      * Set message.
125      * @param message message.
126      */
127   public void setMessage(final String message) {
128     this.message = message;
129   }
130 
131     /**
132      * String representation of ErrorItem.
133      * @return string.
134      */
135   public String toString() {
136     String str =
137       "Reported error: \"" + message + "\"";
138 
139     if (lineNumber != -1) {
140       str += " at line " + lineNumber + " column " + colNumber;
141     }
142     if (exception != null) {
143       str += (" with exception " + exception);
144     }
145     return str;
146   }
147 
148   /**
149    * Dump the details of this ErrorItem to System.out.
150    */
151   public void dump() {
152     dump(System.out);
153   }
154   
155   /**
156    * Dump the details of this ErrorItem on the specified {@link PrintStream}.
157    * @param ps print stream.
158    */
159   public void dump(final PrintStream ps) {
160     String str =
161       "Reported error: \"" + message + "\"";
162 
163     if (lineNumber != -1) {
164       str += " at line " + lineNumber + " column " + colNumber;
165     }
166     ps.println(str);
167 
168     if (exception != null) {
169       exception.printStackTrace(ps);
170     }
171   }
172 }