Skip to content

Commit 184a080

Browse files
committed
GH-548 Replace complex Messenger with simple logging API (Resolve #540)
1 parent 6f49781 commit 184a080

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+132
-2073
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2020 Dzikoysk
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.panda_lang.language.interpreter.logging;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
24+
import static org.panda_lang.utilities.commons.collection.Lists.add;
25+
26+
public class Channel implements Comparable<Channel> {
27+
28+
private static final Collection<Channel> VALUES = new ArrayList<>();
29+
30+
public static Channel FATAL = add(VALUES, new Channel("fatal", 6.0));
31+
public static Channel ERROR = add(VALUES, new Channel("error", 5.0));
32+
public static Channel WARN = add(VALUES, new Channel("warn", 4.0));
33+
public static Channel INFO = add(VALUES, new Channel("info", 3.0));
34+
public static Channel DEBUG = add(VALUES, new Channel("debug", 2.0));
35+
public static Channel TRACE = add(VALUES, new Channel("trace", 1.0));
36+
37+
private final String channel;
38+
private final double priority;
39+
40+
public Channel(String channel, double priority) {
41+
this.channel = channel;
42+
this.priority = priority;
43+
}
44+
45+
@Override
46+
public int compareTo(@NotNull Channel o) {
47+
return Double.compare(priority, o.priority);
48+
}
49+
50+
public double getPriority() {
51+
return priority;
52+
}
53+
54+
public String getChannel() {
55+
return channel;
56+
}
57+
58+
public static Channel of(String channel) {
59+
for (Channel value : VALUES) {
60+
if (value.getChannel().equalsIgnoreCase(channel)) {
61+
return value;
62+
}
63+
}
64+
65+
throw new UnsupportedOperationException(channel + " is not supported by default");
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2020 Dzikoysk
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.panda_lang.language.interpreter.logging;
18+
19+
public interface Logger {
20+
21+
void log(Channel channel, String message);
22+
23+
void exception(Throwable throwable);
24+
25+
default void fatal(String message) {
26+
log(Channel.FATAL, message);
27+
}
28+
29+
default void error(String message) {
30+
log(Channel.ERROR, message);
31+
}
32+
33+
default void warn(String message) {
34+
log(Channel.WARN, message);
35+
}
36+
37+
default void info(String message) {
38+
log(Channel.INFO, message);
39+
}
40+
41+
default void debug(String message) {
42+
log(Channel.DEBUG, message);
43+
}
44+
45+
default void trace(String message) {
46+
log(Channel.TRACE, message);
47+
}
48+
49+
}

panda-framework/src/main/java/org/panda_lang/language/interpreter/messenger/LoggerHolder.java renamed to panda-framework/src/main/java/org/panda_lang/language/interpreter/logging/LoggerHolder.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.panda_lang.language.interpreter.messenger;
18-
19-
import org.slf4j.Logger;
17+
package org.panda_lang.language.interpreter.logging;
2018

2119
/**
2220
* Logger container
+14-12
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.panda_lang.language.interpreter.messenger;
17+
package org.panda_lang.language.interpreter.logging;
1818

19-
import org.slf4j.event.Level;
19+
public final class SystemLogger implements Logger {
2020

21-
public final class PandaMessengerMessage implements MessengerMessage {
21+
private final Channel threshold;
2222

23-
private final Level level;
24-
private final String message;
23+
public SystemLogger() {
24+
this(Channel.INFO);
25+
}
2526

26-
public PandaMessengerMessage(Level level, String message) {
27-
this.message = message;
28-
this.level = level;
27+
public SystemLogger(Channel threshold) {
28+
this.threshold = threshold;
2929
}
3030

3131
@Override
32-
public String getContent() {
33-
return message;
32+
public void log(Channel channel, String message) {
33+
if (channel.getPriority() >= threshold.getPriority()) {
34+
System.out.println(message);
35+
}
3436
}
3537

3638
@Override
37-
public Level getLevel() {
38-
return level;
39+
public void exception(Throwable throwable) {
40+
throwable.printStackTrace(System.err);
3941
}
4042

4143
}

panda-framework/src/main/java/org/panda_lang/language/interpreter/messenger/FormatterFunction.java

-30
This file was deleted.

panda-framework/src/main/java/org/panda_lang/language/interpreter/messenger/LoggerMessengerOutputListener.java

-59
This file was deleted.

panda-framework/src/main/java/org/panda_lang/language/interpreter/messenger/Messenger.java

-82
This file was deleted.

0 commit comments

Comments
 (0)