sundar@1373: /* sundar@1373: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1373: * sundar@1373: * Redistribution and use in source and binary forms, with or without sundar@1373: * modification, are permitted provided that the following conditions sundar@1373: * are met: sundar@1373: * sundar@1373: * - Redistributions of source code must retain the above copyright sundar@1373: * notice, this list of conditions and the following disclaimer. sundar@1373: * sundar@1373: * - Redistributions in binary form must reproduce the above copyright sundar@1373: * notice, this list of conditions and the following disclaimer in the sundar@1373: * documentation and/or other materials provided with the distribution. sundar@1373: * sundar@1373: * - Neither the name of Oracle nor the names of its sundar@1373: * contributors may be used to endorse or promote products derived sundar@1373: * from this software without specific prior written permission. sundar@1373: * sundar@1373: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS sundar@1373: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, sundar@1373: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sundar@1373: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sundar@1373: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sundar@1373: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sundar@1373: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sundar@1373: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sundar@1373: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sundar@1373: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sundar@1373: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sundar@1373: */ sundar@1373: sundar@1373: /** sundar@1373: * Simple Web Console-like support for Nashorn. In addition to sundar@1373: * Web console object methods, this console add methods of sundar@1373: * java.io.Console as well. Note:not all web console methods are sundar@1373: * implemented but useful subset is implemented. sundar@1373: * sundar@1373: * See also: https://developer.mozilla.org/en/docs/Web/API/console sundar@1373: */ sundar@1373: sundar@1373: sundar@1373: if (typeof console == 'undefined') { sundar@1373: sundar@1373: (function() { sundar@1373: var LocalDateTime = Java.type("java.time.LocalDateTime"); sundar@1373: var System = Java.type("java.lang.System"); sundar@1373: var jconsole = System.console(); sundar@1373: sundar@1373: // add a new global variable called "console" sundar@1373: this.console = { sundar@1373: }; sundar@1373: sundar@1373: function addConsoleMethods() { sundar@1373: // expose methods of java.io.Console as an extension sundar@1373: var placeholder = "-*-"; sundar@1373: // put a placeholder for each name from java.lang.Object sundar@1373: var objMethods = Object.bindProperties({}, new java.lang.Object()); sundar@1373: for (var m in objMethods) { sundar@1373: console[m] = placeholder; sundar@1373: } sundar@1373: sundar@1373: // bind only the methods of java.io.Console sundar@1373: // This bind will skip java.lang.Object methods as console sundar@1373: // has properties of same name. sundar@1373: Object.bindProperties(console, jconsole); sundar@1373: sundar@1373: // Now, delete java.lang.Object methods sundar@1373: for (var m in console) { sundar@1373: if (console[m] == placeholder) { sundar@1373: delete console[m]; sundar@1373: } sundar@1373: } sundar@1373: } sundar@1373: sundar@1373: addConsoleMethods(); sundar@1373: sundar@1373: function consoleLog(type, msg) { sundar@1373: // print type of message, then time. sundar@1373: jconsole.format("%s [%s] ", type, LocalDateTime.now().toString()); sundar@1373: if (typeof msg == 'string') { sundar@1373: jconsole.format(msg + "\n", Array.prototype.slice.call(arguments, 2)); sundar@1373: } else { sundar@1373: // simple space separated values and newline at the end sundar@1373: var arr = Array.prototype.slice.call(arguments, 1); sundar@1373: jconsole.format("%s\n", arr.join(" ")); sundar@1373: } sundar@1373: } sundar@1373: sundar@1373: console.toString = function() "[object Console]"; sundar@1373: sundar@1373: // web console functions sundar@1373: sundar@1373: console.assert = function(expr) { sundar@1373: if (! expr) { sundar@1373: arguments[0] = "Assertion Failed:"; sundar@1373: consoleLog.apply(console, arguments); sundar@1373: // now, stack trace at the end sundar@1373: jconsole.format("%s\n", new Error().stack); sundar@1373: } sundar@1373: }; sundar@1373: sundar@1373: // dummy clear to avoid error! sundar@1373: console.clear = function() {}; sundar@1373: sundar@1373: var counter = { sundar@1373: get: function(label) { sundar@1373: if (! this[label]) { sundar@1373: return this[label] = 1; sundar@1373: } else { sundar@1373: return ++this[label]; sundar@1373: } sundar@1373: } sundar@1373: }; sundar@1373: sundar@1373: // counter sundar@1373: console.count = function(label) { sundar@1373: label = label? String(label) : ""; sundar@1373: jconsole.format("%s: %d\n",label, counter.get(label).intValue()); sundar@1373: } sundar@1373: sundar@1373: // logging sundar@1373: console.error = consoleLog.bind(jconsole, "ERROR"); sundar@1373: console.info = consoleLog.bind(jconsole, "INFO"); sundar@1373: console.log = console.info; sundar@1373: console.debug = console.log; sundar@1373: console.warn = consoleLog.bind(jconsole, "WARNING"); sundar@1373: sundar@1373: // print stack trace sundar@1373: console.trace = function() { sundar@1373: jconsole.format("%s\n", new Error().stack); sundar@1373: }; sundar@1373: })(); sundar@1373: sundar@1373: }