1 package net.sf.openrocket.logging;
3 import java.io.PrintWriter;
4 import java.util.concurrent.atomic.AtomicInteger;
6 public class LogLine implements Comparable<LogLine> {
8 private static final AtomicInteger logCount = new AtomicInteger(1);
9 private static final long startTime = System.currentTimeMillis();
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;
18 private String formattedMessage = null;
21 public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
22 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime,
23 trace, message, cause);
26 public LogLine(LogLevel level, int count, TraceException trace, String message,
28 this(level, count, System.currentTimeMillis() - startTime, trace, message, cause);
31 public LogLine(LogLevel level, int count, long timestamp,
32 TraceException trace, String message, Throwable cause) {
35 this.timestamp = timestamp;
37 this.message = message;
46 public LogLevel getLevel() {
54 public int getLogCount() {
60 * @return the timestamp
62 public long getTimestamp() {
70 public TraceException getTrace() {
78 public String getMessage() {
86 public Throwable getCause() {
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.
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);
105 StackTraceWriter stw = new StackTraceWriter();
106 PrintWriter pw = new PrintWriter(stw);
107 cause.printStackTrace(pw);
109 formattedMessage = formattedMessage + "\n" + stw.toString();
112 return formattedMessage;
117 * Compare against another log line based on the log line count number.
120 public int compareTo(LogLine o) {
121 return this.count - o.count;