| 16 | import java.util.regex.Pattern; |
| 17 | |
| 18 | public class NativeConsole extends ScriptableObject { |
| 19 | @Serial private static final long serialVersionUID = 5694613212458273057L; |
| 20 | |
| 21 | private static final String CLASS_NAME = "Console"; |
| 22 | |
| 23 | private static final String DEFAULT_LABEL = "default"; |
| 24 | |
| 25 | private static final Pattern FMT_REG = Pattern.compile("%[sfdioOc%]"); |
| 26 | |
| 27 | private final Map<String, Long> timers = new ConcurrentHashMap<>(); |
| 28 | |
| 29 | private final Map<String, AtomicInteger> counters = new ConcurrentHashMap<>(); |
| 30 | |
| 31 | private final ConsolePrinter printer; |
| 32 | |
| 33 | public enum Level { |
| 34 | TRACE, |
| 35 | DEBUG, |
| 36 | INFO, |
| 37 | WARN, |
| 38 | ERROR |
| 39 | } |
| 40 | |
| 41 | public interface ConsolePrinter extends Serializable { |
| 42 | void print( |
| 43 | Context cx, VarScope scope, Level level, Object[] args, ScriptStackElement[] stack); |
| 44 | } |
| 45 | |
| 46 | public static void init(VarScope scope, boolean sealed, ConsolePrinter printer) { |
| 47 | NativeConsole obj = new NativeConsole(printer); |
| 48 | obj.setPrototype(getObjectPrototype(scope)); |
| 49 | obj.setParentScope(scope); |
| 50 | |
| 51 | obj.defineProperty( |
| 52 | scope, "toSource", 0, NativeConsole::js_toSource, 0, DONTENUM | READONLY); |
| 53 | obj.defineBuiltinProperty(scope, "trace", 1, obj::js_trace, 0, DONTENUM | READONLY); |
| 54 | obj.defineBuiltinProperty(scope, "debug", 1, obj::js_debug, 0, DONTENUM | READONLY); |
| 55 | obj.defineBuiltinProperty(scope, "log", 1, obj::js_log, 0, DONTENUM | READONLY); |
| 56 | obj.defineBuiltinProperty(scope, "info", 1, obj::js_info, 0, DONTENUM | READONLY); |
| 57 | obj.defineBuiltinProperty(scope, "warn", 1, obj::js_warn, 0, DONTENUM | READONLY); |
| 58 | obj.defineBuiltinProperty(scope, "error", 1, obj::js_error, 0, DONTENUM | READONLY); |
| 59 | obj.defineBuiltinProperty(scope, "assert", 2, obj::js_assert, 0, DONTENUM | READONLY); |
| 60 | obj.defineBuiltinProperty(scope, "count", 1, obj::js_count, 0, DONTENUM | READONLY); |
| 61 | obj.defineBuiltinProperty( |
| 62 | scope, "countReset", 1, obj::js_countReset, 0, DONTENUM | READONLY); |
| 63 | obj.defineBuiltinProperty(scope, "time", 1, obj::js_time, 0, DONTENUM | READONLY); |
| 64 | obj.defineBuiltinProperty(scope, "timeEnd", 1, obj::js_timeEnd, 0, DONTENUM | READONLY); |
| 65 | obj.defineBuiltinProperty(scope, "timeLog", 2, obj::js_timeLog, 0, DONTENUM | READONLY); |
| 66 | if (sealed) { |
| 67 | obj.sealObject(); |
| 68 | } |
| 69 | ScriptableObject.defineProperty(scope, "console", obj, ScriptableObject.DONTENUM); |
| 70 | } |
| 71 | |
| 72 | private NativeConsole(ConsolePrinter printer) { |
| 73 | this.printer = printer; |
| 74 | } |
| 75 | |