Skip to content

Commit

Permalink
Added Log class
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Apr 3, 2024
1 parent 492a716 commit 8de9993
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 76 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ repositories {
}

dependencies {
implementation('net.dv8tion:JDA:5.0.0-beta.12')
implementation('io.javalin:javalin:5.6.1')
implementation('org.mongodb:mongodb-driver-sync:4.10.2')
implementation('net.dv8tion:JDA:5.0.0-beta.21')
implementation('io.javalin:javalin:6.1.3')
implementation('org.mongodb:mongodb-driver-sync:5.0.0')
// implementation('org.slf4j:slf4j-simple:2.0.5')
// implementation('com.google.guava:guava:31.1-jre') { exclude group: "com.google.code.findbugs" }
compileOnly('org.jetbrains:annotations:24.0.1')
Expand Down
46 changes: 1 addition & 45 deletions src/main/java/dev/latvian/apps/webutils/ansi/Ansi.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package dev.latvian.apps.webutils.ansi;

import dev.latvian.apps.webutils.TimeUtils;
import dev.latvian.apps.webutils.json.JSON;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

Expand All @@ -13,7 +11,7 @@
*/
public interface Ansi {
Pattern PATTERN = Pattern.compile("\u001B\\[(?:\\d;)?\\d+[mD]");
char CODE = '\u001B';
char CHAR = '\u001B';

String[] COLOR_NAMES = {
"black",
Expand Down Expand Up @@ -214,46 +212,4 @@ static AnsiComponent cyan(Object text) {
static AnsiComponent white(Object text) {
return of(text).white();
}

static Date log(Object message, boolean newline) {
var c = of();
var now = new Date();
c.append(cyan(TimeUtils.formatDate(new StringBuilder(), now).toString()));

var c1 = of(message);

exit:
if (c1.text.length() >= 3 && c1.text.charAt(1) == ' ') {
switch (c1.text.charAt(0)) {
case '|' -> c1.blue();
case '+' -> c1.green();
case '*' -> c1.orange();
case '~' -> c1.yellow();
case '-' -> c1.red();
case '!' -> c1.error();
case '?' -> c1.lightGray();
case '`' -> c1.teal();
default -> {
break exit;
}
}

c1.text = c1.text.substring(2);
}

c.append(' ');
c.append(c1);

if (newline) {
System.out.println(c);
} else {
System.out.print(c);
}

return now;
}

static Date log(Object message) {
return log(message, true);
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/apps/webutils/ansi/AnsiCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public enum AnsiCode implements AnsiAppendable {
@Override
public void appendAnsi(StringBuilder builder, AnsiContext ctx) {
if (!ctx.unformatted()) {
builder.append(ctx.debug() ? '§' : Ansi.CODE);
builder.append(ctx.debug() ? '§' : Ansi.CHAR);
builder.append(code);
}
}

@Override
public String toString() {
return Ansi.CODE + code;
return Ansi.CHAR + code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void appendAnsi(StringBuilder builder, AnsiContext ctx) {
boolean changed = color != pColor || bgColor != pBgColor || bold != pBold || italic != pItalic || underline != pUnderline || blink != pBlink || reverse != pReverse || hidden != pHidden || strikethrough != pStrikethrough;

if (changed) {
builder.append(Ansi.CODE);
builder.append(Ansi.CHAR);

if (ctx.debug()) {
builder.append("[9").append((ctx.index++ & 3) + 1).append("m§");
Expand All @@ -147,7 +147,7 @@ public void appendAnsi(StringBuilder builder, AnsiContext ctx) {

if (ctx.debug()) {
builder.append(']');
builder.append(Ansi.CODE);
builder.append(Ansi.CHAR);
builder.append(AnsiCode.RESET.code);
} else {
builder.append('m');
Expand All @@ -165,7 +165,7 @@ public void appendAnsi(StringBuilder builder, AnsiContext ctx) {
}

if (changed) {
builder.append(Ansi.CODE);
builder.append(Ansi.CHAR);

if (ctx.debug()) {
builder.append("[9").append((ctx.index++ & 3) + 1).append("m§");
Expand All @@ -185,7 +185,7 @@ public void appendAnsi(StringBuilder builder, AnsiContext ctx) {

if (ctx.debug()) {
builder.append(']');
builder.append(Ansi.CODE);
builder.append(Ansi.CHAR);
builder.append(AnsiCode.RESET.code);
} else {
builder.append('m');
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/ansi/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.latvian.apps.webutils.ansi;

import dev.latvian.apps.webutils.TimeUtils;

import java.util.Date;

public class Log {
public enum Type {
INFO,
WARN,
ERROR,
DEBUG,
SUCCESS,
FAIL,
IMPORTANT,
CODE
}

public static Log INSTANCE = new Log();

public static void info(Object message) {
INSTANCE.log(Type.INFO, Ansi.of(message));
}

public static void warn(Object message) {
INSTANCE.log(Type.WARN, Ansi.of(message).yellow());
}

public static void error(Object message) {
INSTANCE.log(Type.ERROR, Ansi.of(message).error());
}

public static void debug(Object message) {
INSTANCE.log(Type.DEBUG, Ansi.of(message).lightGray());
}

public static void success(Object message) {
INSTANCE.log(Type.SUCCESS, Ansi.of(message).green());
}

public static void fail(Object message) {
INSTANCE.log(Type.FAIL, Ansi.of(message).red());
}

public static void success(Object message, boolean success) {
if (success) {
success(message);
} else {
fail(message);
}
}

public static void important(Object message) {
INSTANCE.log(Type.IMPORTANT, Ansi.of(message).orange());
}

public static void code(Object message) {
INSTANCE.log(Type.CODE, Ansi.of(message).teal());
}

public void log(Type type, AnsiComponent message) {
var now = new Date();
var c = Ansi.of();
c.append(Ansi.cyan(TimeUtils.formatDate(new StringBuilder(), now).toString()));
c.append(' ');
c.append(message);
System.out.println(c);
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/apps/webutils/ansi/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public List<String> getLines(boolean colors) {
}

public void print() {
Ansi.log("\n" + String.join("\n", getLines(true)));
Log.info("\n" + String.join("\n", getLines(true)));
}

public Tag toTag() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/apps/webutils/data/Lazy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.apps.webutils.data;

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.Log;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand Down Expand Up @@ -56,7 +56,7 @@ public T get() {
try {
object = getter.get();
} catch (Exception ex) {
Ansi.log("! " + ex);
Log.error(ex);
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/dev/latvian/apps/webutils/data/Mutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import dev.latvian.apps.webutils.Cast;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Mutable<T> {
public class Mutable<T> implements Supplier<T>, Consumer<T> {
private static final Function<Object, Mutable<?>> MAP_VALUE = o -> new Mutable<>();

public static <T> Function<Object, Mutable<T>> mapValue() {
Expand All @@ -21,6 +23,16 @@ public Mutable(T v) {
value = v;
}

@Override
public T get() {
return value;
}

@Override
public void accept(T t) {
value = t;
}

@Override
public String toString() {
return String.valueOf(value);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/latvian/apps/webutils/html/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.AnsiComponent;
import dev.latvian.apps.webutils.ansi.Log;
import dev.latvian.apps.webutils.net.FileResponse;
import dev.latvian.apps.webutils.net.MimeType;
import dev.latvian.apps.webutils.net.Response;
Expand Down Expand Up @@ -43,7 +44,7 @@ default String toRawString() {
try {
appendRaw(builder);
} catch (OutOfMemoryError error) {
Ansi.log("! Out of memory while generating HTML:");
Log.error("Out of memory while generating HTML:");
error.printStackTrace();
} catch (Throwable error) {
error.printStackTrace();
Expand All @@ -58,7 +59,7 @@ default String toTagString(boolean header) {
try {
append(builder, header);
} catch (OutOfMemoryError error) {
Ansi.log("! Out of memory while generating HTML:");
Log.error("Out of memory while generating HTML:");
error.printStackTrace();
} catch (Throwable error) {
error.printStackTrace();
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.apps.webutils.test;

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.Log;
import dev.latvian.apps.webutils.html.PairedTag;
import org.junit.jupiter.api.Test;

Expand All @@ -18,7 +19,7 @@ public void components() {
System.out.println(c1.toDebugString());
System.out.println();

Ansi.log("! Hello");
Log.error("Hello");
}

@Test
Expand All @@ -27,6 +28,6 @@ public void html() {
section.classes("test test-2");
section.img("img.png");
section.p().string("Hello!").p().raw("abc").spanstr("yo").raw("test");
Ansi.log("Tag:\n" + section.toAnsi(true));
Log.info("Tag:\n" + section.toAnsi(true));
}
}
9 changes: 5 additions & 4 deletions src/test/java/dev/latvian/apps/webutils/test/JSONTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.apps.webutils.test;

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.Log;
import dev.latvian.apps.webutils.json.JSON;
import dev.latvian.apps.webutils.json.JSONObject;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -39,11 +40,11 @@ public void adapt() {
public void readRemoteObject() throws Exception {
// {"hours":672,"total_launches":7864423,"hourly":11700.028795489174,"ml":[{"ml":1,"fraction":0.9412095458242773,"launches":7402070},{"ml":2,"fraction":0.05879045417572275,"launches":462353}],"mc":[{"mc":2001,"fraction":0.6443314404629558,"launches":5067295},{"mc":1902,"fraction":0.3556624561013567,"launches":2797080},{"mc":2004,"fraction":6.103435687525964E-6,"launches":48}],"mlmc":[{"ml":1,"mc":2001,"fraction":0.6154869848684386,"launches":4840450},{"ml":1,"mc":1902,"fraction":0.3257164575201512,"launches":2561572},{"ml":2,"mc":1902,"fraction":0.029945998581205512,"launches":235508},{"ml":2,"mc":2001,"fraction":0.028844455594517232,"launches":226845},{"ml":1,"mc":2004,"fraction":6.103435687525964E-6,"launches":48}],"launches":[{"version":"2001.6.4-build.114","fraction":0.26559062247796184,"launches":2088717},{"version":"2001.6.4-build.120","fraction":0.21153312836809515,"launches":1663586},{"version":"1902.6.2-build.45","fraction":0.1828411315108559,"launches":1437940},{"version":"2001.6.4-build.127","fraction":0.0810527358459737,"launches":637433},{"version":"1902.6.2-build.3","fraction":0.038601560470488426,"launches":303579},{"version":"2001.6.4-build.95","fraction":0.03645264248883866,"launches":286679},{"version":"1902.6.2-build.15","fraction":0.02442060911525232,"launches":192054},{"version":"1902.6.2-build.50","fraction":0.018523800156731142,"launches":145679},{"version":"2001.6.3-build.83","fraction":0.018382658206457105,"launches":144569},{"version":"1902.6.2-build.27","fraction":0.01564005394928528,"launches":123000},{"version":"","fraction":1.3987040117246999E-6,"launches":11}]}
var content = "{\"hours\":672,\"total_launches\":7864423,\"hourly\":11700.028795489174,\"ml\":[{\"ml\":1,\"fraction\":0.9412095458242773,\"launches\":7402070},{\"ml\":2,\"fraction\":0.05879045417572275,\"launches\":462353}],\"mc\":[{\"mc\":2001,\"fraction\":0.6443314404629558,\"launches\":5067295},{\"mc\":1902,\"fraction\":0.3556624561013567,\"launches\":2797080},{\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"mlmc\":[{\"ml\":1,\"mc\":2001,\"fraction\":0.6154869848684386,\"launches\":4840450},{\"ml\":1,\"mc\":1902,\"fraction\":0.3257164575201512,\"launches\":2561572},{\"ml\":2,\"mc\":1902,\"fraction\":0.029945998581205512,\"launches\":235508},{\"ml\":2,\"mc\":2001,\"fraction\":0.028844455594517232,\"launches\":226845},{\"ml\":1,\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"launches\":[{\"version\":\"2001.6.4-build.114\",\"fraction\":0.26559062247796184,\"launches\":2088717},{\"version\":\"2001.6.4-build.120\",\"fraction\":0.21153312836809515,\"launches\":1663586},{\"version\":\"1902.6.2-build.45\",\"fraction\":0.1828411315108559,\"launches\":1437940},{\"version\":\"2001.6.4-build.127\",\"fraction\":0.0810527358459737,\"launches\":637433},{\"version\":\"1902.6.2-build.3\",\"fraction\":0.038601560470488426,\"launches\":303579},{\"version\":\"2001.6.4-build.95\",\"fraction\":0.03645264248883866,\"launches\":286679},{\"version\":\"1902.6.2-build.15\",\"fraction\":0.02442060911525232,\"launches\":192054},{\"version\":\"1902.6.2-build.50\",\"fraction\":0.018523800156731142,\"launches\":145679},{\"version\":\"2001.6.3-build.83\",\"fraction\":0.018382658206457105,\"launches\":144569},{\"version\":\"1902.6.2-build.27\",\"fraction\":0.01564005394928528,\"launches\":123000},{\"version\":\"\",\"fraction\":1.3987040117246999E-6,\"launches\":11}]}";
Ansi.log(content);
Log.info(content);
var json = JSON.DEFAULT.read(content).readObject();
Ansi.log(Ansi.ofObject(json));
Log.info(Ansi.ofObject(json));

Ansi.log(json.asInt("total_launches"));
Ansi.log(json.asDouble("hourly"));
Log.info(json.asInt("total_launches"));
Log.info(json.asDouble("hourly"));
}
}
20 changes: 10 additions & 10 deletions src/test/java/dev/latvian/apps/webutils/test/XMLTests.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package dev.latvian.apps.webutils.test;

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.Log;
import dev.latvian.apps.webutils.html.XMLParser;
import org.junit.jupiter.api.Test;

public class XMLTests {
@Test
public void xml() {
var xml = XMLParser.parse("""
<?xml version="1.0" encoding="UTF-8"?>
<note>
<!--Comment-->
<to>Tove</to>
<from when="yesterday">Jani</from>
<heading>Reminder</heading>
<body a="b"c="d">Don't <b>forget</b> me this weekend!</body>
</note>""", false);
Ansi.log("XML:\n" + xml.toAnsi(true));
<?xml version="1.0" encoding="UTF-8"?>
<note>
<!--Comment-->
<to>Tove</to>
<from when="yesterday">Jani</from>
<heading>Reminder</heading>
<body a="b"c="d">Don't <b>forget</b> me this weekend!</body>
</note>""", false);
Log.info("XML:\n" + xml.toAnsi(true));
}
}

0 comments on commit 8de9993

Please sign in to comment.