You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pm...@apache.org on 2012/05/22 19:42:58 UTC
js commit: [CB-348] console object improvements
Updated Branches:
refs/heads/master 56051d527 -> 4c8c52f9c
[CB-348] console object improvements
added the following files as a new implementation of
console on top of the logger API. Currently, no one
is using them; will switch iOS to use this as soon
as cordova-js and cordova-ios are synchronized.
- lib/common/plugin/console-via-logger.js
- test/test.console-via-logger.js
fixes to the following:
- Jakefile
add additional warnings to skip (new one for console redefinition)
- lib/common/plugin/logger.js
changed the protocol for logger/console "using" checks
- test/runner.js
modified test matcher to accept hyphens in test file names
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/4c8c52f9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/4c8c52f9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/4c8c52f9
Branch: refs/heads/master
Commit: 4c8c52f9c52e170c2f66648ea281d24f12cb27ef
Parents: 56051d5
Author: Patrick Mueller <pm...@apache.org>
Authored: Tue May 22 13:38:43 2012 -0400
Committer: Patrick Mueller <pm...@apache.org>
Committed: Tue May 22 13:38:43 2012 -0400
----------------------------------------------------------------------
Jakefile | 7 +-
lib/common/plugin/console-via-logger.js | 166 +++++++++++++++++++++++++
lib/common/plugin/logger.js | 6 +-
test/runner.js | 2 +-
test/test.console-via-logger.js | 172 ++++++++++++++++++++++++++
5 files changed, 349 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/4c8c52f9/Jakefile
----------------------------------------------------------------------
diff --git a/Jakefile b/Jakefile
index fad61ef..e83c0db 100644
--- a/Jakefile
+++ b/Jakefile
@@ -99,7 +99,12 @@ task('set-cwd', [], function() {
desc('check sources with JSHint');
task('hint', ['complainwhitespace'], function () {
- var knownWarnings = ["Redefinition of 'FileReader'", "Redefinition of 'require'", "Read only"];
+ var knownWarnings = [
+ "Redefinition of 'FileReader'",
+ "Redefinition of 'require'",
+ "Read only",
+ "Redefinition of 'console'"
+ ];
var filterKnownWarnings = function(el, index, array) {
var wut = true;
// filter out the known warnings listed out above
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/4c8c52f9/lib/common/plugin/console-via-logger.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/console-via-logger.js b/lib/common/plugin/console-via-logger.js
new file mode 100644
index 0000000..81d95f2
--- /dev/null
+++ b/lib/common/plugin/console-via-logger.js
@@ -0,0 +1,166 @@
+//------------------------------------------------------------------------------
+
+var logger = require("cordova/plugin/logger");
+var utils = require("cordova/utils");
+
+//------------------------------------------------------------------------------
+// object that we're exporting
+//------------------------------------------------------------------------------
+var console = module.exports;
+
+//------------------------------------------------------------------------------
+// copy of the original console object
+//------------------------------------------------------------------------------
+var WinConsole = window.console;
+
+//------------------------------------------------------------------------------
+// whether to use the logger
+//------------------------------------------------------------------------------
+var UseLogger = false;
+
+//------------------------------------------------------------------------------
+// Timers
+//------------------------------------------------------------------------------
+var Timers = {};
+
+//------------------------------------------------------------------------------
+// used for unimplemented methods
+//------------------------------------------------------------------------------
+function noop() {}
+
+//------------------------------------------------------------------------------
+// used for unimplemented methods
+//------------------------------------------------------------------------------
+console.useLogger = function (value) {
+ if (arguments.length) UseLogger = !!value;
+
+ if (UseLogger) {
+ if (logger.useConsole()) {
+ throw new Error("console and logger are too intertwingly");
+ }
+ }
+
+ return UseLogger;
+};
+
+//------------------------------------------------------------------------------
+console.log = function() {
+ if (logger.useConsole()) return;
+ logger.log.apply(logger, [].slice.call(arguments));
+};
+
+//------------------------------------------------------------------------------
+console.error = function() {
+ if (logger.useConsole()) return;
+ logger.error.apply(logger, [].slice.call(arguments));
+};
+
+//------------------------------------------------------------------------------
+console.warn = function() {
+ if (logger.useConsole()) return;
+ logger.warn.apply(logger, [].slice.call(arguments));
+};
+
+//------------------------------------------------------------------------------
+console.info = function() {
+ if (logger.useConsole()) return;
+ logger.info.apply(logger, [].slice.call(arguments));
+};
+
+//------------------------------------------------------------------------------
+console.debug = function() {
+ if (logger.useConsole()) return;
+ logger.debug.apply(logger, [].slice.call(arguments));
+};
+
+//------------------------------------------------------------------------------
+console.assert = function(expression) {
+ if (expression) return;
+
+ var message = utils.vformat(arguments[1], [].slice.call(arguments, 2));
+ console.log("ASSERT: " + message);
+};
+
+//------------------------------------------------------------------------------
+console.clear = function() {};
+
+//------------------------------------------------------------------------------
+console.dir = function(object) {
+ console.log("%o", object);
+};
+
+//------------------------------------------------------------------------------
+console.dirxml = function(node) {
+ console.log(node.innerHTML);
+};
+
+//------------------------------------------------------------------------------
+console.trace = noop;
+
+//------------------------------------------------------------------------------
+console.group = console.log;
+
+//------------------------------------------------------------------------------
+console.groupCollapsed = console.log;
+
+//------------------------------------------------------------------------------
+console.groupEnd = noop;
+
+//------------------------------------------------------------------------------
+console.time = function(name) {
+ Timers[name] = new Date().valueOf();
+};
+
+//------------------------------------------------------------------------------
+console.timeEnd = function(name) {
+ var timeStart = Timers[name];
+ if (!timeStart) {
+ console.warn("unknown timer: " + name);
+ return;
+ }
+
+ var timeElapsed = new Date().valueOf() - timeStart;
+ console.log(name + ": " + timeElapsed + "ms");
+};
+
+//------------------------------------------------------------------------------
+console.timeStamp = noop;
+
+//------------------------------------------------------------------------------
+console.profile = noop;
+
+//------------------------------------------------------------------------------
+console.profileEnd = noop;
+
+//------------------------------------------------------------------------------
+console.count = noop;
+
+//------------------------------------------------------------------------------
+console.exception = console.log;
+
+//------------------------------------------------------------------------------
+console.table = function(data, columns) {
+ console.log("%o", data);
+};
+
+//------------------------------------------------------------------------------
+// return a new function that calls both functions passed as args
+//------------------------------------------------------------------------------
+function wrapperedOrigCall(orgFunc, newFunc) {
+ return function() {
+ var args = [].slice.call(arguments);
+ try { orgFunc.apply(WinConsole, args); } catch (e) {}
+ try { newFunc.apply(console, args); } catch (e) {}
+ };
+}
+
+//------------------------------------------------------------------------------
+// For every function that exists in the original console object, that
+// also exists in the new console object, wrap the new console method
+// with one that calls both
+//------------------------------------------------------------------------------
+for (var key in console) {
+ if (typeof WinConsole[key] == "function") {
+ console[key] = wrapperedOrigCall(WinConsole[key], console[key]);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/4c8c52f9/lib/common/plugin/logger.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/logger.js b/lib/common/plugin/logger.js
index 812fed3..62946e9 100644
--- a/lib/common/plugin/logger.js
+++ b/lib/common/plugin/logger.js
@@ -104,8 +104,10 @@ logger.useConsole = function (value) {
throw new Error("global console object does not have a log function");
}
- if (console.__usingCordovaLogger) {
- throw new Error("console and logger are too intertwingly");
+ if (typeof console.useLogger == "function") {
+ if (console.useLogger()) {
+ throw new Error("console and logger are too intertwingly");
+ }
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/4c8c52f9/test/runner.js
----------------------------------------------------------------------
diff --git a/test/runner.js b/test/runner.js
index 502a726..eaf15ab 100644
--- a/test/runner.js
+++ b/test/runner.js
@@ -7,7 +7,7 @@ var fs = require('fs'),
function collect(path, files, matches) {
matches = matches || function (path) {
- return path.match(/test\.\w+\.js$/);
+ return path.match(/test\.(\w|-)+\.js$/);
};
if (fs.statSync(path).isDirectory()) {
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/4c8c52f9/test/test.console-via-logger.js
----------------------------------------------------------------------
diff --git a/test/test.console-via-logger.js b/test/test.console-via-logger.js
new file mode 100644
index 0000000..08b4366
--- /dev/null
+++ b/test/test.console-via-logger.js
@@ -0,0 +1,172 @@
+describe("console-via-logger", function () {
+
+ var cordova = require('cordova')
+ var logger = require('cordova/plugin/logger')
+ var console = require('cordova/plugin/console-via-logger')
+ var exec = require("cordova/exec")
+ var savedLevel
+
+ // fake device ready, but can't test the queued messages this way
+ logger.__onDeviceReady()
+
+ //--------------------------------------------------------------------------
+ beforeEach(function() {
+ savedLevel = logger.level()
+ logger.level(logger.DEBUG)
+ logger.useConsole(false)
+ console.useLogger(true)
+ exec.reset();
+ })
+
+ //--------------------------------------------------------------------------
+ afterEach(function() {
+ logger.level(savedLevel)
+ logger.useConsole(true)
+ console.useLogger(false)
+ })
+
+ //--------------------------------------------------------------------------
+ it("is using the logger", function () {
+ expect(console.useLogger()).toBe(true)
+ })
+
+ //--------------------------------------------------------------------------
+ it("is not being used by logger", function () {
+ expect(logger.useConsole()).toBe(false)
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements log() correctly", function () {
+ console.log("%s", "1")
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "1"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements error() correctly", function () {
+ console.error("%s", "2")
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.ERROR, "2"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements warn() correctly", function () {
+ console.warn("%s", "3")
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.WARN, "3"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements info() correctly", function () {
+ console.info("%s", "4")
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.INFO, "4"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements debug() correctly", function () {
+ console.debug("%s", "5")
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.DEBUG, "5"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements assert(false) correctly", function () {
+ console.assert(false, "%s", 6)
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "ASSERT: 6"]);
+ })
+
+ it("implements assert(true) correctly", function () {
+ console.assert(true, "%s", 6)
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for clear()", function () {
+ console.clear()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements dir() correctly", function () {
+ console.dir({a:1, b:2})
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, '{"a":1,"b":2}']);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements dirxml() correctly", function () {
+ console.dirxml({ innerHTML: "<b>nonce</b>" })
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "<b>nonce</b>"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for trace()", function () {
+ console.trace()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements group() correctly", function () {
+ console.group("%s", 7)
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "7"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements groupCollapsed() correctly", function () {
+ console.groupCollapsed("%s", 8)
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "8"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for groupEnd()", function () {
+ console.groupEnd()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements time() and timeEnd() correctly", function () {
+ runs(function() {
+ console.time("foo")
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ waits(50)
+
+ runs(function() {
+ console.timeEnd("foo")
+ var message = exec.mostRecentCall.args[4][1]
+ expect(message).toMatch(/foo: \d+ms/)
+ })
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for timeStamp()", function () {
+ console.timeStamp()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for profile()", function () {
+ console.profile()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for profileEnd()", function () {
+ console.profileEnd()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("does nothing for count()", function () {
+ console.count()
+ expect(exec).not.toHaveBeenCalled();
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements exception() correctly", function () {
+ console.exception(new Error("bar"))
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, "Error: bar"]);
+ })
+
+ //--------------------------------------------------------------------------
+ it("implements table() correctly", function () {
+ console.table({c:3,d:4})
+ expect(exec).toHaveBeenCalledWith(null, null, "Logger", "logLevel", [logger.LOG, '{"c":3,"d":4}']);
+ })
+})