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       *
51       * @param message message
52       * @param e       exception
53       */
54      public ErrorItem(final String message, final Exception e) {
55          super();
56          this.message = message;
57          exception = e;
58      }
59  
60      /**
61       * Creaet new instance.
62       *
63       * @param message message.
64       */
65      public ErrorItem(final String message) {
66          this(message, null);
67      }
68  
69      /**
70       * Get column number.
71       *
72       * @return column number.
73       */
74      public int getColNumber() {
75          return colNumber;
76      }
77  
78      /**
79       * Set column number.
80       *
81       * @param colNumber new column number.
82       */
83      public void setColNumber(int colNumber) {
84          this.colNumber = colNumber;
85      }
86  
87      /**
88       * Get exception.
89       *
90       * @return exception.
91       */
92      public Throwable getException() {
93          return exception;
94      }
95  
96      /**
97       * Set exception.
98       *
99       * @param exception exception
100      */
101     public void setException(final Throwable exception) {
102         this.exception = exception;
103     }
104 
105     /**
106      * Get line number.
107      *
108      * @return line number.
109      */
110     public int getLineNumber() {
111         return lineNumber;
112     }
113 
114     /**
115      * Set line number.
116      *
117      * @param lineNumber line number.
118      */
119     public void setLineNumber(final int lineNumber) {
120         this.lineNumber = lineNumber;
121     }
122 
123     /**
124      * Get message.
125      *
126      * @return message.
127      */
128     public String getMessage() {
129         return message;
130     }
131 
132     /**
133      * Set message.
134      *
135      * @param message message.
136      */
137     public void setMessage(final String message) {
138         this.message = message;
139     }
140 
141     /**
142      * String representation of ErrorItem.
143      *
144      * @return string.
145      */
146     public String toString() {
147         String str =
148             "Reported error: \"" + message + "\"";
149 
150         if (lineNumber != -1) {
151             str += " at line " + lineNumber + " column " + colNumber;
152         }
153         if (exception != null) {
154             str += (" with exception " + exception);
155         }
156         return str;
157     }
158 
159     /**
160      * Dump the details of this ErrorItem to System.out.
161      */
162     public void dump() {
163         dump(System.out);
164     }
165 
166     /**
167      * Dump the details of this ErrorItem on the specified {@link PrintStream}.
168      *
169      * @param ps print stream.
170      */
171     public void dump(final PrintStream ps) {
172         String str =
173             "Reported error: \"" + message + "\"";
174 
175         if (lineNumber != -1) {
176             str += " at line " + lineNumber + " column " + colNumber;
177         }
178         ps.println(str);
179 
180         if (exception != null) {
181             exception.printStackTrace(ps);
182         }
183     }
184 }