Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
log
Browse files Browse the repository at this point in the history
  • Loading branch information
Enaium committed Sep 19, 2022
1 parent 9e47a19 commit eaec85c
Showing 2 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/cn/enaium/joe/Main.java
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import cn.enaium.joe.util.IOUtil;
import cn.enaium.joe.util.MessageUtil;
import cn.enaium.joe.util.ReflectUtil;
import cn.enaium.joe.util.TinyLogPrintStream;
import com.formdev.flatlaf.FlatDarkLaf;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
@@ -41,6 +42,9 @@
import java.nio.file.Paths;
import java.util.Objects;

import static cn.enaium.joe.util.TinyLogPrintStream.Type.STDERR;
import static cn.enaium.joe.util.TinyLogPrintStream.Type.STDOUT;

/**
* @author Enaium
*/
@@ -72,6 +76,8 @@ private static void agent(Instrumentation inst) throws IOException {

private static void launch() {
Configurator.currentConfig().writer(new ConsoleWriter(), "[{date: HH:mm:ss.SSS}] {level} > {message}").addWriter(new FileWriter("latest.log"), "[{date: HH:mm:ss.SSS}] {level} > {message}").activate();
System.setOut(new TinyLogPrintStream(System.out, STDOUT));
System.setErr(new TinyLogPrintStream(System.err, STDERR));

Logger.info("DIR:{}", System.getProperty("user.dir"));
FlatDarkLaf.setup();
58 changes: 58 additions & 0 deletions src/main/java/cn/enaium/joe/util/TinyLogPrintStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2022 Enaium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.enaium.joe.util;

import org.pmw.tinylog.Logger;

import java.io.OutputStream;
import java.io.PrintStream;

/**
* @author Enaium
* @since 1.3.0
*/
public class TinyLogPrintStream extends PrintStream {

private final Type type;

public TinyLogPrintStream(OutputStream out, Type type) {
super(out);
this.type = type;
}

@Override
public void println(String x) {
log(x);
}

@Override
public void println(Object x) {
log(String.valueOf(x));
}

private void log(String log) {
if (type == Type.STDOUT) {
Logger.info(log);
} else if (type == Type.STDERR) {
Logger.error(log);
}
}

public enum Type {
STDOUT, STDERR
}
}

0 comments on commit eaec85c

Please sign in to comment.