File tree Expand file tree Collapse file tree
main/java/com/scriptbasic
java/com/scriptbasic/testprograms
resources/com/scriptbasic/testprograms Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1010import com .scriptbasic .executors .rightvalues .BasicArrayValue ;
1111import com .scriptbasic .interfaces .BasicRuntimeException ;
1212import com .scriptbasic .interfaces .ExtendedInterpreter ;
13+ import com .scriptbasic .log .Logger ;
14+ import com .scriptbasic .log .LoggerFactory ;
1315import com .scriptbasic .utility .MagicBean ;
1416import com .scriptbasic .utility .NoInstance ;
1517
2830 */
2931public 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 *
Original file line number Diff line number Diff line change 1111
1212import java .util .HashMap ;
1313import java .util .Map ;
14- import java .util .stream .Stream ;
1514
1615import static org .junit .Assert .assertFalse ;
1716import 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
Original file line number Diff line number Diff line change 1+ logError "this is an error message"
2+ logInfo "this is an info message"
3+ logDebug "this is a debug message"
You can’t perform that action at this time.
0 commit comments