Mon, 02 Jul 2018 16:43:07 +0100
Merge
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
32 /**
33 * Simple Web Console-like support for Nashorn. In addition to
34 * Web console object methods, this console add methods of
35 * java.io.Console as well. Note:not all web console methods are
36 * implemented but useful subset is implemented.
37 *
38 * See also: https://developer.mozilla.org/en/docs/Web/API/console
39 */
42 if (typeof console == 'undefined') {
44 (function() {
45 var LocalDateTime = Java.type("java.time.LocalDateTime");
46 var System = Java.type("java.lang.System");
47 var jconsole = System.console();
49 // add a new global variable called "console"
50 this.console = {
51 };
53 function addConsoleMethods() {
54 // expose methods of java.io.Console as an extension
55 var placeholder = "-*-";
56 // put a placeholder for each name from java.lang.Object
57 var objMethods = Object.bindProperties({}, new java.lang.Object());
58 for (var m in objMethods) {
59 console[m] = placeholder;
60 }
62 // bind only the methods of java.io.Console
63 // This bind will skip java.lang.Object methods as console
64 // has properties of same name.
65 Object.bindProperties(console, jconsole);
67 // Now, delete java.lang.Object methods
68 for (var m in console) {
69 if (console[m] == placeholder) {
70 delete console[m];
71 }
72 }
73 }
75 addConsoleMethods();
77 function consoleLog(type, msg) {
78 // print type of message, then time.
79 jconsole.format("%s [%s] ", type, LocalDateTime.now().toString());
80 if (typeof msg == 'string') {
81 jconsole.format(msg + "\n", Array.prototype.slice.call(arguments, 2));
82 } else {
83 // simple space separated values and newline at the end
84 var arr = Array.prototype.slice.call(arguments, 1);
85 jconsole.format("%s\n", arr.join(" "));
86 }
87 }
89 console.toString = function() "[object Console]";
91 // web console functions
93 console.assert = function(expr) {
94 if (! expr) {
95 arguments[0] = "Assertion Failed:";
96 consoleLog.apply(console, arguments);
97 // now, stack trace at the end
98 jconsole.format("%s\n", new Error().stack);
99 }
100 };
102 // dummy clear to avoid error!
103 console.clear = function() {};
105 var counter = {
106 get: function(label) {
107 if (! this[label]) {
108 return this[label] = 1;
109 } else {
110 return ++this[label];
111 }
112 }
113 };
115 // counter
116 console.count = function(label) {
117 label = label? String(label) : "<no label>";
118 jconsole.format("%s: %d\n",label, counter.get(label).intValue());
119 }
121 // logging
122 console.error = consoleLog.bind(jconsole, "ERROR");
123 console.info = consoleLog.bind(jconsole, "INFO");
124 console.log = console.info;
125 console.debug = console.log;
126 console.warn = consoleLog.bind(jconsole, "WARNING");
128 // print stack trace
129 console.trace = function() {
130 jconsole.format("%s\n", new Error().stack);
131 };
132 })();
134 }