diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/src/main/java/cc/sukazyo/messiva/Logger.java b/src/main/java/cc/sukazyo/messiva/Logger.java new file mode 100644 index 0000000..9230cd9 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/Logger.java @@ -0,0 +1,52 @@ +package cc.sukazyo.messiva; + +import cc.sukazyo.messiva.appender.IAppender; +import cc.sukazyo.messiva.log.Log; +import cc.sukazyo.messiva.log.LogLevel; +import cc.sukazyo.messiva.log.Message; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Logger { + + public final List appends = new ArrayList<>(); + + public Logger () {} + + public Logger (IAppender... appends) { + this.appends.addAll(Arrays.asList(appends)); + } + + public void trace (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.TRACE)); + } + + public void info (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.INFO)); + } + + public void warn (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN)); + } + + public void warning (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN)); + } + + public void error (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.ERROR)); + } + + public void fatal (String message) { + pushToAllAppender(new Log(1, new Message(message), LogLevel.FATAL)); + } + + private void pushToAllAppender (Log log) { + for (IAppender appender : appends) { + appender.pushLog(log); + } + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java b/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java new file mode 100644 index 0000000..af6e982 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java @@ -0,0 +1,46 @@ +package cc.sukazyo.messiva.appender; + +import cc.sukazyo.messiva.log.Log; +import cc.sukazyo.messiva.utils.StringUtils; + +import java.util.concurrent.locks.ReentrantLock; + +public class ConsoleAppender implements IAppender { + + private final ReentrantLock syncLock = new ReentrantLock(); + +// private final AppendDaemon DAEMON; +// private static class AppendDaemon extends Thread { +// public AppendDaemon () { this.setName("console-appender-daemon::" + UUID.randomUUID()); } +// @Override public void run () { +// +// } +// } +// + public ConsoleAppender() { +// DAEMON = new AppendDaemon(); +// DAEMON.start(); + } + + @Override + public void pushLog (Log log) { + syncLock.lock(); + System.out.println(formatMessage(log)); + syncLock.unlock(); + } + + private static String formatMessage (Log log) { + final StringBuilder message = new StringBuilder(); + message.append('[').append(log.timestamp) + .append("][").append(log.thread.getName()) + .append("]"); + final String promptNewline = StringUtils.repeatChar('\'', message.length()); + message.append('[').append(log.level.tag).append(']').append(log.message.message[0]); + for (int i = 1; i < log.message.message.length; i++) { + message.append('\n').append(promptNewline) + .append('[').append(log.level.tag).append(']').append(log.message.message[i]); + } + return message.toString(); + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/appender/IAppender.java b/src/main/java/cc/sukazyo/messiva/appender/IAppender.java new file mode 100644 index 0000000..a88d362 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/appender/IAppender.java @@ -0,0 +1,9 @@ +package cc.sukazyo.messiva.appender; + +import cc.sukazyo.messiva.log.Log; + +public interface IAppender { + + void pushLog (Log log); + +} diff --git a/src/main/java/cc/sukazyo/messiva/log/Log.java b/src/main/java/cc/sukazyo/messiva/log/Log.java new file mode 100644 index 0000000..35948f7 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/log/Log.java @@ -0,0 +1,45 @@ +package cc.sukazyo.messiva.log; + +import cc.sukazyo.messiva.utils.StackUtils; + +public class Log { + + public final Message message; + public final LogLevel level; + public final long timestamp; + public final Thread thread; + public final StackTraceElement[] stackTrace; + + public Log (Message message, LogLevel level, Thread thread, long timestamp, StackTraceElement[] stackTrace) { + this.message = message; + this.level = level; + this.thread = thread; + this.timestamp = timestamp; + this.stackTrace = stackTrace; + } + + public Log (int stackOffset, Message message, LogLevel level, Thread thread, long timestamp) { + this(message, level, thread, timestamp, StackUtils.getStackTrace(stackOffset+1)); + } + + public Log (Message message, LogLevel level, Thread thread, long timestamp) { + this(1, message, level, thread, timestamp); + } + + public Log (int stackOffset, Message message, LogLevel level, Thread thread) { + this(stackOffset+1, message, level, thread, System.currentTimeMillis()); + } + + public Log (Message message, LogLevel level, Thread thread) { + this(1, message, level, thread); + } + + public Log (int stackOffset, Message message, LogLevel level) { + this(stackOffset+1, message, level, Thread.currentThread()); + } + + public Log (Message message, LogLevel level) { + this(1, message, level); + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/log/LogLevel.java b/src/main/java/cc/sukazyo/messiva/log/LogLevel.java new file mode 100644 index 0000000..0fe063c --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/log/LogLevel.java @@ -0,0 +1,19 @@ +package cc.sukazyo.messiva.log; + +public enum LogLevel { + + TRACE(-1f, "TRAC"), + INFO(0f, "INFO"), + WARN(0.5f, "WARN"), + ERROR(1f, "ERRO"), + FATAL(10f, "FTAL"); + + public final float level; + public final String tag; + + LogLevel (float level, String tag) { + this.level = level; + this.tag = tag; + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/log/Message.java b/src/main/java/cc/sukazyo/messiva/log/Message.java new file mode 100644 index 0000000..7830751 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/log/Message.java @@ -0,0 +1,11 @@ +package cc.sukazyo.messiva.log; + +public class Message { + + public final String[] message; + + public Message(String message) { + this.message = message.split("\n"); + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/utils/StackUtils.java b/src/main/java/cc/sukazyo/messiva/utils/StackUtils.java new file mode 100644 index 0000000..3bf5976 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/utils/StackUtils.java @@ -0,0 +1,14 @@ +package cc.sukazyo.messiva.utils; + +import java.util.Arrays; + +public class StackUtils { + + public static StackTraceElement[] getStackTrace (int offset) { + offset += 2; + StackTraceElement[] origins = Thread.currentThread().getStackTrace(); + origins = Arrays.copyOfRange(origins, offset, origins.length); + return origins; + } + +} diff --git a/src/main/java/cc/sukazyo/messiva/utils/StringUtils.java b/src/main/java/cc/sukazyo/messiva/utils/StringUtils.java new file mode 100644 index 0000000..a5f7672 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/utils/StringUtils.java @@ -0,0 +1,15 @@ +package cc.sukazyo.messiva.utils; + +import javax.annotation.Nonnull; +import java.util.Arrays; + +public class StringUtils { + + @Nonnull + public static String repeatChar (char c, int i) { + final char[] chars = new char[i]; + Arrays.fill(chars, c); + return new String(chars); + } + +}