]> git.gag.com Git - debian/openrocket/blob - src/net/sf/openrocket/logging/LogLine.java
71b92d48f8452db2e24f477ae5c104c6b397e630
[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 /**
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         public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
36                 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime, trace, message, cause);
37         }
38         
39         
40         public LogLine(LogLevel level, int count, long timestamp, 
41                         TraceException trace, String message, Throwable cause) {
42                 this.level = level;
43                 this.count = count;
44                 this.timestamp = timestamp;
45                 this.trace = trace;
46                 this.message = message;
47                 this.cause = cause;
48         }
49
50         
51         
52         /**
53          * @return the level
54          */
55         public LogLevel getLevel() {
56                 return level;
57         }
58
59
60         /**
61          * @return the count
62          */
63         public int getLogCount() {
64                 return count;
65         }
66
67
68         /**
69          * @return the timestamp
70          */
71         public long getTimestamp() {
72                 return timestamp;
73         }
74
75
76         /**
77          * @return the trace
78          */
79         public TraceException getTrace() {
80                 return trace;
81         }
82
83
84         /**
85          * @return the message
86          */
87         public String getMessage() {
88                 return message;
89         }
90
91
92         /**
93          * @return the error
94          */
95         public Throwable getCause() {
96                 return cause;
97         }
98
99
100
101
102         /**
103          * Return a formatted string of the log line.  The line contains the log
104          * line count, the time stamp, the log level, the trace position, the log
105          * message and, if provided, the stack trace of the error throwable.
106          */
107         @Override
108         public String toString() {
109                 if (formattedMessage == null) {
110                         String str;
111                         str = String.format("%4d %10.3f %-" + LogLevel.LENGTH + "s %s %s",
112                                         count, timestamp/1000.0, (level != null) ? level.toString() : "NULL",
113                                         (trace != null) ? trace.getMessage() : "(-)",
114                                         message);
115                         if (cause != null) {
116                                 StackTraceWriter stw = new StackTraceWriter();
117                                 PrintWriter pw = new PrintWriter(stw);
118                                 cause.printStackTrace(pw);
119                                 pw.flush();
120                                 str = str + "\n" + stw.toString();
121                         }
122                         formattedMessage = str;
123                 }
124                 return formattedMessage;
125         }
126
127
128         /**
129          * Compare against another log line based on the log line count number.
130          */
131         @Override
132         public int compareTo(LogLine o) {
133                 return this.count - o.count;
134         }
135         
136 }