create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / logging / LogLine.java
1 package net.sf.openrocket.logging;
2
3 import java.io.PrintWriter;
4 import java.util.concurrent.atomic.AtomicInteger;
5
6 /**
7  * Container object for a log line.  A log line consists of the following elements:
8  * <ul>
9  *      <li>a LogLevel
10  *      <li>a TraceException
11  *      <li>a message
12  *      <li>a cause Throwable
13  *      <li>an incremental log line counter (provided by LogLine)
14  *      <li>a millisecond timestamp (provided by LogLine)
15  * </ul>
16  * Any one of the provided input values may be null.
17  * 
18  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19  */
20 public class LogLine implements Comparable<LogLine> {
21         
22         private static final AtomicInteger logCount = new AtomicInteger(1);
23         private static final long startTime = System.currentTimeMillis();
24         
25         private final LogLevel level;
26         private final int count;
27         private final long timestamp;
28         private final TraceException trace;
29         private final String message;
30         private final Throwable cause;
31         
32         private volatile String formattedMessage = null;
33         
34         
35         /**
36          * Construct a LogLine at the current moment.  The next log line count number is selected
37          * and the current run time set to the timestamp.
38          * 
39          * @param level         the logging level
40          * @param trace         the trace exception for the log line, <code>null</code> permitted
41          * @param message       the log message
42          * @param cause         the causing throwable, <code>null</code> permitted
43          */
44         public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
45                 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime, trace, message, cause);
46         }
47         
48         /**
49          * Construct a LogLine with all parameters.  This should only be used in special conditions,
50          * for example to insert a log line at a specific point within normal logs.
51          * 
52          * @param level         the logging level
53          * @param count         the log line count number
54          * @param timestamp     the log line timestamp
55          * @param trace         the trace exception for the log line, <code>null</code> permitted
56          * @param message       the log message
57          * @param cause         the causing throwable, <code>null</code> permitted
58          */
59         public LogLine(LogLevel level, int count, long timestamp,
60                         TraceException trace, String message, Throwable cause) {
61                 this.level = level;
62                 this.count = count;
63                 this.timestamp = timestamp;
64                 this.trace = trace;
65                 this.message = message;
66                 this.cause = cause;
67         }
68         
69         
70
71         /**
72          * @return the level
73          */
74         public LogLevel getLevel() {
75                 return level;
76         }
77         
78         
79         /**
80          * @return the count
81          */
82         public int getLogCount() {
83                 return count;
84         }
85         
86         
87         /**
88          * @return the timestamp
89          */
90         public long getTimestamp() {
91                 return timestamp;
92         }
93         
94         
95         /**
96          * @return the trace
97          */
98         public TraceException getTrace() {
99                 return trace;
100         }
101         
102         
103         /**
104          * @return the message
105          */
106         public String getMessage() {
107                 return message;
108         }
109         
110         
111         /**
112          * @return the error
113          */
114         public Throwable getCause() {
115                 return cause;
116         }
117         
118         
119
120
121         /**
122          * Return a formatted string of the log line.  The line contains the log
123          * line count, the time stamp, the log level, the trace position, the log
124          * message and, if provided, the stack trace of the error throwable.
125          */
126         @Override
127         public String toString() {
128                 if (formattedMessage == null) {
129                         String str;
130                         str = String.format("%4d %10.3f %-" + LogLevel.LENGTH + "s %s %s",
131                                         count, timestamp / 1000.0, (level != null) ? level.toString() : "NULL",
132                                         (trace != null) ? trace.getMessage() : "(-)",
133                                         message);
134                         if (cause != null) {
135                                 StackTraceWriter stw = new StackTraceWriter();
136                                 PrintWriter pw = new PrintWriter(stw);
137                                 cause.printStackTrace(pw);
138                                 pw.flush();
139                                 str = str + "\n" + stw.toString();
140                         }
141                         formattedMessage = str;
142                 }
143                 return formattedMessage;
144         }
145         
146         
147         /**
148          * Compare against another log line based on the log line count number.
149          */
150         @Override
151         public int compareTo(LogLine o) {
152                 return this.count - o.count;
153         }
154         
155 }