Skip to content

Commit e75c3be

Browse files
committed
BASIC program logging possibility via logError, logInfo, logDebug is implemented
1 parent 5ed7352 commit e75c3be

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

src/main/java/com/scriptbasic/log/LoggerFactory.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class LoggerFactory {
1414

1515
/**
1616
* Get the logger that has the name which is the same as the name of the class where the logger is used.
17-
*
17+
* <p>
1818
* Loggers are usually stored in static fields.
1919
*
2020
* @return the logger instance.
@@ -32,4 +32,24 @@ public static synchronized Logger getLogger() {
3232
}
3333
return logger;
3434
}
35+
36+
public static synchronized Logger getBasicLogger() {
37+
Class<?> klass =
38+
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
39+
.walk(stream -> stream
40+
.filter(frame ->
41+
frame.getDeclaringClass().getModule()
42+
.equals(LoggerFactory.class.getModule())
43+
).findFirst()).get().getDeclaringClass();
44+
final Logger logger;
45+
if (loggers.containsKey(klass)) {
46+
logger = loggers.get(klass);
47+
} else {
48+
logger = new Logger(
49+
java.lang.System.LoggerFinder.getLoggerFinder()
50+
.getLogger("BASIC", klass.getModule()));
51+
loggers.put(klass, logger);
52+
}
53+
return logger;
54+
}
3555
}

src/main/java/com/scriptbasic/utility/functions/UtilityFunctions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.scriptbasic.executors.rightvalues.BasicArrayValue;
1111
import com.scriptbasic.interfaces.BasicRuntimeException;
1212
import com.scriptbasic.interfaces.ExtendedInterpreter;
13+
import com.scriptbasic.log.Logger;
14+
import com.scriptbasic.log.LoggerFactory;
1315
import com.scriptbasic.utility.MagicBean;
1416
import com.scriptbasic.utility.NoInstance;
1517

@@ -28,6 +30,8 @@
2830
*/
2931
public class UtilityFunctions {
3032

33+
private static final Logger BASIC_LOGGER = LoggerFactory.getBasicLogger();
34+
3135
private UtilityFunctions() {
3236
NoInstance.isPossible();
3337
}
@@ -58,6 +62,21 @@ public static Object newObject(String klass) throws
5862
}
5963
}
6064

65+
@Function(classification = System.class)
66+
public static void logError(String message) {
67+
BASIC_LOGGER.error(message);
68+
}
69+
70+
@Function(classification = System.class)
71+
public static void logInfo(String message) {
72+
BASIC_LOGGER.info(message);
73+
}
74+
75+
@Function(classification = System.class)
76+
public static void logDebug(String message) {
77+
BASIC_LOGGER.debug(message);
78+
}
79+
6180
/**
6281
* This function returns the undef value.
6382
*

src/test/java/com/scriptbasic/testprograms/TestPrograms.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import java.util.HashMap;
1313
import java.util.Map;
14-
import java.util.stream.Stream;
1514

1615
import static org.junit.Assert.assertFalse;
1716
import static org.junit.Assert.assertTrue;
@@ -129,7 +128,28 @@ public void testPrograms() throws Exception {
129128

130129
@Test
131130
public void testLength() throws Exception {
132-
codeTest("TestLengthFunction.bas","");
131+
codeTest("TestLengthFunction.bas", "");
132+
}
133+
134+
/**
135+
* This is not really a unit test because it does not check that the actual
136+
* BASIC program sends the strings to the standard log output.
137+
* It would be too complex and mainly pointless, since the functionality is
138+
* simple and redirecting the log to a channel that can be checked during test
139+
* execution is more complex than the whole functionality.
140+
* <p>
141+
* Just look at the log and search for the messages
142+
* "this is an error message"
143+
* "this is an info message"
144+
* "this is a debug message"
145+
* <p>
146+
* Note that some may be missing in case the logging is configured not to emit debug messages
147+
*
148+
* @throws Exception
149+
*/
150+
@Test
151+
public void testLogging() throws Exception {
152+
codeTest("TestLogging.bas", "");
133153
}
134154

135155
@Test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logError "this is an error message"
2+
logInfo "this is an info message"
3+
logDebug "this is a debug message"

0 commit comments

Comments
 (0)