1 package net.sf.openrocket.logging;
3 import java.io.PrintWriter;
4 import java.util.concurrent.atomic.AtomicInteger;
7 * Container object for a log line. A log line consists of the following elements:
10 * <li>a TraceException
12 * <li>a cause Throwable
13 * <li>an incremental log line counter (provided by LogLine)
14 * <li>a millisecond timestamp (provided by LogLine)
16 * Any one of the provided input values may be null.
18 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20 public class LogLine implements Comparable<LogLine> {
22 private static final AtomicInteger logCount = new AtomicInteger(1);
23 private static final long startTime = System.currentTimeMillis();
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;
32 private volatile String formattedMessage = null;
35 public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
36 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime, trace, message, cause);
40 public LogLine(LogLevel level, int count, long timestamp,
41 TraceException trace, String message, Throwable cause) {
44 this.timestamp = timestamp;
46 this.message = message;
55 public LogLevel getLevel() {
63 public int getLogCount() {
69 * @return the timestamp
71 public long getTimestamp() {
79 public TraceException getTrace() {
87 public String getMessage() {
95 public Throwable getCause() {
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.
108 public String toString() {
109 if (formattedMessage == null) {
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() : "(-)",
116 StackTraceWriter stw = new StackTraceWriter();
117 PrintWriter pw = new PrintWriter(stw);
118 cause.printStackTrace(pw);
120 str = str + "\n" + stw.toString();
122 formattedMessage = str;
124 return formattedMessage;
129 * Compare against another log line based on the log line count number.
132 public int compareTo(LogLine o) {
133 return this.count - o.count;