From 80bc0640aab98423c1e5d16cace168068204855b Mon Sep 17 00:00:00 2001 From: MCXIV Date: Thu, 28 Jan 2021 13:51:15 +0530 Subject: [PATCH 1/3] Unfortunately v5 --- .idea/compiler.xml | 15 -- build.gradle | 2 +- .../mcxiv/logger/decorations/Decoration.java | 23 ---- .../mcxiv/logger/decorations/Decorations.java | 127 +++++++++++++++++ .../logger/decorations/RawFileDecoration.java | 62 --------- .../java/com/mcxiv/logger/formatted/FLog.java | 57 +++++++- .../formatted/Logger_AnnotationCompiler.java | 98 -------------- .../Logger_LevelDependencyAdder.java | 28 +--- .../formatted/Logger_MethodCollection.java | 13 +- .../formatted/Logger_MethodImplierBody.java | 92 +++++++++++++ .../mcxiv/logger/formatted/fixed/FileLog.java | 26 ++++ .../fixed/Logger_LevelDependencyAdder.java | 22 +++ .../formatted/fixed/Logger_LogFileWriter.java | 122 +++++++++++++++++ .../java/com/mcxiv/logger/packets/Packet.java | 29 ++++ .../mcxiv/logger/plotting/BoxBarGraph.java | 128 +++++++++--------- .../java/com/mcxiv/logger/plotting/Plot.java | 3 +- .../mcxiv/logger/plotting/SimpleBarGraph.java | 79 +++++------ .../com/mcxiv/logger/tables/BoxTable.java | 81 ++++++++++- .../com/mcxiv/logger/tables/EmptyTable.java | 32 ++++- .../com/mcxiv/logger/tables/StripesTable.java | 73 +++++++--- .../java/com/mcxiv/logger/tables/Table.java | 43 +++--- .../com/mcxiv/logger/tables/TableAdaptor.java | 9 +- .../ultimate/Logger_LevelDependencyAdder.java | 23 ++++ .../ultimate/Logger_MethodImplierBody.java | 79 +++++++++++ .../Logger_StreamDependencyAdder.java | 14 ++ .../java/com/mcxiv/logger/ultimate/ULog.java | 11 ++ .../mcxiv/logger/ultimate/ULogBuilder.java | 25 ++++ .../java/com/mcxiv/logger/util/Printer.java | 13 ++ .../decorations/RawFileDecorationTest.java | 22 --- .../Logger_AnnotationCompilerTest.java | 11 +- .../com/mcxiv/logger/plotting/PlotTest.java | 62 +++++---- .../com/mcxiv/logger/tables/TableTest.java | 54 +++++--- .../com/mcxiv/logger/ultimate/ULogTest.java | 100 ++++++++++++++ 33 files changed, 1104 insertions(+), 474 deletions(-) create mode 100644 src/main/java/com/mcxiv/logger/decorations/Decorations.java delete mode 100644 src/main/java/com/mcxiv/logger/formatted/Logger_AnnotationCompiler.java create mode 100644 src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java create mode 100644 src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java create mode 100644 src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java create mode 100644 src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java create mode 100644 src/main/java/com/mcxiv/logger/packets/Packet.java create mode 100644 src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java create mode 100644 src/main/java/com/mcxiv/logger/ultimate/Logger_MethodImplierBody.java create mode 100644 src/main/java/com/mcxiv/logger/ultimate/Logger_StreamDependencyAdder.java create mode 100644 src/main/java/com/mcxiv/logger/ultimate/ULog.java create mode 100644 src/main/java/com/mcxiv/logger/ultimate/ULogBuilder.java create mode 100644 src/main/java/com/mcxiv/logger/util/Printer.java delete mode 100644 src/test/java/com/mcxiv/logger/decorations/RawFileDecorationTest.java create mode 100644 src/test/java/com/mcxiv/logger/ultimate/ULogTest.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 7419393..2918ec6 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -21,21 +21,6 @@ - - - - - - - - - - - - - - - diff --git a/build.gradle b/build.gradle index ef1c485..df92648 100644 --- a/build.gradle +++ b/build.gradle @@ -32,4 +32,4 @@ jar { from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } -} +} \ No newline at end of file diff --git a/src/main/java/com/mcxiv/logger/decorations/Decoration.java b/src/main/java/com/mcxiv/logger/decorations/Decoration.java index ccb7390..160bfbb 100644 --- a/src/main/java/com/mcxiv/logger/decorations/Decoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/Decoration.java @@ -52,29 +52,6 @@ public Decoration(String... codes) { static Pattern re_SBcolor = Pattern.compile("([\\[][@]([A-Fa-f0-9]{6,8})[]])"); - public static Decoration getRandomDecoration() { - - RandomColor c = new RandomColor(); - - String[] codes = new String[]{ - ":#" + c.yieldHex() + ": ::", - ":#" + c.getBright().yieldHex() + ": ::", - ":#" + c.getDark().yieldHex() + ": ::", - ":#" + c.getBright().yieldHex() + ": ::" - }; - - switch (DECORATION_CLASS) { - case "com.mcxiv.logger.decorations.ConsoleDecoration": - return new ConsoleDecoration(codes); - - case "com.mcxiv.logger.decorations.TagDecoration": - return new TagDecoration(codes); - - default: - return new EmptyDecoration(); - } - } - public static String center(int len, String txt) { txt = String.format("%" + (len - txt.length()) / 2 + "s", " ") + txt; return String.format("%-" + len + "s", txt); diff --git a/src/main/java/com/mcxiv/logger/decorations/Decorations.java b/src/main/java/com/mcxiv/logger/decorations/Decorations.java new file mode 100644 index 0000000..18446b8 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/decorations/Decorations.java @@ -0,0 +1,127 @@ +package com.mcxiv.logger.decorations; + +import com.mcxiv.logger.tools.RandomColor; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Objects; + +public class Decorations { + + public static final String CONSOLE = "console"; + public static final String RAW_FILE = "raw file"; + public static final String TAG = "tag"; + public static final String EMPTY = "empty"; + + private static HashMap decorations_map = new HashMap<>(); + + public static Decoration get(String decorator) { + + StackTraceElement element = null; + StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (int i = 2; i < 6; i++) { + if (!(stackTrace[i].getClassName().endsWith("Logger_MethodImplierBody") || stackTrace[i].getClassName().endsWith("Logger_LogFileWriter"))) { + element = stackTrace[i]; + break; + } + } + if (element == null) element = Thread.currentThread().getStackTrace()[3]; + +// for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) +// System.out.println(stackTraceElement.getClassName() + " " + stackTraceElement.getMethodName()); + + Tag tag = new Tag(element.getClassName(), element.getMethodName(), decorator); + if (decorations_map.containsKey(tag)) + return decorations_map.get(tag); + + else + try { + Class clazz = Class.forName(element.getClassName()); + + Format format = null; + + for (Method m : clazz.getMethods()) + if (m.getName().equals(element.getMethodName())) + if (m.isAnnotationPresent(Format.class)) + format = m.getAnnotation(Format.class); + + if (format == null && element.getMethodName().equals("")) + for (Constructor c : clazz.getConstructors()) + if (c.isAnnotationPresent(Format.class)) + format = c.getAnnotation(Format.class); + + if (format == null) + if (clazz.isAnnotationPresent(Format.class)) + format = clazz.getAnnotation(Format.class); + + if (format != null) { + Decoration t = getSpecific(decorator, format.value()); + decorations_map.put(tag, t); + return t; + } + } catch (Exception ignored) { + } + + Decoration t = getRandom(decorator); + decorations_map.put(tag, t); + return t; + } + + public static Decoration getSpecific(String decorator, String... formats) { + switch (decorator) { + case CONSOLE: + return new ConsoleDecoration(formats); + case TAG: + return new TagDecoration(formats); + case RAW_FILE: + return new RawFileDecoration(formats); + case EMPTY: + default: + return new EmptyDecoration(); + } + } + + public static Decoration getRandom(String decorator) { + RandomColor c = new RandomColor(); + + String[] codes = new String[]{ + ":#" + c.yieldHex() + ": ::", + ":#" + c.getBright().yieldHex() + ": ::", + ":#" + c.getDark().yieldHex() + ": ::", + ":#" + c.getBright().yieldHex() + ": ::" + }; + + return getSpecific(decorator, codes); + } + + + private static class Tag { + + String className; + String executableName; + String decorator; + + public Tag(String className, String executableName, String decorator) { + this.className = className; + this.executableName = executableName; + this.decorator = decorator; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Tag tag = (Tag) o; + return Objects.equals(className, tag.className) && + Objects.equals(executableName, tag.executableName) && + Objects.equals(decorator, tag.decorator); + } + + @Override + public int hashCode() { + return Objects.hash(className, executableName, decorator); + } + } + +} diff --git a/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java b/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java index b4515c2..d90473e 100644 --- a/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java @@ -1,53 +1,11 @@ package com.mcxiv.logger.decorations; -import com.mcxiv.logger.tools.C; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.time.LocalDateTime; -import java.util.function.Function; -import java.util.function.Supplier; import java.util.regex.Matcher; public class RawFileDecoration extends Decoration { - private static Function getPartnerDecoration = ConsoleDecoration::new; - - public static void setPartnerDecorationDecoration(Function decoration) { - getPartnerDecoration = decoration; - } - - public static int MAX_BUFFER_SIZE = 200; - - Decoration decoration; - FileWriter writer; - StringBuilder buffer; - - - // - public RawFileDecoration(String... codes) { - this(resolveFile(), codes); - } - - private static File resolveFile() { - new File("logs").mkdir(); - return new File("logs/" + LocalDateTime.now().toString().replaceAll("[^a-zA-Z0-9]", ",") + ".txt"); - } - - public RawFileDecoration(File file, String... codes) { - if (codes.length == 0) return; - this.decoration = getPartnerDecoration.apply(codes); - - try { - writer = new FileWriter(file); - buffer = new StringBuilder(MAX_BUFFER_SIZE); - Runtime.getRuntime().addShutdownHook(new Thread(this::flush)); - } catch (IOException e) { - System.out.println(C.RBG + C.hex.font.to24Bit(255, 255, 255) + "IO Exception " + C.RS + C.Y + e.getCause() + C.RS); - } decorates = new Decorate[codes.length]; @@ -127,24 +85,4 @@ public RawFileDecoration(File file, String... codes) { } - @Override - public String decorate(String... input) { - if (writer != null) write(super.decorate(input.clone())); - return decoration.decorate(input); - } - - private void write(String msg) { - if (buffer.length() > MAX_BUFFER_SIZE) flush(); - buffer.append(msg); - } - - private void flush() { - try { - writer.write(buffer.toString()); - writer.flush(); - } catch (IOException e) { - System.out.println(C.RBG + C.hex.font.to24Bit(255, 255, 255) + "IO Exception " + C.RS + C.Y + e.getCause() + C.RS); - } - buffer.setLength(0); - } } diff --git a/src/main/java/com/mcxiv/logger/formatted/FLog.java b/src/main/java/com/mcxiv/logger/formatted/FLog.java index 185b785..4dd4d00 100644 --- a/src/main/java/com/mcxiv/logger/formatted/FLog.java +++ b/src/main/java/com/mcxiv/logger/formatted/FLog.java @@ -1,6 +1,7 @@ package com.mcxiv.logger.formatted; -import com.mcxiv.logger.tools.LogLevel; +import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.packets.Packet; import com.mcxiv.logger.util.ByteConsumer; import com.mcxiv.logger.util.LevelDependent; import com.mcxiv.logger.util.StringsConsumer; @@ -9,20 +10,66 @@ public abstract class FLog extends LevelDependent implements Logger_MethodCollection { + protected String decorator_name = Decorations.CONSOLE; + public static FLog getNew() { - return new Logger_AnnotationCompiler(); + return new Logger_MethodImplierBody(); } public static FLog getNew(OutputStream stream) { - return new Logger_AnnotationCompiler(stream); + return new Logger_MethodImplierBody(stream); } public static FLog getNew(ByteConsumer consumer) { - return new Logger_AnnotationCompiler(consumer); + return new Logger_MethodImplierBody(consumer); } public static FLog getNew(StringsConsumer consumer) { - return new Logger_AnnotationCompiler(consumer); + return new Logger_MethodImplierBody(consumer); + } + + @Override + public void setDecorationType(String name) { + decorator_name=name; } + @Override + public String getDecorationType() { + return decorator_name; + } + + protected static final FLog EMPTY_VESSEL = new FLog() { + @Override + public Packet newPacket() { + return null; + } + + @Override + public FLog provide() { + return null; + } + + @Override + public FLog provideEmpty() { + return null; + } + + @Override + public void prt(String... msg) { + } + + @Override + public void prt(Object... obj) { + } + + @Override + public void raw(String raw) { + } + + + @Override + public StringsConsumer prtf(String... format) { + return null; + } + }; } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_AnnotationCompiler.java b/src/main/java/com/mcxiv/logger/formatted/Logger_AnnotationCompiler.java deleted file mode 100644 index 2beddeb..0000000 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_AnnotationCompiler.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.mcxiv.logger.formatted; - -import com.mcxiv.logger.decorations.Decoration; -import com.mcxiv.logger.decorations.Format; -import com.mcxiv.logger.util.ByteConsumer; -import com.mcxiv.logger.util.StringsConsumer; - -import java.io.OutputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.HashMap; - -class Logger_AnnotationCompiler extends Logger_StreamDependencyAdder { - - public Logger_AnnotationCompiler(OutputStream stream) { - super(stream); - } - - public Logger_AnnotationCompiler() { - super(); - } - - public Logger_AnnotationCompiler(ByteConsumer consumer) { - super(consumer); - } - - public Logger_AnnotationCompiler(StringsConsumer consumer) { - super(consumer); - } - - static HashMap decorations = new HashMap<>(); - - private static Decoration getDecoration() { - - StackTraceElement element = Thread.currentThread().getStackTrace()[3]; - - String key = element.getClassName() + element.getMethodName(); - if (decorations.containsKey(key)) - return decorations.get(key); - - else - try { - Class clazz = Class.forName(element.getClassName()); - - Format format = null; - - for (Method m : clazz.getMethods()) - if (m.getName().equals(element.getMethodName())) - if (m.isAnnotationPresent(Format.class)) - format = m.getAnnotation(Format.class); - - if (format == null && element.getMethodName().equals("")) - for (Constructor c : clazz.getConstructors()) - if (c.isAnnotationPresent(Format.class)) - format = c.getAnnotation(Format.class); - - if (format == null) - if (clazz.isAnnotationPresent(Format.class)) - format = clazz.getAnnotation(Format.class); - - if (format != null) { - Decoration t = Decoration.getDecoration(format.value()); - decorations.put(key, t); - return t; - } - } catch (Exception ignored) { - } - - Decoration t = Decoration.getRandomDecoration(); - decorations.put(key, t); - return t; - } - - @Override - public void prt(String... msg) { - Decoration decoration = getDecoration(); - writer.consume(decoration.decorate(msg)); - } - - @Override - public void prt(Object... obj) { - Decoration decoration = getDecoration(); - String[] stf = new String[obj.length]; - for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); - writer.consume(decoration.decorate(stf)); - } - - @Override - public void raw(String raw) { - writer.consume(raw); - } - - @Override - public StringsConsumer prtf(String... format) { - Decoration decoration = Decoration.getDecoration(format); - return msg -> writer.consume(decoration.decorate(msg)); - } -} diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java index 98231c4..6d731c8 100644 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java @@ -1,11 +1,7 @@ package com.mcxiv.logger.formatted; -import com.mcxiv.logger.tools.LogLevel; -import com.mcxiv.logger.util.ByteConsumer; import com.mcxiv.logger.util.StringsConsumer; -import java.io.OutputStream; - abstract class Logger_LevelDependencyAdder extends FLog { public Logger_LevelDependencyAdder() { @@ -22,27 +18,5 @@ public FLog provideEmpty() { return EMPTY_VESSEL; } - private static final FLog EMPTY_VESSEL = new FLog() { - @Override - public FLog provide() { - return null; - } - @Override - public FLog provideEmpty() { - return null; - } - @Override - public void prt(String... msg) { - } - @Override - public void prt(Object... obj) { - } - @Override - public void raw(String raw) { - } - @Override - public StringsConsumer prtf(String... format) { - return null; - } - }; + } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodCollection.java b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodCollection.java index bea5ceb..e2eba7b 100644 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodCollection.java +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodCollection.java @@ -1,16 +1,15 @@ package com.mcxiv.logger.formatted; +import com.mcxiv.logger.packets.Packet; +import com.mcxiv.logger.util.Printer; import com.mcxiv.logger.util.StringsConsumer; -interface Logger_MethodCollection { +interface Logger_MethodCollection extends Printer { - void prt(String... msg); + void setDecorationType(String name); + String getDecorationType(); - void prt(Object... obj); - - void raw(String raw); - - StringsConsumer prtf(String... format); + Packet newPacket(); } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java new file mode 100644 index 0000000..f3759ec --- /dev/null +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java @@ -0,0 +1,92 @@ +package com.mcxiv.logger.formatted; + +import com.mcxiv.logger.decorations.Decoration; +import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.packets.Packet; +import com.mcxiv.logger.util.ByteConsumer; +import com.mcxiv.logger.util.StringsConsumer; + +import java.io.OutputStream; + +class Logger_MethodImplierBody extends Logger_StreamDependencyAdder { + + public Logger_MethodImplierBody(OutputStream stream) { + super(stream); + } + + public Logger_MethodImplierBody() { + super(); + } + + public Logger_MethodImplierBody(ByteConsumer consumer) { + super(consumer); + } + + public Logger_MethodImplierBody(StringsConsumer consumer) { + super(consumer); + } + + @Override + public void prt(String... msg) { + Decoration decoration = Decorations.get(Decorations.CONSOLE); + writer.consume(decoration.decorate(msg)); + } + + @Override + public void prt(Object... obj) { + Decoration decoration = Decorations.get(Decorations.CONSOLE); + String[] stf = new String[obj.length]; + for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); + writer.consume(decoration.decorate(stf)); + } + + @Override + public void raw(String raw) { + prtf("").consume(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + Decoration decoration = Decorations.getSpecific(Decorations.CONSOLE, format); + return msg -> writer.consume(decoration.decorate(msg)); + } + + @Override + public Packet newPacket() { + return new OurPacket(); + } + + private class OurPacket extends Packet{ + + @Override + public void prt(String... msg) { + Decoration decoration = Decorations.get(Decorations.CONSOLE); + builder.append(decoration.decorate(msg)); + } + + @Override + public void prt(Object... obj) { + Decoration decoration = Decorations.get(Decorations.CONSOLE); + String[] stf = new String[obj.length]; + for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); + builder.append(decoration.decorate(stf)); + } + + @Override + public void raw(String raw) { + prtf("").consume(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + Decoration decoration = Decorations.getSpecific(Decorations.CONSOLE, format); + return msg -> builder.append(decoration.decorate(msg)); + } + + @Override + public void consume() { + Logger_MethodImplierBody.this.raw(builder.toString()); + } + } + +} diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java b/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java new file mode 100644 index 0000000..1546403 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java @@ -0,0 +1,26 @@ +package com.mcxiv.logger.formatted.fixed; + +import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.formatted.FLog; + +import java.io.File; + +public abstract class FileLog extends FLog { + + public FileLog() { + decorator_name = Decorations.RAW_FILE; + } + + public static FLog getNew(String file) { + return new Logger_LogFileWriter(file); + } + + public static FLog getNew(File file) { + return new Logger_LogFileWriter(file); + } + + @Override + public void setDecorationType(String name) { + } + +} diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java new file mode 100644 index 0000000..503e0e8 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java @@ -0,0 +1,22 @@ +package com.mcxiv.logger.formatted.fixed; + +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.util.StringsConsumer; + +abstract class Logger_LevelDependencyAdder extends FileLog { + + public Logger_LevelDependencyAdder() { + super(); + } + + @Override + public FLog provide() { + return this; + } + + @Override + public FLog provideEmpty() { + return EMPTY_VESSEL; + } + +} diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java new file mode 100644 index 0000000..020aae5 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java @@ -0,0 +1,122 @@ +package com.mcxiv.logger.formatted.fixed; + +import com.mcxiv.logger.decorations.Decoration; +import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.decorations.RawFileDecoration; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; +import com.mcxiv.logger.tables.Table; +import com.mcxiv.logger.util.StringsConsumer; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDateTime; + +class Logger_LogFileWriter extends Logger_LevelDependencyAdder { + + public static int MAX_BUFFER_SIZE = 200; + + FileWriter writer = null; + StringBuilder buffer; + + public Logger_LogFileWriter() { + this(LocalDateTime.now().toString().replaceAll("[^a-zA-Z0-9]", ",") + ".txt"); + } + + public Logger_LogFileWriter(String file) { + this(new File(String.format("logs%s%s", File.separator, file))); + } + + public Logger_LogFileWriter(File file) { + File fp = new File("logs"); + if (!fp.exists()) fp.mkdir(); + + try { + writer = new FileWriter(file, true); + buffer = new StringBuilder(MAX_BUFFER_SIZE); + Runtime.getRuntime().addShutdownHook(new Thread(this::flush)); + } catch (IOException e) { +// System.out.println(C.RBG + C.hex.font.to24Bit(255, 255, 255) + "IO Exception " + C.RS + C.Y + e.getCause() + C.RS); + Table.tabulate(FLog.getNew(), e); + } + } + + private void write(String msg) { + if (writer == null) return; + if (buffer.length() > MAX_BUFFER_SIZE) flush(); + buffer.append(msg); + } + + private void flush() { + if (writer == null) return; + try { + writer.append(buffer.toString()); + writer.flush(); + } catch (IOException e) { +// System.out.println(C.RBG + C.hex.font.to24Bit(255, 255, 255) + "IO Exception " + C.RS + C.Y + e.getCause() + C.RS); + Table.tabulate(FLog.getNew(), e); + } + buffer.setLength(0); + } + + @Override + public void prt(String... msg) { + write(Decorations.get("raw file").decorate(msg)); + } + + @Override + public void prt(Object... obj) { + String[] stf = new String[obj.length]; + for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); + write(Decorations.get("raw file").decorate(stf)); + } + + @Override + public void raw(String raw) { + prtf("").consume(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + return msg -> write(new RawFileDecoration(format).decorate(msg)); + } + + @Override + public Packet newPacket() { + return new OurPacket(); + } + + private class OurPacket extends Packet { + + @Override + public void prt(String... msg) { + Decoration decoration = Decorations.get(Decorations.RAW_FILE); + builder.append(decoration.decorate(msg)); + } + + @Override + public void prt(Object... obj) { + Decoration decoration = Decorations.get(Decorations.RAW_FILE); + String[] stf = new String[obj.length]; + for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); + builder.append(decoration.decorate(stf)); + } + + @Override + public void raw(String raw) { + prtf("").consume(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + return msg -> builder.append(new RawFileDecoration(format).decorate(msg)); + } + + @Override + public void consume() { + Logger_LogFileWriter.this.raw(builder.toString()); + } + } + +} diff --git a/src/main/java/com/mcxiv/logger/packets/Packet.java b/src/main/java/com/mcxiv/logger/packets/Packet.java new file mode 100644 index 0000000..1e59c58 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/packets/Packet.java @@ -0,0 +1,29 @@ +package com.mcxiv.logger.packets; + +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.util.Printer; +import com.mcxiv.logger.util.StringsConsumer; + +public abstract class Packet implements Printer { + + protected StringBuilder builder; + + public Packet() { + builder = new StringBuilder(); + } + + @Override + public abstract void prt(String... msg); + + @Override + public abstract void prt(Object... obj); + + @Override + public abstract void raw(String raw); + + @Override + public abstract StringsConsumer prtf(String... format); + + public abstract void consume(); + +} diff --git a/src/main/java/com/mcxiv/logger/plotting/BoxBarGraph.java b/src/main/java/com/mcxiv/logger/plotting/BoxBarGraph.java index e585579..155092b 100644 --- a/src/main/java/com/mcxiv/logger/plotting/BoxBarGraph.java +++ b/src/main/java/com/mcxiv/logger/plotting/BoxBarGraph.java @@ -1,8 +1,8 @@ package com.mcxiv.logger.plotting; import com.mcxiv.logger.boxUtilities.Box; -import com.mcxiv.logger.decorations.Decoration; -import com.mcxiv.logger.tools.C; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; import com.mcxiv.logger.tools.RandomColor; import com.mcxiv.logger.util.Iterator; @@ -64,7 +64,7 @@ public Plot.BarGraph values(int a, int b, Iterator its) { public Plot.BarGraph charHeight(int h) { double highestBar = Integer.MIN_VALUE; for (int i : values) if (highestBar < i) highestBar = i; - scale = (h - (title == null ? 1 : 2)) / highestBar; + scale = (h - (title == null ? 2 : 4)) / highestBar; return this; } @@ -81,14 +81,15 @@ public Plot.BarGraph setBarType(String bar) { } @Override - public String create() { + public void create(FLog mainLog) { + Packet packet = mainLog.newPacket(); // Getting the highest value provided here! int highestBar = Integer.MIN_VALUE; for (int i : values) if (highestBar < i) highestBar = i; // Getting the height of the highest bar in char count. - int charHeight = (int) (highestBar * scale); + int charHeight = Math.max(4, (int) (highestBar * scale)); // Longest Y Label, applied before the y axis. int longestYLabel = Integer.MIN_VALUE; @@ -100,58 +101,45 @@ public String create() { if (xLabels == null) xLabels = defaultXLables(values); for (String s : xLabels) if (longestXLabel < s.length()) longestXLabel = s.length(); - - // If xLabels exceed the bar height: - // set values of first xLabels such that they also contains all elements after - // . Thus giving the effect if the list is continued aside. - // Else they are not much modified. - // Also, apply an intend and respective bar colors. - String form = " %s%s" + C.RS + " %-" + longestXLabel + "s"; - for (int i = 0; i < Math.min(charHeight, xLabels.length); i++) { - String value = ""; - - for (int j = 0; j < Math.ceil(xLabels.length / (float) charHeight); j++) { - int k = j * charHeight + i; - if (k >= xLabels.length) - value += String.format(form, "", String.format("%" + bar.length() + "s", ""), ""); - else - value += String.format(form, RandomColor.getRandomAt(k), bar, xLabels[k]); - } - - xLabels[i] = value; + // Initialising a set of random colors so that they match the bars and the x labels. + // And setting xLabels to be of same length. + String form = "%-" + longestXLabel + "s"; + String[] colors = new String[xLabels.length]; + for (int i = 0; i < xLabels.length; i++) { + colors[i] = ":#" + new RandomColor().yieldHex() + ":"; + xLabels[i] = String.format(form, xLabels[i]); } -// longestXLabel = (int) (Math.ceil(xLabels.length / (float) (charHeight))) * (bar.length() + longestXLabel); // 4 of pad + 1 of bar + 1 of pad - longestXLabel = (int) (Math.ceil(xLabels.length / (float) (charHeight))) * (5 + bar.length() + longestXLabel); // - StringBuilder builder = new StringBuilder(); - if (title != null) { // centering the title about x axis and putting it. - builder.append("\n").append(Box.TL_DC); - for (int i = 0; i < longestYLabel + values.length * bar.length() + longestXLabel + 6; i++) // Beam until top border's and x label's separator's meeting - builder.append(Box.DB); - builder.append(Box.TR_DC).append("\n").append(Box.DP); + packet.raw("\n"); + packet.raw(Box.TL_DC); + for (int i = 0; i < longestYLabel + values.length * bar.length() + (longestXLabel + 6) * Math.ceil(xLabels.length / (float) charHeight) + 6; i++) // Beam until top border's and x label's separator's meeting + packet.raw(Box.DB); + packet.raw(Box.TR_DC); + packet.raw("\n"); + packet.raw(Box.DP); for (int i = 0; i < longestYLabel + 2; i++) - builder.append(" "); - builder.append(C.FB).append(Decoration.center(values.length * bar.length() + 3, title)).append(C.RS); - for (int i = 0; i < longestXLabel+1 ; i++) // Beam until top border's and x label's separator's meeting - builder.append(" "); - builder.append(Box.DP).append("\n"); + packet.raw(" "); + packet.prtf(":b%-" + (int) (values.length * bar.length() + (longestXLabel + 6) * Math.ceil(xLabels.length / (float) charHeight) + 4) + "s:").consume(title); + packet.raw(Box.DP); + packet.raw("\n"); } - builder.append(title == null ? Box.TL_DC : Box.R_DC); // Top Left Corner + packet.raw(title == null ? Box.TL_DC : Box.R_DC); // Top Left Corner for (int i = 0; i < longestYLabel + 1; i++) // Beam until top border's and y axis's meeting - builder.append(Box.DB); - builder.append("\u2564"); // Double Beam to Single Pillar down connector + packet.raw(Box.DB); + packet.raw("\u2564"); // Double Beam to Single Pillar down connector for (int i = 0; i < values.length * bar.length() + 2; i++) // Beam until top border's and x label's separator's meeting - builder.append(Box.DB); - builder.append(Box.B_DC); // Double Beam to Double Pillar down connector - for (int i = 0; i < longestXLabel+1; i++) // Double Beam to end connector - builder.append(Box.DB); - builder.append(title == null ? Box.TR_DC : Box.L_DC).append("\n"); // Top Right Corner + packet.raw(Box.DB); + packet.raw(Box.B_DC); // Double Beam to Double Pillar down connector + for (int i = 0; i < (longestXLabel + 6) * Math.ceil(xLabels.length / (float) charHeight) + 1; i++) // Double Beam to end connector + packet.raw(Box.DB); + packet.raw(title == null ? Box.TR_DC : Box.L_DC); + packet.raw("\n"); // Top Right Corner form = Box.DP + "%" + longestYLabel + "s \u2524 "; @@ -162,38 +150,52 @@ public String create() { // Adding in yLabels, or, a space if not enough provided. // Note that these names/space are formatted to have a padding and an axis fragment. if (dh >= charHeight - yLabels.length) - builder.append(String.format(form, yLabels[charHeight - dh - 1])); - else builder.append(String.format(form, "")); + packet.raw(String.format(form, yLabels[charHeight - dh - 1])); + else packet.raw(String.format(form, "")); // Putting in a Bar if bar is to be printed else a space. for (int dw = 0; dw < values.length; dw++) { // for each bar space - if (values[dw] * scale >= charHeight - dh) - builder.append(RandomColor.getRandomAt(dw)).append(bar); - else for (int i = 0; i < bar.length(); i++) builder.append(" "); + if (values[dw] * scale >= charHeight - dh)//{ + packet.prtf(colors[dw]).consume(bar); +// packet.raw(RandomColor.getRandomAt(dw));packet.raw(bar);} + else for (int i = 0; i < bar.length(); i++) packet.raw(" "); } - builder.append(" ").append(C.RS).append(Box.DP); + packet.raw(" ");/*packet.raw(C.RS);*/ + packet.raw(Box.DP); // Putting in xLabels if they are present. - if (xLabels != null && dh < xLabels.length) - builder.append(xLabels[dh]); + // if (xLabels != null && dh < xLabels.length) + // packet.raw(xLabels[dh]); + // Putting in all xLabels such that if they exceed the charheight they are printed appearing to exist in separate column. + for (int j = 0; j < Math.ceil(xLabels.length / (float) charHeight); j++) { + int s = dh + j * charHeight; + if (s < xLabels.length) + packet.prtf(":: : :", colors[s], ":: : :").consume("", bar, xLabels[s]); + } + + if (dh>=xLabels.length){ + packet.prtf(":: :%"+longestXLabel+"s:").consume(""); + } - builder.append(" ").append(Box.DP).append("\n").append(C.RS); + packet.raw(" "); + packet.raw(Box.DP); + packet.raw("\n");//packet.raw(C.RS); } - builder.append(Box.BL_DC); // Bottom Left Corner + packet.raw(Box.BL_DC); // Bottom Left Corner for (int i = 0; i < longestYLabel + 1; i++) // filling up Double Beam to cover ylabels - builder.append(Box.DB); - builder.append("\u2567"); // putting a Double Beam to Single Pillar up connector + packet.raw(Box.DB); + packet.raw("\u2567"); // putting a Double Beam to Single Pillar up connector for (int i = 0; i < values.length * bar.length() + 2; i++) // applying x axis - builder.append(Box.DB); - builder.append(Box.T_DC); // putting a Double Beam to Double Pillar up connector - for (int i = 0; i < longestXLabel + 1; i++) // filling up Double Beam to cover xlabels - builder.append(Box.DB); - builder.append(Box.BR_DC); // Bottom Right Corner + packet.raw(Box.DB); + packet.raw(Box.T_DC); // putting a Double Beam to Double Pillar up connector + for (int i = 0; i < (longestXLabel + 6) * Math.ceil(xLabels.length / (float) charHeight) + 1; i++) // filling up Double Beam to cover xlabels + packet.raw(Box.DB); + packet.raw(Box.BR_DC); // Bottom Right Corner - return builder.toString(); + packet.consume(); } private static String[] defaultXLables(int[] values) { diff --git a/src/main/java/com/mcxiv/logger/plotting/Plot.java b/src/main/java/com/mcxiv/logger/plotting/Plot.java index e181f1c..8274436 100644 --- a/src/main/java/com/mcxiv/logger/plotting/Plot.java +++ b/src/main/java/com/mcxiv/logger/plotting/Plot.java @@ -2,6 +2,7 @@ import com.mcxiv.logger.boxUtilities.Box; import com.mcxiv.logger.decorations.ConsoleDecoration; +import com.mcxiv.logger.formatted.FLog; import com.mcxiv.logger.util.Iterator; public class Plot { @@ -36,7 +37,7 @@ static BarGraph box() { BarGraph setBarType(String bar); - String create(); + void create(FLog log); } diff --git a/src/main/java/com/mcxiv/logger/plotting/SimpleBarGraph.java b/src/main/java/com/mcxiv/logger/plotting/SimpleBarGraph.java index ccde499..5d22dbb 100644 --- a/src/main/java/com/mcxiv/logger/plotting/SimpleBarGraph.java +++ b/src/main/java/com/mcxiv/logger/plotting/SimpleBarGraph.java @@ -1,8 +1,8 @@ package com.mcxiv.logger.plotting; import com.mcxiv.logger.boxUtilities.Box; -import com.mcxiv.logger.decorations.Decoration; -import com.mcxiv.logger.tools.C; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; import com.mcxiv.logger.tools.RandomColor; import com.mcxiv.logger.util.Iterator; @@ -14,7 +14,7 @@ class SimpleBarGraph implements Plot.BarGraph { int[] values = null; double scale = 0.1; - String bar = Box.B_F ; + String bar = Box.B_F; @Override public Plot.BarGraph title(String title) { @@ -81,7 +81,8 @@ public Plot.BarGraph setBarType(String bar) { } @Override - public String create() { + public void create(FLog mainLog) { + Packet packet = mainLog.newPacket(); // Getting the highest value provided here! int highestBar = Integer.MIN_VALUE; @@ -101,31 +102,18 @@ public String create() { for (String s : xLabels) if (longestXLabel < s.length()) longestXLabel = s.length(); - // If xLabels exceed the bar height: - // set values of first xLabels such that they also contains all elements after - // . Thus giving the effect if the list is continued aside. - // Else they are not much modified. - // Also, apply an intend and respective bar colors. - String form = " %s%s" + C.RS + " %-" + longestXLabel + "s"; - - for (int i = 0; i < Math.min(charHeight, xLabels.length); i++) { - String value = ""; - - for (int j = 0; j < Math.ceil(xLabels.length / (float) charHeight); j++) { - int k = j * charHeight + i; - if (k >= xLabels.length) continue; - value += String.format(form, RandomColor.getRandomAt(k), bar, xLabels[k]); - } - - xLabels[i] = value; + // Initialising a set of random colors so that they match the bars and the x labels. + // And setting xLabels to be of same length. + String form = "%-"+longestXLabel+"s"; + String[] colors = new String[xLabels.length]; + for (int i = 0; i < xLabels.length; i++) { + colors[i] = ":#" + new RandomColor().yieldHex() + ":"; + xLabels[i] = String.format(form, xLabels[i]); } // - - StringBuilder builder = new StringBuilder(); - form = " %" + longestYLabel + "s \u2528 "; // Note that these operations are evaluated row wise. @@ -133,45 +121,46 @@ public String create() { // Adding in yLabels, or, a space if not enough provided. // Note that these names/space are formatted to have a padding and an axis fragment. - if (dh >= charHeight - yLabels.length) - builder.append(String.format(form, yLabels[charHeight - dh - 1])); - else builder.append(String.format(form, "")); + if (dh >= charHeight - yLabels.length) { + packet.raw(String.format(form, yLabels[charHeight - dh - 1])); + } else packet.raw(String.format(form, "")); // Putting in a Bar if bar is to be printed else a space. for (int dw = 0; dw < values.length; dw++) { // for each bar space if (values[dw] * scale >= charHeight - dh) - builder.append(RandomColor.getRandomAt(dw)).append(bar); - else for (int i = 0; i < bar.length(); i++) builder.append(" "); + packet.prtf(colors[dw]).consume(bar); + else for (int i = 0; i < bar.length(); i++) packet.raw(" "); } - // Putting in xLabels if they are present. - if (xLabels != null && dh < xLabels.length) - builder.append(xLabels[dh]); + // Putting in all xLabels such that if they exceed the charheight they are printed appearing to exist in separate column. + for (int j = 0; j < Math.ceil(xLabels.length / (float) charHeight); j++) { + int s = dh + j * charHeight; + if (s < xLabels.length) + packet.prtf(":: : :", colors[s], ":: : :").consume("", bar, xLabels[s]); + } - builder.append("\n").append(C.RS); + packet.raw("\n"); } for (int i = 0; i < longestYLabel + 2; i++) // applying space in order to match up with the ylabels width and padding - builder.append(" "); - builder.append("\u2517"); // applying a bottom left corner + packet.raw(" "); + packet.raw("\u2517"); // applying a bottom left corner for (int i = 0; i < values.length * bar.length() + 2; i++) // applying x axis - builder.append("\u2501"); + packet.raw("\u2501"); - if (title != null) { // centering the title about x axis and putting it. - builder.append("\n"); - for (int i = 0; i < longestYLabel + 2; i++) - builder.append(" "); - builder.append(C.FB).append(Decoration.center(values.length * bar.length() + 3, title)).append(C.RS); - } + // centering the title about x axis and putting it. + if (title != null) + packet.prtf("::\n:b%*" + (values.length * bar.length() + (longestYLabel + 4) * 2) + "s:").consume(title); - return builder.toString(); +// mainLog.raw(builder.toString()); + packet.consume(); } private static String[] defaultXLables(int[] values) { - return Iterator.toArray(0, values.length, 1,i -> values[i]); + return Iterator.toArray(0, values.length, 1, i -> values[i]); } private static String[] defaultYLables(int highestBar, double scale) { - return Iterator.toArray(0, (int) (highestBar*scale),1 ,i -> String.format("%.3f", i/scale) ); + return Iterator.toArray(0, (int) (highestBar * scale), 1, i -> String.format("%.3f", i / scale)); } } diff --git a/src/main/java/com/mcxiv/logger/tables/BoxTable.java b/src/main/java/com/mcxiv/logger/tables/BoxTable.java index 16859e7..57ac0a9 100644 --- a/src/main/java/com/mcxiv/logger/tables/BoxTable.java +++ b/src/main/java/com/mcxiv/logger/tables/BoxTable.java @@ -2,6 +2,8 @@ import com.mcxiv.logger.boxUtilities.Box; import com.mcxiv.logger.decorations.Decoration; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; import java.util.ArrayList; @@ -78,6 +80,83 @@ public Table row(String... msg) { return this; } + @Override + public int getWidth() { + return 1+rowWidth.stream().reduce(0, Integer::sum) + rowWidth.size() * 4; + } + + @Override + public void create(FLog mainLog) { + Packet packet = mainLog.newPacket(); + + int w = rowWidth.size() * 3 + 1; // number of cells * (number of padding spaces provided per cell + border character per cell) + one extra border character which lyes unpaired with the cells. + for (Integer rw : rowWidth) w += rw; // adding up individual row widths to w + // now, w = table width. + + if (title != null) { + + packet.raw(Box.TL_DC); + for (int i = 0; i < w - 2; i++) packet.raw(Box.DB); + packet.raw(Box.TR_DC);packet.raw("\n"); + + packet.prtf(Box.DP + ":: :bn%*"+(w-4)+"s: ::" + Box.DP).consume(title); + + packet.raw(Box.R_DC); + for (int i = 0; i < rowWidth.size(); i++) { + for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); + if (i != rowWidth.size() - 1) packet.raw(Box.B_DC); + } + packet.raw(Box.L_DC);packet.raw("\n"); + + } else { + packet.raw(Box.TL_DC); + for (int i = 0; i < rowWidth.size(); i++) { + for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); + if (i != rowWidth.size() - 1) packet.raw(Box.B_DC); + } + packet.raw(Box.TR_DC);packet.raw("\n"); + } + + // Adjusting each header element to fit it's respective row width. + for (int i = 0; i < header.length; i++) + header[i] = String.format("%" + rowWidth.get(i) + "s", header[i]); + + // filling in values of header into head form + packet.prtf(Box.DP + ":: :b: ::" + Box.DP, ":: :b~: ::" + Box.DP).consume(header); + packet.raw("\n"); + + + packet.raw(Box.R_DC); + for (int i = 0; i < rowWidth.size(); i++) { + for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); + if (i != rowWidth.size() - 1) packet.raw(Box.A_DC); + } + packet.raw(Box.L_DC);packet.raw("\n"); + + // Adjusting each row element to fit it's respective row width. + for (String[] row : rows) + for (int j = 0; j < row.length; j++) + row[j] = String.format("%" + rowWidth.get(j) + "s", row[j]); + + // for every row {filling in values of that row into row form and then appending it after header} + for (String[] row : rows) { + packet.prtf(Box.DP + ":: :b: ::" + Box.DP, ":: :b~: ::" + Box.DP).consume(row); + packet.raw("\n"); + } + + packet.raw(Box.BL_DC); + for (int i = 0; i < rowWidth.size(); i++) { + for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); + if (i != rowWidth.size() - 1) packet.raw(Box.T_DC); + } + packet.raw(Box.BR_DC);packet.raw("\n"); + + packet.consume(); + + } +} + +/* @Override public String create() { // Initialising all formats, if not specified, the default is used. @@ -162,4 +241,4 @@ public String create() { return table.toString(); } -} + */ \ No newline at end of file diff --git a/src/main/java/com/mcxiv/logger/tables/EmptyTable.java b/src/main/java/com/mcxiv/logger/tables/EmptyTable.java index 4880122..d82542b 100644 --- a/src/main/java/com/mcxiv/logger/tables/EmptyTable.java +++ b/src/main/java/com/mcxiv/logger/tables/EmptyTable.java @@ -1,5 +1,8 @@ package com.mcxiv.logger.tables; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; + import java.util.ArrayList; class EmptyTable extends TableAdaptor { @@ -35,6 +38,7 @@ public Table formatHead(String... codes) { return this; } + @Override public Table title(String title) { this.title = title; @@ -58,6 +62,31 @@ public Table row(String... msg) { } @Override + public int getWidth() { + return rowWidth.stream().reduce(0, Integer::sum) + rowWidth.size() * 2; + } + + @Override + public void create(FLog mainLog) { + Packet packet = mainLog.newPacket(); + + StringBuilder table = new StringBuilder(); + + if (title != null) packet.prtf(":: :n:").consume(title); + + for (String[] row : rows) { + for (int i = 0; i < row.length; i++) + packet.prtf(":: :%-" + rowWidth.get(i) + "s: ::").consume(row[i]); + packet.raw("\n"); + } + + packet.consume(); + + } +} + + + /*@Override public String create() { StringBuilder table = new StringBuilder(); @@ -84,5 +113,4 @@ public String create() { } return table.toString(); - } -} + }*/ \ No newline at end of file diff --git a/src/main/java/com/mcxiv/logger/tables/StripesTable.java b/src/main/java/com/mcxiv/logger/tables/StripesTable.java index 7391d53..73b0c21 100644 --- a/src/main/java/com/mcxiv/logger/tables/StripesTable.java +++ b/src/main/java/com/mcxiv/logger/tables/StripesTable.java @@ -1,11 +1,9 @@ package com.mcxiv.logger.tables; -import com.mcxiv.logger.decorations.Decoration; -import com.mcxiv.logger.util.GroupIterator; -import com.mcxiv.logger.util.Iterator; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; import java.util.ArrayList; -import java.util.Arrays; class StripesTable extends TableAdaptor { @@ -15,17 +13,17 @@ class StripesTable extends TableAdaptor { String title = null; String[] header; - Decoration titleFormat = null; - Decoration headFormat1 = null; - Decoration headFormat2 = null; - Decoration rowFormat1 = null; - Decoration rowFormat2 = null; + String titleFormat = null; + String headFormat1 = null; + String headFormat2 = null; + String rowFormat1 = null; + String rowFormat2 = null; - static Decoration TITLE_FORMAT = Decoration.getDecoration(":@eb:"); - static Decoration HEAD_FORMAT1 = Decoration.getDecoration(":@bb:"); - static Decoration HEAD_FORMAT2 = Decoration.getDecoration(":@cb:"); - static Decoration ROW_FORMAT1 = Decoration.getDecoration(":@d:"); - static Decoration ROW_FORMAT2 = Decoration.getDecoration(":@e:"); + static String TITLE_FORMAT = "@eb"; + static String HEAD_FORMAT1 = "@bb"; + static String HEAD_FORMAT2 = "@cb"; + static String ROW_FORMAT1 = "@d"; + static String ROW_FORMAT2 = "@e"; public StripesTable() { rowWidth = new ArrayList<>(); @@ -40,14 +38,14 @@ public StripesTable() { */ @Override public Table format(String... codes) { - rowFormat1 = Decoration.getDecoration(codes[0]); - rowFormat2 = Decoration.getDecoration(codes[1]); + rowFormat1 = codes[0]; + rowFormat2 = codes[1]; return this; } @Override public Table formatTitle(String code) { - titleFormat = Decoration.getDecoration(code); + titleFormat = code; return this; } @@ -59,8 +57,8 @@ public Table formatTitle(String code) { */ @Override public Table formatHead(String... codes) { - headFormat1 = Decoration.getDecoration(codes[0]); - headFormat2 = Decoration.getDecoration(codes[1]); + headFormat1 = codes[0]; + headFormat2 = codes[1]; return this; } @@ -87,6 +85,40 @@ public Table row(String... msg) { } @Override + public int getWidth() { + return rowWidth.stream().reduce(0, Integer::sum) + rowWidth.size() * 2; + } + + @Override + public void create(FLog mainLog) { + Packet packet = mainLog.newPacket(); + + // Initialising all formats, if not specified, the default is used. + if (titleFormat == null) titleFormat = TITLE_FORMAT; + if (headFormat1 == null) headFormat1 = HEAD_FORMAT1; + if (headFormat2 == null) headFormat2 = HEAD_FORMAT2; + if (rowFormat1 == null) rowFormat1 = ROW_FORMAT1; + if (rowFormat2 == null) rowFormat2 = ROW_FORMAT2; + + if (title != null) + packet.prtf(":n%" + rowWidth.stream().reduce(0, Integer::sum) + "s" + titleFormat + ":").consume(title); + + for (int i = 0; i < header.length; i++) + packet.prtf(":: :%-" + rowWidth.get(i) + "s" + (i % 2 == 0 ? headFormat1 : headFormat2) + ": ::").consume(header[i]); + packet.raw("\n"); + + for (String[] row : rows) { + for (int i = 0; i < row.length; i++) + packet.prtf(":: :%-" + rowWidth.get(i) + "s" + (i % 2 == 0 ? rowFormat1 : rowFormat2) + ": ::").consume(row[i]); + packet.raw("\n"); + } + + packet.consume(); + } + +} + +/*@Override public String create() { // Initialising all formats, if not specified, the default is used. @@ -135,5 +167,4 @@ public String create() { for (String[] row : rows) table.append(String.format(row_form, (Object[]) row)); return table.toString(); - } -} + }*/ \ No newline at end of file diff --git a/src/main/java/com/mcxiv/logger/tables/Table.java b/src/main/java/com/mcxiv/logger/tables/Table.java index 9e32b6f..9f96566 100644 --- a/src/main/java/com/mcxiv/logger/tables/Table.java +++ b/src/main/java/com/mcxiv/logger/tables/Table.java @@ -26,12 +26,17 @@ static String[] form(Object... obj) { } - @Format({ - ":: :@a00 #F b %-40s: ::", - ":: :$B @ee0 u n %-63s:", - ":: at :@ffc %-100s x\nx n:" - }) - static void tabulate(FLog log, Exception e) { + static void tabulate(FLog log, Throwable e) { + + Table table = Table.stripped().formatHead("b@ffa", "b@ffc").format("@ffc", "@ffa") + .head("Package", "Class", "Method", "Line no."); + + for (StackTraceElement ele : e.getStackTrace()) { + int i = 0; + for (; i < ele.getClassName().length(); i++) + if (Character.isUpperCase(ele.getClassName().charAt(i))) break; + table.row(ele.getClassName().substring(0, i - 1), ele.getClassName().substring(i), ele.getMethodName(), "" + ele.getLineNumber()); + } String name = e.getClass().getName(); name = name.substring(name.lastIndexOf(".") + 1); @@ -39,28 +44,22 @@ static void tabulate(FLog log, Exception e) { String msg = e.getMessage(); msg = msg == null ? "" : msg; - String elem = ""; + log.prtf(":: :@a00 #F b %-40s: ::", + ":: :$B @ee0 u n %-" + (table.getWidth() - Math.max(40, name.length())-3) + "s:") + .consume(name, msg); - for (StackTraceElement ele : e.getStackTrace()) { - int i = 0; - for (; i < ele.getClassName().length(); i++) - if (Character.isUpperCase(ele.getClassName().charAt(i))) break; - String pkg = ele.getClassName().substring(0, i - 1); - elem += String.format("%-30s %30s %-25s %d\n", ele.getClassName().substring(0, i - 1), ele.getClassName().substring(i), ele.getMethodName(), ele.getLineNumber()); - } + table.create(log); - for (Throwable se : e.getSuppressed()) - elem+=se+"\n"; - Throwable ourCause = e.getCause(); - if (ourCause != null) - elem+=ourCause+"\n"; +// Throwable ourCause = e.getCause(); +// if (ourCause != null) +// elem += ourCause + "\n"; // for (Throwable throwable : e.getSuppressed()) { // // } - log.prt(name, msg, elem); +// log.prt(name, msg, elem); } @@ -95,6 +94,8 @@ static void tabulate(FLog log, Exception e) { Table formatHead(String... codes); - String create(); + int getWidth(); + + void create(FLog mainLog); } diff --git a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java index 14d304a..e9948c3 100644 --- a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java +++ b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java @@ -1,5 +1,6 @@ package com.mcxiv.logger.tables; +import com.mcxiv.logger.formatted.FLog; import com.mcxiv.logger.util.GroupIterator; import com.mcxiv.logger.util.Iterator; import com.mcxiv.logger.util.LevelDependent; @@ -40,8 +41,12 @@ public Table formatHead(String... codes) { } @Override - public String create() { - return ""; + public int getWidth() { + return 0; + } + + @Override + public void create(FLog mainLog) { } }; diff --git a/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java new file mode 100644 index 0000000..864edb0 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java @@ -0,0 +1,23 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.util.StringsConsumer; + +abstract class Logger_LevelDependencyAdder extends FLog { + + public Logger_LevelDependencyAdder() { + super(); + } + + @Override + public FLog provide() { + return this; + } + + @Override + public FLog provideEmpty() { + return EMPTY_VESSEL; + } + + +} diff --git a/src/main/java/com/mcxiv/logger/ultimate/Logger_MethodImplierBody.java b/src/main/java/com/mcxiv/logger/ultimate/Logger_MethodImplierBody.java new file mode 100644 index 0000000..8178a8e --- /dev/null +++ b/src/main/java/com/mcxiv/logger/ultimate/Logger_MethodImplierBody.java @@ -0,0 +1,79 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.decorations.Decoration; +import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.Packet; +import com.mcxiv.logger.util.StringsConsumer; + +class Logger_MethodImplierBody extends Logger_StreamDependencyAdder { + + public Logger_MethodImplierBody(FLog... loggers) { + super(loggers); + } + + @Override + public void prt(String... msg) { + for (FLog logger : loggers) logger.prt(msg.clone()); + } + + @Override + public void prt(Object... obj) { + for (FLog logger : loggers) logger.prt(obj.clone()); + } + + @Override + public void raw(String raw) { + for (FLog logger : loggers) logger.raw(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + return msg -> { + for (FLog logger : loggers) logger.prtf(format).consume(msg.clone()); + }; + } + + @Override + public Packet newPacket() { + return new OurPacket(); + } + + private class OurPacket extends Packet { + + Packet[] packets; + + public OurPacket() { + packets = new Packet[loggers.length]; + for (int i = 0; i < loggers.length; i++) packets[i] = loggers[i].newPacket(); + } + + @Override + public void prt(String... msg) { + for (Packet packet : packets) packet.prt(msg.clone()); + } + + @Override + public void prt(Object... obj) { + for (Packet packet : packets) packet.prt(obj.clone()); + } + + @Override + public void raw(String raw) { + for (Packet packet : packets) packet.raw(raw); + } + + @Override + public StringsConsumer prtf(String... format) { + return msg -> { + for (Packet packet : packets) packet.prtf(format).consume(msg.clone()); + }; + } + + @Override + public void consume() { + for (Packet packet : packets) packet.consume(); + } + } + +} diff --git a/src/main/java/com/mcxiv/logger/ultimate/Logger_StreamDependencyAdder.java b/src/main/java/com/mcxiv/logger/ultimate/Logger_StreamDependencyAdder.java new file mode 100644 index 0000000..c0a879c --- /dev/null +++ b/src/main/java/com/mcxiv/logger/ultimate/Logger_StreamDependencyAdder.java @@ -0,0 +1,14 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.formatted.FLog; + +abstract class Logger_StreamDependencyAdder extends Logger_LevelDependencyAdder { + + FLog[] loggers; + + public Logger_StreamDependencyAdder(FLog...loggers) { + super(); + this.loggers = loggers; + } + +} diff --git a/src/main/java/com/mcxiv/logger/ultimate/ULog.java b/src/main/java/com/mcxiv/logger/ultimate/ULog.java new file mode 100644 index 0000000..d661fdf --- /dev/null +++ b/src/main/java/com/mcxiv/logger/ultimate/ULog.java @@ -0,0 +1,11 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.formatted.FLog; + +public abstract class ULog extends FLog { + + public static ULogBuilder forNew() { + return new ULogBuilder(); + } + +} diff --git a/src/main/java/com/mcxiv/logger/ultimate/ULogBuilder.java b/src/main/java/com/mcxiv/logger/ultimate/ULogBuilder.java new file mode 100644 index 0000000..26f3bbb --- /dev/null +++ b/src/main/java/com/mcxiv/logger/ultimate/ULogBuilder.java @@ -0,0 +1,25 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.formatted.FLog; + +import java.util.ArrayList; + +public class ULogBuilder { + + ULogBuilder() { + } + + private ArrayList loggersList = new ArrayList<>(); + + public ULogBuilder add(FLog log) { + loggersList.add(log); + return this; + } + + public FLog create() { + FLog[] loggers = new FLog[loggersList.size()]; + for (int i = 0; i < loggers.length; i++) loggers[i] = loggersList.get(i); + return new Logger_MethodImplierBody(loggers); + } + +} diff --git a/src/main/java/com/mcxiv/logger/util/Printer.java b/src/main/java/com/mcxiv/logger/util/Printer.java new file mode 100644 index 0000000..3fa0845 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/util/Printer.java @@ -0,0 +1,13 @@ +package com.mcxiv.logger.util; + +public interface Printer { + + void prt(String... msg); + + void prt(Object... obj); + + void raw(String raw); + + StringsConsumer prtf(String... format); + +} diff --git a/src/test/java/com/mcxiv/logger/decorations/RawFileDecorationTest.java b/src/test/java/com/mcxiv/logger/decorations/RawFileDecorationTest.java deleted file mode 100644 index edb49af..0000000 --- a/src/test/java/com/mcxiv/logger/decorations/RawFileDecorationTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mcxiv.logger.decorations; - -import com.mcxiv.logger.formatted.FLog; -import org.junit.Test; - -import java.io.File; - -public class RawFileDecorationTest { - - - @Test - @Format({":$B: ::", "::_:$GBG$Rn:", ":: :#ff00ff: ::", ":$R:", ":$BRn:"}) - public void Simple() { - FLog log = FLog.getNew(); -// Decoration.setDecoration(strings -> new RawFileDecoration(new File("src/test/resources/log.txt"), strings)); - Decoration.setDecoration(RawFileDecoration::new); - - for (int i = 0; i < 16 / 16; i++) - log.prt("Yo", "Hey", "oof", "Hey", " You!"); - - } -} \ No newline at end of file diff --git a/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java b/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java index 18d6407..97a5dd6 100644 --- a/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java +++ b/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java @@ -1,14 +1,9 @@ package com.mcxiv.logger.formatted; -import com.mcxiv.logger.decorations.Decoration; import com.mcxiv.logger.decorations.Format; -import com.mcxiv.logger.decorations.RawFileDecoration; -import com.mcxiv.logger.decorations.TagDecoration; import org.junit.Test; -import java.io.File; - public class Logger_AnnotationCompilerTest { @Test @@ -157,14 +152,14 @@ public void test_SufSufFix() { @Test @Format({":$B:", ":: :$GBG$R: ::", ":#ff00ff:", ":$R:", ":$BR:"}) public void test() { - FLog log = new Logger_AnnotationCompiler(); + FLog log = new Logger_MethodImplierBody(); log.prt("Hey!", "So", "How", "Do", "You", "Do", "?"); } @Test @Format({":$B:", ":: :$GBG$R: ::", ":#ff00ff:"}) public void test2() { - FLog log = new Logger_AnnotationCompiler(); + FLog log = new Logger_MethodImplierBody(); // Decoration.setDecoration(TagDecoration::new); // Decoration.setDecoration(TagDecoration.class); log.prt("Hey!", "So", "How", "Do", "You", "Do", "?"); @@ -173,7 +168,7 @@ public void test2() { @Test @Format({":$B:", ":: :$GBG$R: ::", ":#ff00ff:"}) public void testCustomFormat() { - FLog log = new Logger_AnnotationCompiler(); + FLog log = new Logger_MethodImplierBody(); log.prtf(":$B:", ":: :$GBG$R: ::", ":#ff00ff:").consume("Hey!", "So", "How", "Do", "You", "Do", "?"); log.prt(); } diff --git a/src/test/java/com/mcxiv/logger/plotting/PlotTest.java b/src/test/java/com/mcxiv/logger/plotting/PlotTest.java index 4c29b23..723d4b9 100644 --- a/src/test/java/com/mcxiv/logger/plotting/PlotTest.java +++ b/src/test/java/com/mcxiv/logger/plotting/PlotTest.java @@ -2,6 +2,7 @@ import com.mcxiv.logger.boxUtilities.Box; import com.mcxiv.logger.decorations.ConsoleDecoration; +import com.mcxiv.logger.decorations.Decoration; import com.mcxiv.logger.formatted.FLog; import org.junit.Test; @@ -19,10 +20,9 @@ public void SuperSimpleTest() { int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94, 4, 90, 44, 86, 86, 46, 20, 91, 89, 86, 47, 8, 45, 56}; - log.raw(Plot.BarGraph.box() + Plot.BarGraph.box() .values(age) - .create() - ); + .create(log); } @Test @@ -31,14 +31,13 @@ public void SimpleTest1() { int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94, 4, 90, 44, 86, 86, 46, 20, 91, 89, 86, 47, 8, 45, 56}; - log.raw(Plot.BarGraph.box() + Plot.BarGraph.box() .title("Visitor's Age Survey") .XLabel(0, age.length, i -> (age[i] > 18 ? "Adult " : "Minor ") + age[i]) .YLabel(0, 10, i -> i * 15) .scale(0.08) .values(age) - .create() - ); + .create(log); } @Test @@ -47,12 +46,11 @@ public void SimpleTest2() { int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94, 4, 90, 44, 86, 86, 46, 20, 91, 89, 86, 47, 8, 45, 56}; - log.raw(Plot.BarGraph.box() + Plot.BarGraph.box() .values(age) .charHeight(15) .setBarType(Box.B_T) - .create() - ); + .create(log); } @Test @@ -61,42 +59,46 @@ public void SimpleTest3() { int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94};//,6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94,6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94}; - log.raw(Plot.BarGraph.box() + Plot.BarGraph.box() // .title("Yo") - .values(age) - .setBarType("Hello") - .create() - ); + .values(age) + .setBarType("Hello") + .create(log); } @Test public void simple() { FLog log = FLog.getNew(); - log.raw(Plot.BarGraph.simple() - .title("Simple Test") -// .XLabel(0, 20, i -> "Title Name " + i) - .values(0, 20, i -> (int) (100 * Math.random())) -// .XLabel("1","2","3","4","5","6","4","5","6","4","5","6") -// .bar(10, 20, 70, 90, 50, 60, 20, 70, 90, 50, 60, 20, 70, 90, 50, 60, 20, 70, 90, 50, 60) - .charHeight(13) - .setBarType(Box.B_T) - .create() - ); + + Plot.BarGraph.simple() + .title("Simple Test") + .XLabel(0, 20, i -> "Title Name " + i) + .values(0, 20, i -> (int) (100 * Math.random())) + .charHeight(13) + .setBarType(Box.B_T) + .create(log); + + Plot.BarGraph.box() + .title("Simple Test") + .XLabel(0, 20, i -> "Title Name " + i) + .values(0, 20, i -> (int) (100 * Math.random())) + .charHeight(13) + .setBarType(Box.B_T) + .create(log); } @Test public void box() { FLog log = FLog.getNew(); - log.raw(Plot.BarGraph.box() - .title("Simple Test") + Plot.BarGraph.box() + .title("Simple Test") // .XLabel(0, 20, i -> "Title Name " + i) - .YLabel(0, 4, i -> "M.Height " + i) - .values(0, 20, i -> (int) (100 * Math.random())) + .YLabel(0, 4, i -> "M.Height " + i) + .values(0, 20, i -> (int) (100 * Math.random())) // .setBarType("(*)") - .create() - ); + .create(log); } diff --git a/src/test/java/com/mcxiv/logger/tables/TableTest.java b/src/test/java/com/mcxiv/logger/tables/TableTest.java index 381c0e7..9e091b1 100644 --- a/src/test/java/com/mcxiv/logger/tables/TableTest.java +++ b/src/test/java/com/mcxiv/logger/tables/TableTest.java @@ -2,12 +2,17 @@ import com.mcxiv.logger.formatted.FLog; import com.mcxiv.logger.tools.LogLevel; +import com.sun.xml.internal.ws.wsdl.parser.MemberSubmissionAddressingWSDLParserExtension; import org.junit.Test; +import javax.lang.model.element.ExecutableElement; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.UnknownFormatConversionException; public class TableTest { + static FLog log = FLog.getNew(); + @Test public void SimpleTest() { @@ -23,31 +28,32 @@ public void SimpleTest() { Oce[i] = Ace[i] * Mice[i]; } - System.out.print(Table.stripped() + Table.stripped() + .title("Yo") .head("S.No.", "Number 1", "Number 2", "Answer") .row("", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); - System.out.print(Table.box() + Table.box() .title("Hello") .head("S.No.", "Number 1", "Number 2", "Answer") .row("!", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); - System.out.print(Table.empty() + Table.empty() .title("Hello") .head("S.No.", "Number 1", "Number 2", "Answer") .row("!", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); } @Test @@ -67,24 +73,24 @@ public void LevelTest() { FLog log = FLog.getNew(); - log.raw(Table.stripped().warn() + Table.stripped().warn() .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); LogLevel.VITAL.activate(); - log.raw(Table.stripped().warn() + Table.stripped().warn() .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); // LogLevel.ALL.activate(); - log.raw(Table.stripped().warn() + Table.stripped().warn() .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); } @@ -96,7 +102,7 @@ public void BunchTest() { Double[] iterations = new Double[len]; for (int i = 0; i < len; i++) iterations[i] = Math.random(); - System.out.print(Table.stripped() + Table.stripped() .title("Average of Random Numbers") .head("S.No.", "Range", "Average") .bunch(iterations, len / 10, @@ -107,9 +113,9 @@ public void BunchTest() { .formatTitle(":@4085eeb:") .formatHead(":@2565ae#fff:", ":@0f5298#fff:") .format(":@66d3fa:", ":@55d3fe:") - .create()); + .create(log); - System.out.print(Table.box() + Table.box() .title("Average of Random Numbers") .head("S.No.", "Range", "Average") .bunch(iterations, len / 10, @@ -117,9 +123,9 @@ public void BunchTest() { (gi, g) -> (gi * len / 10) + "-" + ((gi + 1) * len / 10), (gi, g) -> String.format("%.3f", avg(g) ) ) - .create()); + .create(log); - System.out.print(Table.empty() + Table.empty() .title("Average of Random Numbers") .head("S.No.", "Range", "Average") .bunch(iterations, len / 10, @@ -127,7 +133,7 @@ public void BunchTest() { (gi, g) -> (gi * len / 10) + "-" + ((gi + 1) * len / 10), (gi, g) -> String.format("%.3f", avg(g) ) ) - .create()); + .create(log); } @@ -144,8 +150,13 @@ public void TabulationTest() { FLog log = FLog.getNew(); try { - log.prt(String.format("%*s", "Hey")); - } catch (UnknownFormatConversionException e) { + + throw new InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneIconifyButtonPainter(); +// throw new SQLIntegrityConstraintViolationException("Yo"); +// int k = 1/0; +// log.prt(String.format("%*s", "Hey")); + + } catch (Throwable e) { Table.tabulate(log, e); @@ -154,4 +165,7 @@ public void TabulationTest() { } } + + private class InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneIconifyButtonPainter extends Throwable { + } } \ No newline at end of file diff --git a/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java b/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java new file mode 100644 index 0000000..307dbd1 --- /dev/null +++ b/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java @@ -0,0 +1,100 @@ +package com.mcxiv.logger.ultimate; + +import com.mcxiv.logger.boxUtilities.Box; +import com.mcxiv.logger.decorations.Format; +import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.formatted.fixed.FileLog; +import com.mcxiv.logger.plotting.Plot; +import com.mcxiv.logger.tables.Table; +import org.junit.Test; + +public class ULogTest { + + @Test + @Format({":$Btn:", ":$Ytt n:"}) + public void simple() { + + FLog log = ULog.forNew() + .add(FLog.getNew()) + .add(FileLog.getNew("new.txt")) + .create(); + + log.prt("A","B"); + +// Plot.BarGraph.box() +// .values(0,10,i -> i).create(); + + new Thread(() -> log.prtf(":n:").consume("Yo")).start(); + + Plot.BarGraph.simple() + .title("Simple Test") + .XLabel(0, 20, i -> "Title Name " + i) + .values(0, 20, i -> (int) (100 * Math.random())) + .setBarType(Box.B_T) + .charHeight(30) + .create(log); + + Plot.BarGraph.box() + .title("Simple box Test") + .XLabel(0, 20, i -> "Title Name " + i) + .values(0, 20, i -> (int) (100 * Math.random())) + .setBarType(Box.B_T) + .charHeight(30) + .create(log); + } + + @Test + public void tableTest() { + + FLog log = ULog.forNew() + .add(FLog.getNew()) + .add(FileLog.getNew("new.txt")) + .create(); + + int len = 10; + + int[] Ace = new int[len]; + int[] Mice = new int[len]; + int[] Oce = new int[len]; + + for (int i = 0; i < len; i++) { + Ace[i] = (int) (Math.random() * 10); + Mice[i] = (int) (Math.random() * 10); + Oce[i] = Ace[i] * Mice[i]; + } + + + Table.stripped() + .head("S.No.", "Number 1", "Number 2", "Answer") + .row("", "A", "B", "A x B") + .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .row("", "Only", "Even", "Values") + .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .create(log); + + new Thread(() -> { + for (int i = 0; i < 10; i++) { + log.prtf(":n:").consume("Yo"); + } + }).start(); + + Table.box() + .title("%%s") + .head("S.No.", "Number 1", "Number 2", "Answer") + .row("!", "A", "B", "A x B") + .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .row("", "Only", "Even", "Values") + .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .create(log); + + Table.empty() + .title("Hello") + .head("S.No.", "Number 1", "Number 2", "Answer") + .row("!", "A", "B", "A x B") + .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .row("", "Only", "Even", "Values") + .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) + .create(log); + + } +} \ No newline at end of file From 46d2b483fc9530e48cea7bdd4d67e7c3593c1e67 Mon Sep 17 00:00:00 2001 From: MCXIV Date: Thu, 28 Jan 2021 21:41:56 +0530 Subject: [PATCH 2/3] Lambda Logger Added --- .../mcxiv/logger/decorations/Decorations.java | 3 +- .../java/com/mcxiv/logger/formatted/FLog.java | 27 +++++---- .../Logger_LevelDependencyAdder.java | 29 +++++++-- .../formatted/Logger_MethodImplierBody.java | 1 + .../fixed/Logger_LevelDependencyAdder.java | 29 +++++++-- .../mcxiv/logger/packets/LambdaPacket.java | 52 ++++++++++++++++ .../com/mcxiv/logger/tables/BoxTable.java | 21 ++++--- .../com/mcxiv/logger/tables/EmptyTable.java | 3 + .../com/mcxiv/logger/tables/StripesTable.java | 2 + .../java/com/mcxiv/logger/tables/Table.java | 11 ++-- .../com/mcxiv/logger/tables/TableAdaptor.java | 60 +++---------------- .../ultimate/Logger_LevelDependencyAdder.java | 28 +++++++-- ...pendent.java => LevelDependencyAdder.java} | 27 +++++---- .../mcxiv/logger/util/StringsConsumer.java | 2 + .../Logger_LevelDependencyAdderTest.java | 21 +++---- .../com/mcxiv/logger/tables/TableTest.java | 7 ++- .../com/mcxiv/logger/ultimate/ULogTest.java | 3 + 17 files changed, 204 insertions(+), 122 deletions(-) create mode 100644 src/main/java/com/mcxiv/logger/packets/LambdaPacket.java rename src/main/java/com/mcxiv/logger/util/{LevelDependent.java => LevelDependencyAdder.java} (56%) diff --git a/src/main/java/com/mcxiv/logger/decorations/Decorations.java b/src/main/java/com/mcxiv/logger/decorations/Decorations.java index 18446b8..01195d2 100644 --- a/src/main/java/com/mcxiv/logger/decorations/Decorations.java +++ b/src/main/java/com/mcxiv/logger/decorations/Decorations.java @@ -21,7 +21,8 @@ public static Decoration get(String decorator) { StackTraceElement element = null; StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for (int i = 2; i < 6; i++) { - if (!(stackTrace[i].getClassName().endsWith("Logger_MethodImplierBody") || stackTrace[i].getClassName().endsWith("Logger_LogFileWriter"))) { + + if (!(stackTrace[i].getClassName().endsWith("Logger_MethodImplierBody") || stackTrace[i].getClassName().endsWith("Logger_LogFileWriter") || stackTrace[i].getClassName().contains("$"))) { element = stackTrace[i]; break; } diff --git a/src/main/java/com/mcxiv/logger/formatted/FLog.java b/src/main/java/com/mcxiv/logger/formatted/FLog.java index 4dd4d00..c68b845 100644 --- a/src/main/java/com/mcxiv/logger/formatted/FLog.java +++ b/src/main/java/com/mcxiv/logger/formatted/FLog.java @@ -1,14 +1,15 @@ package com.mcxiv.logger.formatted; import com.mcxiv.logger.decorations.Decorations; +import com.mcxiv.logger.packets.LambdaPacket; import com.mcxiv.logger.packets.Packet; import com.mcxiv.logger.util.ByteConsumer; -import com.mcxiv.logger.util.LevelDependent; +import com.mcxiv.logger.util.LevelDependencyAdder; import com.mcxiv.logger.util.StringsConsumer; import java.io.OutputStream; -public abstract class FLog extends LevelDependent implements Logger_MethodCollection { +public abstract class FLog implements Logger_MethodCollection, LevelDependencyAdder { protected String decorator_name = Decorations.CONSOLE; @@ -28,6 +29,12 @@ public static FLog getNew(StringsConsumer consumer) { return new Logger_MethodImplierBody(consumer); } + static String[] form(Object... obj) { + String[] msg = new String[obj.length]; + for (int i = 0; i < obj.length; i++) msg[i] = obj[i].toString(); + return msg; + } + @Override public void setDecorationType(String name) { decorator_name=name; @@ -38,19 +45,14 @@ public String getDecorationType() { return decorator_name; } - protected static final FLog EMPTY_VESSEL = new FLog() { + public static final FLog EMPTY_VESSEL = new FLog() { @Override - public Packet newPacket() { - return null; + public LambdaPacket provide() { + return LambdaPacket.EMPTY_VESSEL; } @Override - public FLog provide() { - return null; - } - - @Override - public FLog provideEmpty() { + public Packet newPacket() { return null; } @@ -66,10 +68,9 @@ public void prt(Object... obj) { public void raw(String raw) { } - @Override public StringsConsumer prtf(String... format) { - return null; + return st -> {}; } }; } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java index 6d731c8..2265ec2 100644 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdder.java @@ -1,5 +1,6 @@ package com.mcxiv.logger.formatted; +import com.mcxiv.logger.packets.LambdaPacket; import com.mcxiv.logger.util.StringsConsumer; abstract class Logger_LevelDependencyAdder extends FLog { @@ -9,14 +10,30 @@ public Logger_LevelDependencyAdder() { } @Override - public FLog provide() { - return this; + public LambdaPacket provide() { + return packet; } - @Override - public FLog provideEmpty() { - return EMPTY_VESSEL; - } + protected LambdaPacket packet = new LambdaPacket() { + @Override + public void prt(StringsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + + @Override + public void prt(ObjectsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + + @Override + public void raw(StringSupplier supplier) { + Logger_LevelDependencyAdder.this.raw(supplier.get()); + } + @Override + public StringsConsumer prtf(StringsSupplier supplier) { + return Logger_LevelDependencyAdder.this.prtf(supplier.get()); + } + }; } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java index f3759ec..9fa9d0a 100644 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java @@ -86,6 +86,7 @@ public StringsConsumer prtf(String... format) { @Override public void consume() { Logger_MethodImplierBody.this.raw(builder.toString()); +// builder.setLength(0); } } diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java index 503e0e8..33325ad 100644 --- a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LevelDependencyAdder.java @@ -1,6 +1,7 @@ package com.mcxiv.logger.formatted.fixed; import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.LambdaPacket; import com.mcxiv.logger.util.StringsConsumer; abstract class Logger_LevelDependencyAdder extends FileLog { @@ -10,13 +11,29 @@ public Logger_LevelDependencyAdder() { } @Override - public FLog provide() { - return this; + public LambdaPacket provide() { + return packet; } - @Override - public FLog provideEmpty() { - return EMPTY_VESSEL; - } + protected LambdaPacket packet = new LambdaPacket() { + @Override + public void prt(StringsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + + @Override + public void prt(ObjectsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + + @Override + public void raw(StringSupplier supplier) { + Logger_LevelDependencyAdder.this.raw(supplier.get()); + } + @Override + public StringsConsumer prtf(StringsSupplier supplier) { + return Logger_LevelDependencyAdder.this.prtf(supplier.get()); + } + }; } diff --git a/src/main/java/com/mcxiv/logger/packets/LambdaPacket.java b/src/main/java/com/mcxiv/logger/packets/LambdaPacket.java new file mode 100644 index 0000000..41f6646 --- /dev/null +++ b/src/main/java/com/mcxiv/logger/packets/LambdaPacket.java @@ -0,0 +1,52 @@ +package com.mcxiv.logger.packets; + +import com.mcxiv.logger.util.StringsConsumer; + +import java.util.function.Supplier; + +public abstract class LambdaPacket { + + public abstract void prt(StringsSupplier supplier); + + public abstract void prt(ObjectsSupplier supplier); + + public abstract void raw(StringSupplier supplier); + + public abstract StringsConsumer prtf(StringsSupplier supplier); + + public interface StringSupplier extends Supplier { + + } + + public interface StringsSupplier extends Supplier { + + } + + public interface ObjectsSupplier extends Supplier { + + } + + + public static LambdaPacket EMPTY_VESSEL = new LambdaPacket() { + @Override + public void prt(StringsSupplier supplier) { + + } + + @Override + public void prt(ObjectsSupplier supplier) { + + } + + @Override + public void raw(StringSupplier supplier) { + + } + + @Override + public StringsConsumer prtf(StringsSupplier supplier) { + return null; + } + }; + +} diff --git a/src/main/java/com/mcxiv/logger/tables/BoxTable.java b/src/main/java/com/mcxiv/logger/tables/BoxTable.java index 57ac0a9..261b034 100644 --- a/src/main/java/com/mcxiv/logger/tables/BoxTable.java +++ b/src/main/java/com/mcxiv/logger/tables/BoxTable.java @@ -82,11 +82,13 @@ public Table row(String... msg) { @Override public int getWidth() { - return 1+rowWidth.stream().reduce(0, Integer::sum) + rowWidth.size() * 4; + return 1 + rowWidth.stream().reduce(0, Integer::sum) + rowWidth.size() * 4; } @Override public void create(FLog mainLog) { + if(level!=null&&!level.accepted())return; + Packet packet = mainLog.newPacket(); int w = rowWidth.size() * 3 + 1; // number of cells * (number of padding spaces provided per cell + border character per cell) + one extra border character which lyes unpaired with the cells. @@ -97,16 +99,18 @@ public void create(FLog mainLog) { packet.raw(Box.TL_DC); for (int i = 0; i < w - 2; i++) packet.raw(Box.DB); - packet.raw(Box.TR_DC);packet.raw("\n"); + packet.raw(Box.TR_DC); + packet.raw("\n"); - packet.prtf(Box.DP + ":: :bn%*"+(w-4)+"s: ::" + Box.DP).consume(title); + packet.prtf(Box.DP + ":: :bn%*" + (w - 4) + "s: ::" + Box.DP).consume(title); packet.raw(Box.R_DC); for (int i = 0; i < rowWidth.size(); i++) { for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); if (i != rowWidth.size() - 1) packet.raw(Box.B_DC); } - packet.raw(Box.L_DC);packet.raw("\n"); + packet.raw(Box.L_DC); + packet.raw("\n"); } else { packet.raw(Box.TL_DC); @@ -114,7 +118,8 @@ public void create(FLog mainLog) { for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); if (i != rowWidth.size() - 1) packet.raw(Box.B_DC); } - packet.raw(Box.TR_DC);packet.raw("\n"); + packet.raw(Box.TR_DC); + packet.raw("\n"); } // Adjusting each header element to fit it's respective row width. @@ -131,7 +136,8 @@ public void create(FLog mainLog) { for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); if (i != rowWidth.size() - 1) packet.raw(Box.A_DC); } - packet.raw(Box.L_DC);packet.raw("\n"); + packet.raw(Box.L_DC); + packet.raw("\n"); // Adjusting each row element to fit it's respective row width. for (String[] row : rows) @@ -149,7 +155,8 @@ public void create(FLog mainLog) { for (int j = 0; j < rowWidth.get(i) + 2; j++) packet.raw(Box.DB); if (i != rowWidth.size() - 1) packet.raw(Box.T_DC); } - packet.raw(Box.BR_DC);packet.raw("\n"); + packet.raw(Box.BR_DC); + packet.raw("\n"); packet.consume(); diff --git a/src/main/java/com/mcxiv/logger/tables/EmptyTable.java b/src/main/java/com/mcxiv/logger/tables/EmptyTable.java index d82542b..fd58175 100644 --- a/src/main/java/com/mcxiv/logger/tables/EmptyTable.java +++ b/src/main/java/com/mcxiv/logger/tables/EmptyTable.java @@ -68,6 +68,9 @@ public int getWidth() { @Override public void create(FLog mainLog) { + if(level!=null&&!level.accepted())return; + + Packet packet = mainLog.newPacket(); StringBuilder table = new StringBuilder(); diff --git a/src/main/java/com/mcxiv/logger/tables/StripesTable.java b/src/main/java/com/mcxiv/logger/tables/StripesTable.java index 73b0c21..76f7419 100644 --- a/src/main/java/com/mcxiv/logger/tables/StripesTable.java +++ b/src/main/java/com/mcxiv/logger/tables/StripesTable.java @@ -91,6 +91,8 @@ public int getWidth() { @Override public void create(FLog mainLog) { + if(level!=null&&!level.accepted())return; + Packet packet = mainLog.newPacket(); // Initialising all formats, if not specified, the default is used. diff --git a/src/main/java/com/mcxiv/logger/tables/Table.java b/src/main/java/com/mcxiv/logger/tables/Table.java index 9f96566..fa06a02 100644 --- a/src/main/java/com/mcxiv/logger/tables/Table.java +++ b/src/main/java/com/mcxiv/logger/tables/Table.java @@ -2,8 +2,10 @@ import com.mcxiv.logger.decorations.Format; import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.tools.LogLevel; import com.mcxiv.logger.util.GroupIterator; import com.mcxiv.logger.util.Iterator; +import javafx.scene.control.Tab; public interface Table { @@ -19,13 +21,6 @@ static TableAdaptor empty() { return new EmptyTable(); } - static String[] form(Object... obj) { - String[] msg = new String[obj.length]; - for (int i = 0; i < obj.length; i++) msg[i] = obj[i].toString(); - return msg; - } - - static void tabulate(FLog log, Throwable e) { Table table = Table.stripped().formatHead("b@ffa", "b@ffc").format("@ffc", "@ffa") @@ -96,6 +91,8 @@ static void tabulate(FLog log, Throwable e) { int getWidth(); + Table setLogLevel(LogLevel level); + void create(FLog mainLog); } diff --git a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java index e9948c3..f5e5a3f 100644 --- a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java +++ b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java @@ -1,60 +1,24 @@ package com.mcxiv.logger.tables; import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.LambdaPacket; +import com.mcxiv.logger.tools.LogLevel; import com.mcxiv.logger.util.GroupIterator; import com.mcxiv.logger.util.Iterator; -import com.mcxiv.logger.util.LevelDependent; +import com.mcxiv.logger.util.LevelDependencyAdder; +import com.mcxiv.logger.util.StringsConsumer; import java.util.Arrays; -public abstract class TableAdaptor extends LevelDependent implements Table { - - static final TableAdaptor EMPTY_VESSEL = new TableAdaptor() { - @Override - public Table title(String title) { - return this; - } - - @Override - public Table head(String... msg) { - return this; - } - - @Override - public Table row(String... msg) { - return this; - } - - @Override - public Table format(String... codes) { - return this; - } - - @Override - public Table formatTitle(String code) { - return this; - } - - @Override - public Table formatHead(String... codes) { - return this; - } - - @Override - public int getWidth() { - return 0; - } - - @Override - public void create(FLog mainLog) { - } - }; +public abstract class TableAdaptor implements Table { // @Override // public Table iter(int a, int b, Iterator... its) { // return null; // } + protected LogLevel level = null; + @Override public Table iter(int b, Iterator... its) { return iter(0, b, 1, its); @@ -99,14 +63,8 @@ public final Table bunch(T[] main, int groupSize, GroupIterator... its) { } @Override - public TableAdaptor provide() { + public Table setLogLevel(LogLevel level) { + this.level = level; return this; } - - @Override - public TableAdaptor provideEmpty() { - return EMPTY_VESSEL; - } - - } diff --git a/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java b/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java index 864edb0..d6564b0 100644 --- a/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java +++ b/src/main/java/com/mcxiv/logger/ultimate/Logger_LevelDependencyAdder.java @@ -1,6 +1,7 @@ package com.mcxiv.logger.ultimate; import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.LambdaPacket; import com.mcxiv.logger.util.StringsConsumer; abstract class Logger_LevelDependencyAdder extends FLog { @@ -10,14 +11,29 @@ public Logger_LevelDependencyAdder() { } @Override - public FLog provide() { - return this; + public LambdaPacket provide() { + return packet; } - @Override - public FLog provideEmpty() { - return EMPTY_VESSEL; - } + protected LambdaPacket packet = new LambdaPacket() { + @Override + public void prt(StringsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + + @Override + public void prt(ObjectsSupplier supplier) { + Logger_LevelDependencyAdder.this.prt(supplier.get()); + } + @Override + public void raw(StringSupplier supplier) { + Logger_LevelDependencyAdder.this.raw(supplier.get()); + } + @Override + public StringsConsumer prtf(StringsSupplier supplier) { + return Logger_LevelDependencyAdder.this.prtf(supplier.get()); + } + }; } diff --git a/src/main/java/com/mcxiv/logger/util/LevelDependent.java b/src/main/java/com/mcxiv/logger/util/LevelDependencyAdder.java similarity index 56% rename from src/main/java/com/mcxiv/logger/util/LevelDependent.java rename to src/main/java/com/mcxiv/logger/util/LevelDependencyAdder.java index 9efeaa4..6c3f25f 100644 --- a/src/main/java/com/mcxiv/logger/util/LevelDependent.java +++ b/src/main/java/com/mcxiv/logger/util/LevelDependencyAdder.java @@ -1,35 +1,38 @@ package com.mcxiv.logger.util; -import com.mcxiv.logger.formatted.FLog; +import com.mcxiv.logger.packets.LambdaPacket; import com.mcxiv.logger.tools.LogLevel; -public abstract class LevelDependent { +public interface LevelDependencyAdder { - public abstract T provide(); - public abstract T provideEmpty(); - - public T vital() { + LambdaPacket provide(); + + default LambdaPacket provideEmpty() { + return LambdaPacket.EMPTY_VESSEL; + } + + default LambdaPacket vital() { return LogLevel.VITAL.accepted() ? provide() : provideEmpty(); } - public T error() { + default LambdaPacket error() { return LogLevel.ERROR.accepted() ? provide() : provideEmpty(); } - public T warn() { + default LambdaPacket warn() { return LogLevel.WARN.accepted() ? provide() : provideEmpty(); } - public T notice() { + default LambdaPacket notice() { return LogLevel.NOTICE.accepted() ? provide() : provideEmpty(); } - public T debug() { + default LambdaPacket debug() { return LogLevel.DEBUG.accepted() ? provide() : provideEmpty(); } - public T general() { + default LambdaPacket general() { return LogLevel.ALL.accepted() ? provide() : provideEmpty(); } - + } diff --git a/src/main/java/com/mcxiv/logger/util/StringsConsumer.java b/src/main/java/com/mcxiv/logger/util/StringsConsumer.java index abcfbc9..f228e35 100644 --- a/src/main/java/com/mcxiv/logger/util/StringsConsumer.java +++ b/src/main/java/com/mcxiv/logger/util/StringsConsumer.java @@ -6,3 +6,5 @@ public interface StringsConsumer { void consume(String...st); } + + diff --git a/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java b/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java index f6d1056..381f32f 100644 --- a/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java +++ b/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java @@ -6,13 +6,14 @@ public class Logger_LevelDependencyAdderTest { + @Format({"","",":n:"}) public void print(FLog log) { - log.vital().prt("Vital", "Hello", "World\n"); - log.error().prt("Error", "Hello", "World\n"); - log.warn().prt("Warn", "Hello", "World\n"); - log.notice().prt("Notice", "Hello", "World\n"); - log.debug().prt("Debug", "Hello", "World\n"); - log.general().prt("General", "Hello", "World\n"); + log.vital().prt(() -> FLog.form("Vital", "Hello", "World")); + log.error().prt(() -> FLog.form("Error", "Hello", "World")); + log.warn().prt(() -> FLog.form("Warn", "Hello", "World")); + log.notice().prt(() -> FLog.form("Notice", "Hello", "World")); + log.debug().prt(() -> FLog.form("Debug", "Hello", "World")); + log.general().prt(() -> FLog.form("General", "Hello", "World")); log.prt("\n"); } @@ -35,15 +36,15 @@ public void Test() { public void test_NICEFormatting() { FLog log = FLog.getNew(); - log.general().prt("1", "Useless Sentences", "Some boring text ahead...", + log.general().prt(() -> FLog.form("1", "Useless Sentences", "Some boring text ahead...", "So here we have some totally boring text just", "lying around here for you to read. Though feel", "totally comfortable if you wish to skip ahead." - ); + )); LogLevel.NOTICE.activate(); - log.notice().prt("2", "Senseless Art", "Valuable Shit", + log.notice().prt(() -> FLog.form("2", "Senseless Art", "Valuable Shit", "The main theory behind Senseless Art is the", "ability to use simple sentences to create a", "feeling of improtance and value describing", @@ -52,7 +53,7 @@ public void test_NICEFormatting() { "sense of how to use such to your advantage,", "consider, the person a master of this uniquely", "special art form." - ); + )); } } \ No newline at end of file diff --git a/src/test/java/com/mcxiv/logger/tables/TableTest.java b/src/test/java/com/mcxiv/logger/tables/TableTest.java index 9e091b1..6108530 100644 --- a/src/test/java/com/mcxiv/logger/tables/TableTest.java +++ b/src/test/java/com/mcxiv/logger/tables/TableTest.java @@ -73,21 +73,22 @@ public void LevelTest() { FLog log = FLog.getNew(); - Table.stripped().warn() + Table.stripped() .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .create(log); LogLevel.VITAL.activate(); - Table.stripped().warn() + Table.stripped() + .setLogLevel(LogLevel.DEBUG) .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .create(log); // LogLevel.ALL.activate(); - Table.stripped().warn() + Table.stripped() .head("S.No.", "Number 1", "Number 2", "Answer") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .create(log); diff --git a/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java b/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java index 307dbd1..ae65fd7 100644 --- a/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java +++ b/src/test/java/com/mcxiv/logger/ultimate/ULogTest.java @@ -6,7 +6,9 @@ import com.mcxiv.logger.formatted.fixed.FileLog; import com.mcxiv.logger.plotting.Plot; import com.mcxiv.logger.tables.Table; +import com.mcxiv.logger.tools.LogLevel; import org.junit.Test; +import sun.rmi.runtime.Log; public class ULogTest { @@ -79,6 +81,7 @@ public void tableTest() { }).start(); Table.box() +// .setLogLevel(LogLevel.DEBUG) .title("%%s") .head("S.No.", "Number 1", "Number 2", "Answer") .row("!", "A", "B", "A x B") From f50df764a3278e9a65ee6e7fa2e3f2ba9f09bdca Mon Sep 17 00:00:00 2001 From: MCXIV Date: Fri, 29 Jan 2021 11:04:27 +0530 Subject: [PATCH 3/3] Sorted up most of the stuff... I hope. --- OTHER_IMPORTANT_THINGS.md | 79 +++++++++++++++++++ PLOTTING.md | 22 +++--- README.md | 35 +++++--- TABLE_EXAMPLES.md | 16 ++-- WRITING_A_FORMAT_FOR_DECORATION.md | 3 + .../logger/decorations/ConsoleDecoration.java | 22 +++++- .../mcxiv/logger/decorations/Decoration.java | 24 ++---- .../mcxiv/logger/decorations/Decorations.java | 38 +++++---- .../logger/decorations/EmptyDecoration.java | 1 + ...FileDecoration.java => RawDecoration.java} | 8 +- .../logger/decorations/TagDecoration.java | 7 +- .../formatted/Logger_MethodImplierBody.java | 4 +- .../mcxiv/logger/formatted/fixed/FileLog.java | 2 +- .../formatted/fixed/Logger_LogFileWriter.java | 14 ++-- .../com/mcxiv/logger/tables/BoxTable.java | 23 ------ .../com/mcxiv/logger/tables/StripesTable.java | 2 +- .../com/mcxiv/logger/tables/TableAdaptor.java | 5 +- .../java/com/mcxiv/logger/tools/LogLevel.java | 16 +--- .../decorations/ConsoleDecorationTest.java | 14 ++-- .../Logger_AnnotationCompilerTest.java | 22 +++++- .../Logger_LevelDependencyAdderTest.java | 4 +- .../com/mcxiv/logger/tables/TableTest.java | 6 +- 22 files changed, 231 insertions(+), 136 deletions(-) create mode 100644 OTHER_IMPORTANT_THINGS.md rename src/main/java/com/mcxiv/logger/decorations/{RawFileDecoration.java => RawDecoration.java} (89%) diff --git a/OTHER_IMPORTANT_THINGS.md b/OTHER_IMPORTANT_THINGS.md new file mode 100644 index 0000000..a6fdae5 --- /dev/null +++ b/OTHER_IMPORTANT_THINGS.md @@ -0,0 +1,79 @@ +# Umm, complete documentation? + +### Initialisation + +``` +// Default initialisation, prints to System.out +FLog logger = FLog.getNew(); + +// Prepares and provides the formatted log text to the given object. +// The object can be an OutputStream, a ByteConsumer or a StringsConsumer. +FLog logger = FLog.getNew(object); + + +// The same initialisation methods are followed by ALog +FLog logger = ALog.getNew(); +FLog logger = ALog.getNew(object); + + +// To write logs to to a file "Hello.txt". +FLog logger = FileLog.getNew("Hello.txt"); +// or +FLog logger = FileLog.getNew(new File("logs/Hello.txt")); + +// To write logs to a file with a default name. +FLog logger = FileLog.getNew(); + + +// To use a custom Decoration +log.setDecorationType(Decorations.TAG); // enable tag decos. +log.setDecorationType(Decorations.RAW); // enable raw decos, ie, no strange characters. + + +// creating a logger to both, print to console and write to file. +FLog log = ULog.forNew() + .add(FLog.getNew()) + .add(FileLog.getNew("new.txt")) + .create(); + + +// When using ConsoleDecoration, to set the color mode. +ConsoleDecoration.setColorMode(mode); +// mode can be one of [NONE, BLACK, WHITE, BIT_3, BIT_4, BIT_8, TRUE_COLOR_BIT_24] +``` + +#### Usage + +``` +// Print formatted text +// the arguments can be strings or objects. +logger.prt(arg1, arg2, arg3 ... argn); + +// Print unformatted text +logger.raw(arg); + +// Print with a custom format +logger.prtf(format1, format2...).consume(arg1, arg2, arg3...); + +// To set a specific decoration type +logger.setDecorationType(name); +// name can be one of Decorations.[CONSOLE, RAW, TAG, EMPTY] + +// To print a cluster of data at once, you may prepare a packet +Packet pack = logger.newPacket(); + +pack.prt(...); +pack.raw(...); + +pack.consume(); + +// To use log levels +logger.general().prt(() -> new String[]{"General", "Hello", "World"}); +// or +LogLevel.DEBUG.act(() -> log.prt("General", "Hello", "World")); + +// To set a log level +LogLevel.setLevel(LogLevel.WARN); +// or +LogLevel.NOTICE.activate(); +``` \ No newline at end of file diff --git a/PLOTTING.md b/PLOTTING.md index 7edaa6c..4aba259 100644 --- a/PLOTTING.md +++ b/PLOTTING.md @@ -25,10 +25,9 @@ FLog log = FLog.getNew(); int[] age = new int[]{6,47,74,70,42,22,11,30,18,32,94,4,90,44,86,86,46,20,91,89,86,47,8,45,56}; -log.raw(Plot.BarGraph.simple() +Plot.BarGraph.simple() .values(age) - .create() -); + .create(log); ``` ![SuperSimpleTest](images/SuperSimpleTest.png)

@@ -37,14 +36,13 @@ FLog log = FLog.getNew(); int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94, 4, 90, 44, 86, 86, 46, 20, 91, 89, 86, 47, 8, 45, 56}; -log.raw(Plot.BarGraph.simple() +Plot.BarGraph.simple() .title("Visitor's Age Survey") .XLabel(0, age.length, i -> (age[i] > 18 ? "Adult " : "Minor ") + age[i]) .YLabel(0, 10,i -> i*15) .scale(0.08) .values(age) - .create() -); + .create(log); ``` ![SimpleTest1](images/SimpleTest1.png)

@@ -53,12 +51,11 @@ FLog log = FLog.getNew(); int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94, 4, 90, 44, 86, 86, 46, 20, 91, 89, 86, 47, 8, 45, 56}; -log.raw(Plot.BarGraph.simple() +Plot.BarGraph.simple() .values(age) .charHeight(15) .setBarType(Box.B_T) - .create() -); + .create(log); ``` ![SimpleTest3](images/SimpleTest3.png)

@@ -67,11 +64,10 @@ FLog log = FLog.getNew(); int[] age = new int[]{6, 47, 74, 70, 42, 22, 11, 30, 18, 32, 94}; -log.raw(Plot.BarGraph.simple() +Plot.BarGraph.simple() .values(age) .setBarType("Hello") - .create() -); + .create(log); ``` ![SimpleTest2](images/SimpleTest2.png)

@@ -96,5 +92,7 @@ log.raw(Plot.image(i.getWidth(), i.getHeight(), i.getWidth()/2, i.getHeight()/2, (x, y) -> (i.getRGB(x, y) >> 8) & 0xFF, // for green (x, y) -> i.getRGB(x, y) & 0xFF)); // for blue ``` +original
![SimpleTest2](images/ImageTestB.png)

+the output
![SimpleTest2](images/ImageTestA.png)

\ No newline at end of file diff --git a/README.md b/README.md index 10bc289..9e542de 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,25 @@ A simple _logging_ tool to make **console outputs** look prettier... -For instructions on writing a "Formatting Code", +Get an intro to "writing formatting codes" at, [![](https://img.shields.io/badge/Logger-Instructions-yellow)](WRITING_A_FORMAT_FOR_DECORATION.md) -For examples of `@Format` annotation, +Other important stuff regarding the usage of this tool, +[![](https://img.shields.io/badge/Logger-Stuff-cyan)](WRITING_A_FORMAT_FOR_DECORATION.md) + +Get simple and complicated examples for better insight, [![](https://img.shields.io/badge/Logger-Examples-orange)](LOGGER_EXAMPLES.md) -For instructions on using Tables and examples, +You know, you can also create Tables with this tool! [![](https://img.shields.io/badge/Tables-Examples-green)](TABLE_EXAMPLES.md) -For examples of plotint data, +And well, we have simple plotting tools too, [![](https://img.shields.io/badge/Plotting-Examples-red)](PLOTTING.md) For console specific compatibility, [![](https://img.shields.io/badge/Console-Compatibility-purple)](SUPPORT.md) +
[![](https://jitpack.io/v/Minecraftian14/MyLOGGER.svg)](https://jitpack.io/#Minecraftian14/MyLOGGER) [![](https://img.shields.io/discord/740954840259362826?color=7289da&label=Discord)](https://discord.gg/UgMH9c98mg) @@ -31,12 +35,13 @@ For console specific compatibility, ```groovy allprojects { repositories { - ... + // ... other sources maven { url 'https://jitpack.io' } } } dependencies { + // ... other dependencies // Include the library as a dependency. // Replace TAG_NAME with the number aside jitpack badge. @@ -51,18 +56,24 @@ dependencies { #### Initialise ``` -// default initialisation. +// Default initialisation, to print to Console. FLog logger = FLog.getNew(); -// if using the annotation processor version. +// The annotation processor version. FLog logger = ALog.getNew(); -// to use a custom Decoration -Decoration.setDecoration(TagDecoration::new); +// To write logs to to a file "Hello.txt". +FLog logger = FileLog.getNew("new.txt"); -// to change the output Decoration when using RawFileDecoration -RawFileDecoration.setPartnerDecorationDecoration(TagDecoration::new); -Decoration.setDecoration(RawFileDecoration::new); +// to use a custom Decoration +log.setDecorationType(Decorations.TAG); // enable tag decos. +log.setDecorationType(Decorations.RAW); // enable raw decos, ie, no strange characters. + +// creating a logger to both, print to console and write to file. +FLog log = ULog.forNew() + .add(FLog.getNew()) + .add(FileLog.getNew("new.txt")) + .create(); ``` #### Usage diff --git a/TABLE_EXAMPLES.md b/TABLE_EXAMPLES.md index a27a4f4..cad10af 100644 --- a/TABLE_EXAMPLES.md +++ b/TABLE_EXAMPLES.md @@ -20,13 +20,13 @@ for (int i = 0; i < len; i++) { Oce[i] = Ace[i] * Mice[i]; } -System.out.print(Table.stripped() +Table.stripped() .head("S.No.", "Number 1", "Number 2", "Answer") .row("", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); ``` ### Output @@ -42,14 +42,14 @@ System.out.print(Table.stripped() ``` // Same Initialisation -System.out.print(Table.box() +Table.box() .title("Hello") .head("S.No.", "Number 1", "Number 2", "Answer") .row("!", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); ``` ### Output ![Example Table Two](images/T2.png) @@ -63,14 +63,14 @@ System.out.print(Table.box() ``` // Same initialisation. -System.out.print(Table.empty() +Table.empty() .title("Hello") .head("S.No.", "Number 1", "Number 2", "Answer") .row("!", "A", "B", "A x B") .iter(0, len, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) .row("", "Only", "Even", "Values") .iter(1, len, 2, i -> i + 1, i -> Ace[i], i -> Mice[i], i -> Oce[i]) - .create()); + .create(log); ``` ### Output ![Example Table Three](images/T3.png) @@ -90,7 +90,7 @@ int gs = len / 10; // group size Double[] iterations = new Double[len]; for (int i = 0; i < len; i++) iterations[i] = Math.random(); -System.out.print(Table.stripped() +Table.stripped() .title("Average of Random Numbers") .head("S.No.", "Range", "Average") .bunch(iterations, len / 10, @@ -101,7 +101,7 @@ System.out.print(Table.stripped() .formatTitle(":@4085eeb:") .formatHead(":@2565ae#fff:", ":@0f5298#fff:") .format(":@66d3fa:", ":@55d3fe:") - .create()); + .create(log); ``` ``` private Double avg(Double[] g) { diff --git a/WRITING_A_FORMAT_FOR_DECORATION.md b/WRITING_A_FORMAT_FOR_DECORATION.md index 314b6dc..24f0c49 100644 --- a/WRITING_A_FORMAT_FOR_DECORATION.md +++ b/WRITING_A_FORMAT_FOR_DECORATION.md @@ -76,9 +76,12 @@ input text. |Tab Space |t |Yes |Yes |? | To prepend a tab space | |Word Wrap |wnw |Yes |Yes |? | Word Wrap input to fit a width of 'n' and apply the same format to each line. | |Splitter |xcx |Yes |Yes |? | Split the given input about each occurrance of 'c'. | +|Repeater |R |Yes |Yes |Yes | Reuse the decorators from start when input exceeds them. | +|Meta Data |P C or M |Yes |Yes |Yes | P -> package name, C -> class name, M -> method name. | * Console -> a Formatting Style to decorate the console outputs. +* Raw -> Just like console but no color/bold/underline... formatting. * Tags -> a Formatting Style consisting of appending bracketed tags to the input to inform a parser about the text format. An example of this formatting is used in HyperLap2D to apply diff --git a/src/main/java/com/mcxiv/logger/decorations/ConsoleDecoration.java b/src/main/java/com/mcxiv/logger/decorations/ConsoleDecoration.java index 1ec863f..0d0c123 100644 --- a/src/main/java/com/mcxiv/logger/decorations/ConsoleDecoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/ConsoleDecoration.java @@ -24,7 +24,8 @@ public static void setColorMode(int mode) { STATE = mode; } - public ConsoleDecoration(String... codes) { + public ConsoleDecoration(Decorations.Tag tag, String... codes) { + super(tag); decorates = new Decorate[codes.length]; @@ -39,13 +40,26 @@ public ConsoleDecoration(String... codes) { FormattingCodeSplitter sp = new FormattingCodeSplitter(code); // And some basic initialisation. - StringBuilder format = new StringBuilder(sp.prepre); + StringBuilder format = new StringBuilder(); Matcher m; // + if (tag != null) { + String brush = ""; + if (sp.content.contains("P")) brush += "[" + tag.packageName + "]"; + if (sp.content.contains("C")) brush += "[" + tag.className + "]"; + if (sp.content.contains("M")) brush += "[" + tag.executableName + "]"; + sp.prepre = brush + " " + sp.prepre; + } + format.append(sp.prepre); + + + // + + // Parsing Color type to code String colorcd = ""; @@ -128,7 +142,9 @@ public ConsoleDecoration(String... codes) { if (sp.content.contains("-")) if (!sp.content.contains("%") || sp.content.indexOf("-") < sp.content.indexOf("%")) format.append(C.FS); - if (sp.content.contains("~")) { + + if (sp.content.contains("R")) the_whole_repeats = true; + else if (sp.content.contains("~")) { last_one_repeats = true; repeater_index = i; } diff --git a/src/main/java/com/mcxiv/logger/decorations/Decoration.java b/src/main/java/com/mcxiv/logger/decorations/Decoration.java index 160bfbb..232082a 100644 --- a/src/main/java/com/mcxiv/logger/decorations/Decoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/Decoration.java @@ -1,32 +1,17 @@ package com.mcxiv.logger.decorations; -import com.mcxiv.logger.tools.RandomColor; - -import java.util.function.Function; import java.util.regex.Pattern; import static com.mcxiv.logger.tools.C.map; public abstract class Decoration { - private static Function decorator = ConsoleDecoration::new; - private static String DECORATION_CLASS = ConsoleDecoration.class.getName(); - - public static Decoration getDecoration(String... codes) { - return decorator.apply(codes); - } - - public static void setDecoration(Function decoration) { - decorator = decoration; - DECORATION_CLASS = decoration.apply(new String[0]).getClass().getName(); - } - - Decorate[] decorates; boolean last_one_repeats = false; + boolean the_whole_repeats = false; int repeater_index = -1; - public Decoration(String... codes) { + public Decoration(Decorations.Tag tag, String... codes) { } static Pattern re_prepre = Pattern.compile("^([^:]*)[:][:]"); @@ -63,7 +48,10 @@ public String decorate(String... input) { for (int i = 0; i < Math.min(input.length, decorates.length); i++) input[i] = decorates[i].decorate(input[i]); - if (last_one_repeats && decorates.length < input.length) + if (the_whole_repeats && decorates.length < input.length) + for (int i = decorates.length; i < input.length; i++) + input[i] = decorates[i % decorates.length].decorate(input[i]); + else if (last_one_repeats && decorates.length < input.length) for (int i = decorates.length; i < input.length; i++) input[i] = decorates[repeater_index].decorate(input[i]); diff --git a/src/main/java/com/mcxiv/logger/decorations/Decorations.java b/src/main/java/com/mcxiv/logger/decorations/Decorations.java index 01195d2..95409ae 100644 --- a/src/main/java/com/mcxiv/logger/decorations/Decorations.java +++ b/src/main/java/com/mcxiv/logger/decorations/Decorations.java @@ -2,6 +2,7 @@ import com.mcxiv.logger.tools.RandomColor; +import javax.annotation.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.HashMap; @@ -10,7 +11,7 @@ public class Decorations { public static final String CONSOLE = "console"; - public static final String RAW_FILE = "raw file"; + public static final String RAW = "raw file"; public static final String TAG = "tag"; public static final String EMPTY = "empty"; @@ -57,33 +58,33 @@ public static Decoration get(String decorator) { format = clazz.getAnnotation(Format.class); if (format != null) { - Decoration t = getSpecific(decorator, format.value()); + Decoration t = getSpecific(tag, decorator, format.value()); decorations_map.put(tag, t); return t; } } catch (Exception ignored) { } - Decoration t = getRandom(decorator); + Decoration t = getRandom(tag, decorator); decorations_map.put(tag, t); return t; } - public static Decoration getSpecific(String decorator, String... formats) { + public static Decoration getSpecific(@Nullable Tag tag, String decorator, String... formats) { switch (decorator) { case CONSOLE: - return new ConsoleDecoration(formats); + return new ConsoleDecoration(tag, formats); case TAG: - return new TagDecoration(formats); - case RAW_FILE: - return new RawFileDecoration(formats); + return new TagDecoration(tag, formats); + case RAW: + return new RawDecoration(tag, formats); case EMPTY: default: return new EmptyDecoration(); } } - public static Decoration getRandom(String decorator) { + public static Decoration getRandom(@Nullable Tag tag, String decorator) { RandomColor c = new RandomColor(); String[] codes = new String[]{ @@ -93,18 +94,24 @@ public static Decoration getRandom(String decorator) { ":#" + c.getBright().yieldHex() + ": ::" }; - return getSpecific(decorator, codes); + return getSpecific(tag, decorator, codes); } - private static class Tag { + public static class Tag { + String packageName; String className; String executableName; String decorator; - public Tag(String className, String executableName, String decorator) { - this.className = className; + public Tag(String classAddress, String executableName, String decorator) { + for (int i = 0; i < classAddress.length(); i++) { + if(Character.isUpperCase(classAddress.charAt(i))){ + packageName = classAddress.substring(0, i-1); + className = classAddress.substring(i); + } + } this.executableName = executableName; this.decorator = decorator; } @@ -114,14 +121,15 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Tag tag = (Tag) o; - return Objects.equals(className, tag.className) && + return Objects.equals(packageName, tag.packageName) && + Objects.equals(className, tag.className) && Objects.equals(executableName, tag.executableName) && Objects.equals(decorator, tag.decorator); } @Override public int hashCode() { - return Objects.hash(className, executableName, decorator); + return Objects.hash(packageName, className, executableName, decorator); } } diff --git a/src/main/java/com/mcxiv/logger/decorations/EmptyDecoration.java b/src/main/java/com/mcxiv/logger/decorations/EmptyDecoration.java index 22fa612..c88ee83 100644 --- a/src/main/java/com/mcxiv/logger/decorations/EmptyDecoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/EmptyDecoration.java @@ -3,6 +3,7 @@ public class EmptyDecoration extends Decoration { public EmptyDecoration() { + super(null); decorates = new Decorate[] {}; } diff --git a/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java b/src/main/java/com/mcxiv/logger/decorations/RawDecoration.java similarity index 89% rename from src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java rename to src/main/java/com/mcxiv/logger/decorations/RawDecoration.java index d90473e..219ea2f 100644 --- a/src/main/java/com/mcxiv/logger/decorations/RawFileDecoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/RawDecoration.java @@ -2,10 +2,11 @@ import java.util.regex.Matcher; -public class RawFileDecoration extends Decoration { +public class RawDecoration extends Decoration { - public RawFileDecoration(String... codes) { + public RawDecoration(Decorations.Tag tag, String... codes) { + super(tag); decorates = new Decorate[codes.length]; @@ -37,7 +38,8 @@ public RawFileDecoration(String... codes) { // Parsing other formatting chars - if (sp.content.contains("~")) { + if (sp.content.contains("R")) the_whole_repeats = true; + else if (sp.content.contains("~")) { last_one_repeats = true; repeater_index = i; } diff --git a/src/main/java/com/mcxiv/logger/decorations/TagDecoration.java b/src/main/java/com/mcxiv/logger/decorations/TagDecoration.java index 9c7db8f..bd43454 100644 --- a/src/main/java/com/mcxiv/logger/decorations/TagDecoration.java +++ b/src/main/java/com/mcxiv/logger/decorations/TagDecoration.java @@ -50,7 +50,8 @@ public class TagDecoration extends Decoration { } - public TagDecoration(String... codes) { + public TagDecoration(Decorations.Tag tag, String... codes) { + super(tag); decorates = new Decorate[codes.length]; @@ -142,7 +143,9 @@ public TagDecoration(String... codes) { if (sp.content.contains("-")) if (!sp.content.contains("%") || sp.content.indexOf("-") < sp.content.indexOf("%")) format.append("[STRIKE]"); - if (sp.content.contains("~")) { + + if (sp.content.contains("R")) the_whole_repeats = true; + else if (sp.content.contains("~")) { last_one_repeats = true; repeater_index = i; } diff --git a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java index 9fa9d0a..31c4b94 100644 --- a/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java +++ b/src/main/java/com/mcxiv/logger/formatted/Logger_MethodImplierBody.java @@ -47,7 +47,7 @@ public void raw(String raw) { @Override public StringsConsumer prtf(String... format) { - Decoration decoration = Decorations.getSpecific(Decorations.CONSOLE, format); + Decoration decoration = Decorations.getSpecific(null,Decorations.CONSOLE, format); return msg -> writer.consume(decoration.decorate(msg)); } @@ -79,7 +79,7 @@ public void raw(String raw) { @Override public StringsConsumer prtf(String... format) { - Decoration decoration = Decorations.getSpecific(Decorations.CONSOLE, format); + Decoration decoration = Decorations.getSpecific(null,Decorations.CONSOLE, format); return msg -> builder.append(decoration.decorate(msg)); } diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java b/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java index 1546403..dcdcc59 100644 --- a/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/FileLog.java @@ -8,7 +8,7 @@ public abstract class FileLog extends FLog { public FileLog() { - decorator_name = Decorations.RAW_FILE; + decorator_name = Decorations.RAW; } public static FLog getNew(String file) { diff --git a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java index 020aae5..0dc9d29 100644 --- a/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java +++ b/src/main/java/com/mcxiv/logger/formatted/fixed/Logger_LogFileWriter.java @@ -2,7 +2,7 @@ import com.mcxiv.logger.decorations.Decoration; import com.mcxiv.logger.decorations.Decorations; -import com.mcxiv.logger.decorations.RawFileDecoration; +import com.mcxiv.logger.decorations.RawDecoration; import com.mcxiv.logger.formatted.FLog; import com.mcxiv.logger.packets.Packet; import com.mcxiv.logger.tables.Table; @@ -62,14 +62,14 @@ private void flush() { @Override public void prt(String... msg) { - write(Decorations.get("raw file").decorate(msg)); + write(Decorations.get(Decorations.RAW).decorate(msg)); } @Override public void prt(Object... obj) { String[] stf = new String[obj.length]; for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); - write(Decorations.get("raw file").decorate(stf)); + write(Decorations.get(Decorations.RAW).decorate(stf)); } @Override @@ -79,7 +79,7 @@ public void raw(String raw) { @Override public StringsConsumer prtf(String... format) { - return msg -> write(new RawFileDecoration(format).decorate(msg)); + return msg -> write(new RawDecoration(null, format).decorate(msg)); } @Override @@ -91,13 +91,13 @@ private class OurPacket extends Packet { @Override public void prt(String... msg) { - Decoration decoration = Decorations.get(Decorations.RAW_FILE); + Decoration decoration = Decorations.get(Decorations.RAW); builder.append(decoration.decorate(msg)); } @Override public void prt(Object... obj) { - Decoration decoration = Decorations.get(Decorations.RAW_FILE); + Decoration decoration = Decorations.get(Decorations.RAW); String[] stf = new String[obj.length]; for (int i = 0; i < stf.length; i++) stf[i] = obj[i].toString(); builder.append(decoration.decorate(stf)); @@ -110,7 +110,7 @@ public void raw(String raw) { @Override public StringsConsumer prtf(String... format) { - return msg -> builder.append(new RawFileDecoration(format).decorate(msg)); + return msg -> builder.append(new RawDecoration(null,format).decorate(msg)); } @Override diff --git a/src/main/java/com/mcxiv/logger/tables/BoxTable.java b/src/main/java/com/mcxiv/logger/tables/BoxTable.java index 261b034..22964e1 100644 --- a/src/main/java/com/mcxiv/logger/tables/BoxTable.java +++ b/src/main/java/com/mcxiv/logger/tables/BoxTable.java @@ -15,46 +15,23 @@ class BoxTable extends TableAdaptor { String title = null; String[] header; - Decoration titleFormat = null; - Decoration headFormat = null; - Decoration rowFormat = null; - - static Decoration TITLE_FORMAT = Decoration.getDecoration(Box.DP + ":: :b: ::" + Box.DP); - static Decoration HEAD_FORMAT = Decoration.getDecoration(Box.DP + ":: :b: ::" + Box.DP, ":: :b~: ::" + Box.DP); - static Decoration ROW_FORMAT = Decoration.getDecoration(Box.DP + ":: : : ::" + Box.DP, ":: :~: ::" + Box.DP); - public BoxTable() { rowWidth = new ArrayList<>(); rows = new ArrayList<>(); } - /** - *

- * Important - The number of inputs should be at least 2 elements big. - * The first to corresponds to Column 1 and the second Column 2, the rest all are ignored. - *

- */ @Override public Table format(String... codes) { - rowFormat = Decoration.getDecoration(codes[0], codes[1]); return this; } @Override public Table formatTitle(String code) { - titleFormat = Decoration.getDecoration(code); return this; } - /** - *

- * Important - The number of inputs should be at least 1 elements big. - * The first to corresponds to Head Format, the rest all are ignored. - *

- */ @Override public Table formatHead(String... codes) { - headFormat = Decoration.getDecoration(codes[0]); return this; } diff --git a/src/main/java/com/mcxiv/logger/tables/StripesTable.java b/src/main/java/com/mcxiv/logger/tables/StripesTable.java index 76f7419..3929371 100644 --- a/src/main/java/com/mcxiv/logger/tables/StripesTable.java +++ b/src/main/java/com/mcxiv/logger/tables/StripesTable.java @@ -103,7 +103,7 @@ public void create(FLog mainLog) { if (rowFormat2 == null) rowFormat2 = ROW_FORMAT2; if (title != null) - packet.prtf(":n%" + rowWidth.stream().reduce(0, Integer::sum) + "s" + titleFormat + ":").consume(title); + packet.prtf(":: :n%-" + (getWidth()-2) + "s" + titleFormat + ": ::").consume(title); for (int i = 0; i < header.length; i++) packet.prtf(":: :%-" + rowWidth.get(i) + "s" + (i % 2 == 0 ? headFormat1 : headFormat2) + ": ::").consume(header[i]); diff --git a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java index f5e5a3f..a3ed758 100644 --- a/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java +++ b/src/main/java/com/mcxiv/logger/tables/TableAdaptor.java @@ -7,6 +7,7 @@ import com.mcxiv.logger.util.Iterator; import com.mcxiv.logger.util.LevelDependencyAdder; import com.mcxiv.logger.util.StringsConsumer; +import javafx.scene.control.Tab; import java.util.Arrays; @@ -44,7 +45,7 @@ public Table iter(int a, int b, int c, Iterator... its) { return this; } - @SafeVarargs +// @SafeVarargs @Override public final Table bunch(T[] main, int groupSize, GroupIterator... its) { @@ -56,9 +57,7 @@ public final Table bunch(T[] main, int groupSize, GroupIterator... its) { msg[j] = its[j].consume(i / groupSize, Arrays.copyOfRange(main, i, i + groupSize)).toString(); row(msg); - } - return this; } diff --git a/src/main/java/com/mcxiv/logger/tools/LogLevel.java b/src/main/java/com/mcxiv/logger/tools/LogLevel.java index 95582ea..1354e56 100644 --- a/src/main/java/com/mcxiv/logger/tools/LogLevel.java +++ b/src/main/java/com/mcxiv/logger/tools/LogLevel.java @@ -26,18 +26,8 @@ public void activate() { level = this; } - public static interface LogLevelAdaptor { - T vital(); - - T error(); - - T warn(); - - T notice(); - - T debug(); - - T general(); - } + public void act(Runnable runnable) { + if(accepted()) runnable.run(); + } } diff --git a/src/test/java/com/mcxiv/logger/decorations/ConsoleDecorationTest.java b/src/test/java/com/mcxiv/logger/decorations/ConsoleDecorationTest.java index 389cf76..67652ef 100644 --- a/src/test/java/com/mcxiv/logger/decorations/ConsoleDecorationTest.java +++ b/src/test/java/com/mcxiv/logger/decorations/ConsoleDecorationTest.java @@ -7,25 +7,25 @@ public class ConsoleDecorationTest { @Test public void test1() { - ConsoleDecoration d = new ConsoleDecoration(":$B:"); + ConsoleDecoration d = new ConsoleDecoration(null, ":$B:"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration(":$Gb:"); + d = new ConsoleDecoration(null, ":$Gb:"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration(":$RBKb:"); + d = new ConsoleDecoration(null, ":$RBKb:"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration("Hello:$Gb:"); + d = new ConsoleDecoration(null, "Hello:$Gb:"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration("AA::Hell:$Yb:"); + d = new ConsoleDecoration(null, "AA::Hell:$Yb:"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration("AA::Hell:$Yb:A::d"); + d = new ConsoleDecoration(null, "AA::Hell:$Yb:A::d"); System.out.println(d.decorate("Hey")); - d = new ConsoleDecoration("Yo:: :$Ybu: After Word ::Bye"); + d = new ConsoleDecoration(null, "Yo:: :$Ybu: After Word ::Bye"); System.out.println(d.decorate("Hey")); } diff --git a/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java b/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java index 97a5dd6..60ccddb 100644 --- a/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java +++ b/src/test/java/com/mcxiv/logger/formatted/Logger_AnnotationCompilerTest.java @@ -1,6 +1,7 @@ package com.mcxiv.logger.formatted; +import com.mcxiv.logger.decorations.Decorations; import com.mcxiv.logger.decorations.Format; import org.junit.Test; @@ -67,6 +68,25 @@ public void test_ABitComplicatedFormatting() { log.prt("Text Space One", "some more text", "And well, even more text..."); } + @Test + @Format({":$B: ::", ":$GR: ::"}) + public void test_Repeator() { + FLog log = FLog.getNew(); + log.prt("Text Space One", "some more text", "And well", " even more text..."); + } + + @Test + @Format({"Player:: :PCM $B %7s:ed ::by ", ":$Gn:"}) + public void movement() { + FLog log = FLog.getNew(); + + log.prt("sprint", 45); + + log.prt("shift", 78); + + log.prt("drift", 48); + } + @Test @Format({"\n:: :@ff4$Bb u: ::", ":: :@ff9#FF1493b%-18s: ::", ":: :@ffd#82En%-27s: ::", ":: :w47w@e#4B0082%-47s: ::"}) public void test_NICEFormatting() { @@ -74,7 +94,7 @@ public void test_NICEFormatting() { FLog log = FLog.getNew(); // RawFileDecoration.setPartnerDecorationDecoration(TagDecoration::new); -// Decoration.setDecoration(RawFileDecoration::new); + log.setDecorationType(Decorations.RAW); log.prt("1", "Useless Sentences", "Some boring text ahead...", "So here we have some totally boring text just " + diff --git a/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java b/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java index 381f32f..9e93879 100644 --- a/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java +++ b/src/test/java/com/mcxiv/logger/formatted/Logger_LevelDependencyAdderTest.java @@ -6,12 +6,12 @@ public class Logger_LevelDependencyAdderTest { - @Format({"","",":n:"}) + @Format({"", "", ":n:"}) public void print(FLog log) { log.vital().prt(() -> FLog.form("Vital", "Hello", "World")); log.error().prt(() -> FLog.form("Error", "Hello", "World")); log.warn().prt(() -> FLog.form("Warn", "Hello", "World")); - log.notice().prt(() -> FLog.form("Notice", "Hello", "World")); + log.notice().prt(() -> new String[]{"Notice", "Hello", "World"}); log.debug().prt(() -> FLog.form("Debug", "Hello", "World")); log.general().prt(() -> FLog.form("General", "Hello", "World")); log.prt("\n"); diff --git a/src/test/java/com/mcxiv/logger/tables/TableTest.java b/src/test/java/com/mcxiv/logger/tables/TableTest.java index 6108530..307bd5b 100644 --- a/src/test/java/com/mcxiv/logger/tables/TableTest.java +++ b/src/test/java/com/mcxiv/logger/tables/TableTest.java @@ -111,9 +111,9 @@ public void BunchTest() { (gi, g) -> (gi * len / 10) + "-" + ((gi + 1) * len / 10), (gi, g) -> String.format("%.3f", avg(g) ) ) - .formatTitle(":@4085eeb:") - .formatHead(":@2565ae#fff:", ":@0f5298#fff:") - .format(":@66d3fa:", ":@55d3fe:") + .formatTitle("@4085eeb") + .formatHead("@2565ae#fff", "@0f5298#fff") + .format("@66d3fa", "@55d3fe") .create(log); Table.box()