release 0.9.6
[debian/openrocket] / 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 public class LogLine implements Comparable<LogLine> {
7         
8         private static final AtomicInteger logCount = new AtomicInteger(1);
9         private static final long startTime = System.currentTimeMillis();
10         
11         private final LogLevel level;
12         private final int count;
13         private final long timestamp;
14         private final TraceException trace;
15         private final String message;
16         private final Throwable cause;
17         
18         private String formattedMessage = null;
19         
20
21         public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
22                 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime,
23                                 trace, message, cause);
24         }
25         
26         public LogLine(LogLevel level, int count, TraceException trace, String message, 
27                         Throwable cause) {
28                 this(level, count, System.currentTimeMillis() - startTime, trace, message, cause);
29         }
30         
31         public LogLine(LogLevel level, int count, long timestamp, 
32                         TraceException trace, String message, Throwable cause) {
33                 this.level = level;
34                 this.count = count;
35                 this.timestamp = timestamp;
36                 this.trace = trace;
37                 this.message = message;
38                 this.cause = cause;
39         }
40
41         
42         
43         /**
44          * @return the level
45          */
46         public LogLevel getLevel() {
47                 return level;
48         }
49
50
51         /**
52          * @return the count
53          */
54         public int getLogCount() {
55                 return count;
56         }
57
58
59         /**
60          * @return the timestamp
61          */
62         public long getTimestamp() {
63                 return timestamp;
64         }
65
66
67         /**
68          * @return the trace
69          */
70         public TraceException getTrace() {
71                 return trace;
72         }
73
74
75         /**
76          * @return the message
77          */
78         public String getMessage() {
79                 return message;
80         }
81
82
83         /**
84          * @return the error
85          */
86         public Throwable getCause() {
87                 return cause;
88         }
89
90
91
92
93         /**
94          * Return a formatted string of the log line.  The line contains the log
95          * line count, the time stamp, the log level, the trace position, the log
96          * message and, if provided, the stack trace of the error throwable.
97          */
98         @Override
99         public String toString() {
100                 if (formattedMessage == null) {
101                         formattedMessage = String.format("%4d %10.3f %-" + LogLevel.LENGTH + "s %s %s",
102                                         count, timestamp/1000.0, level.toString(),
103                                         trace.getMessage(), message);
104                         if (cause != null) {
105                                 StackTraceWriter stw = new StackTraceWriter();
106                                 PrintWriter pw = new PrintWriter(stw);
107                                 cause.printStackTrace(pw);
108                                 pw.flush();
109                                 formattedMessage = formattedMessage + "\n" + stw.toString();
110                         }
111                 }
112                 return formattedMessage;
113         }
114
115
116         /**
117          * Compare against another log line based on the log line count number.
118          */
119         @Override
120         public int compareTo(LogLine o) {
121                 return this.count - o.count;
122         }
123         
124 }