Last active
January 10, 2019 14:46
-
-
Save BenjaminUrquhart/508e149a669bbc3daa76390fb013a9ad to your computer and use it in GitHub Desktop.
Adds date/time to standard output/error in Java
This file contains hidden or 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
package net.benjaminurquhart.stdout; | |
import java.io.PrintStream; | |
import java.time.OffsetDateTime; | |
import java.util.Locale; | |
// yes yes I know there's no input for I here. | |
public class STDIOPlus extends PrintStream{ | |
private static boolean enabled; | |
// Do NOT mess around with the stdout or stderr variables unless you want to risk breaking disable() | |
private static PrintStream stdout, stderr, outself, errself; | |
private PrintStream out; | |
private STDIOPlus(PrintStream stream) { | |
super(stream); | |
this.out = stream; | |
} | |
public synchronized static void enable() { | |
if(!enabled) { | |
enabled = true; | |
stdout = System.out; | |
stderr = System.err; | |
if(outself == null) outself = new STDIOPlus(stdout); | |
if(errself == null) errself = new STDIOPlus(stderr); | |
System.setOut(outself); | |
System.setErr(errself); | |
} | |
} | |
public synchronized static void disable() { | |
if(enabled) { | |
System.setOut(stdout); | |
System.setErr(stderr); | |
enabled = false; | |
} | |
} | |
private void write(Object s) { | |
// I don't want people to access this method via reflection. Why? Because there's no need. | |
if(!Thread.currentThread().getStackTrace()[2].getClassName().equals(this.getClass().getName())) throw new ReflectiveAccessException(); | |
OffsetDateTime now = OffsetDateTime.now(); | |
out.printf("[%d/%d/%d %02d:%02d:%02d]: %s", now.getMonthValue(), now.getDayOfMonth(), now.getYear(), now.getHour(), now.getMinute(), now.getSecond(), String.valueOf(s)); | |
} | |
// Do not look beyond here. | |
public void print(Object obj) {write(obj);} | |
public void print(String s) {write(s);} | |
public void print(long l) {write(l);} | |
public void print(int i) {write(i);} | |
public void print(double d) {write(d);} | |
public void print(float f) {write(f);} | |
public void print(char c) {write(Character.toString(c));} | |
public void print(boolean bool) {write(bool);} | |
public void print(char[] chars) {write(new String(chars));} | |
public void println(Object obj) {write(obj + "\n");} | |
public void println(String s) {write(s + "\n");} | |
public void println(long l) {write(l + "\n");} | |
public void println(int i) {write(i + "\n");} | |
public void println(double d) {write(d + "\n");} | |
public void println(float f) {write(f + "\n");} | |
public void println(char c) {write(Character.toString(c) + "\n");} | |
public void println(boolean bool) {write(bool + "\n");} | |
public void println() {write("\n");} | |
public void println(char[] chars) {write(new String(chars) + "\n");} | |
public PrintStream printf(String format, Object... args) { | |
write(String.format(format, args)); | |
return this; | |
} | |
public PrintStream printf(Locale l, String format, Object... args) { | |
write(String.format(l, format, args)); | |
return this; | |
} | |
} | |
class ReflectiveAccessException extends RuntimeException{ | |
private static final long serialVersionUID = -4663638158646941065L; | |
protected ReflectiveAccessException() { | |
super("This method cannot be called reflectively"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment