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;
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.
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
44 public LogLine(LogLevel level, TraceException trace, String message, Throwable cause) {
45 this(level, logCount.getAndIncrement(), System.currentTimeMillis() - startTime, trace, message, cause);
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.
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
59 public LogLine(LogLevel level, int count, long timestamp,
60 TraceException trace, String message, Throwable cause) {
63 this.timestamp = timestamp;
65 this.message = message;
74 public LogLevel getLevel() {
82 public int getLogCount() {
88 * @return the timestamp
90 public long getTimestamp() {
98 public TraceException getTrace() {
104 * @return the message
106 public String getMessage() {
114 public Throwable getCause() {
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.
127 public String toString() {
128 if (formattedMessage == null) {
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() : "(-)",
135 StackTraceWriter stw = new StackTraceWriter();
136 PrintWriter pw = new PrintWriter(stw);
137 cause.printStackTrace(pw);
139 str = str + "\n" + stw.toString();
141 formattedMessage = str;
143 return formattedMessage;
148 * Compare against another log line based on the log line count number.
151 public int compareTo(LogLine o) {
152 return this.count - o.count;