-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdfa5ab
commit 636c849
Showing
5 changed files
with
150 additions
and
50 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/dev/latvian/apps/ansi/log/CallbackPrintStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dev.latvian.apps.ansi.log; | ||
|
||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.util.function.Consumer; | ||
|
||
public class CallbackPrintStream extends PrintStream { | ||
public final Consumer<Object> callback; | ||
|
||
public CallbackPrintStream(Consumer<Object> callback) { | ||
super(OutputStream.nullOutputStream()); | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public void println(int x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(char x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(long x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(float x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(char[] x) { | ||
callback.accept(new String(x)); | ||
} | ||
|
||
@Override | ||
public void println(double x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(String x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(boolean x) { | ||
callback.accept(x); | ||
} | ||
|
||
@Override | ||
public void println(Object x) { | ||
callback.accept(x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.latvian.apps.ansi.log; | ||
|
||
public record CombinedLog(Log first, Log second) implements Log { | ||
@Override | ||
public void log(LogType type, Object message) { | ||
first.log(type, message); | ||
second.log(type, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/dev/latvian/apps/ansi/log/TimestampFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dev.latvian.apps.ansi.log; | ||
|
||
import java.util.Calendar; | ||
|
||
public class TimestampFormat { | ||
public static final String[] MONTH_NAMES = { | ||
"Jan", | ||
"Feb", | ||
"Mar", | ||
"Apr", | ||
"May", | ||
"Jun", | ||
"Jul", | ||
"Aug", | ||
"Sep", | ||
"Oct", | ||
"Nov", | ||
"Dec", | ||
}; | ||
|
||
private final Calendar calendar; | ||
private final StringBuilder builder; | ||
private final boolean date; | ||
private final boolean time; | ||
|
||
public TimestampFormat(boolean date, boolean time) { | ||
this.calendar = Calendar.getInstance(); | ||
this.builder = new StringBuilder(21); | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
private void pad0(int num) { | ||
if (num < 10) { | ||
builder.append('0'); | ||
} | ||
|
||
builder.append(num); | ||
} | ||
|
||
public StringBuilder format(StringBuilder builder, long millis) { | ||
calendar.setTimeInMillis(millis); | ||
|
||
if (date) { | ||
pad0(calendar.get(Calendar.DAY_OF_MONTH)); | ||
builder.append('-'); | ||
builder.append(MONTH_NAMES[calendar.get(Calendar.MONTH)]); | ||
builder.append('-'); | ||
builder.append(calendar.get(Calendar.YEAR)); | ||
} | ||
|
||
if (date && time) { | ||
builder.append(' '); | ||
} | ||
|
||
if (time) { | ||
pad0(calendar.get(Calendar.HOUR_OF_DAY)); | ||
builder.append(':'); | ||
pad0(calendar.get(Calendar.MINUTE)); | ||
builder.append(':'); | ||
pad0(calendar.get(Calendar.SECOND)); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
public String format(long millis) { | ||
var str = format(builder, millis).toString(); | ||
builder.setLength(0); | ||
return str; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return date && time ? "yyyy-MMM-dd HH:mm:ss" : date ? "yyyy-MMM-dd" : time ? "HH:mm:ss" : ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters