Updated Branches:
  refs/heads/master 50b4b306c -> 2033fdc37

CB-1585 - simpler impl of iOS console bits

https://issues.apache.org/jira/browse/CB-1585
console.debug, console.info are undefined

The previous version of this code was getting busy,
and the fix to add debug/info were going to make
it even busier, so I made it simpler instead.

We now return the original console, if it exists,
and just patch the relevant methods on there.


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/2033fdc3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/2033fdc3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/2033fdc3

Branch: refs/heads/master
Commit: 2033fdc37d23b5aea724accc574eed7587aa9a6c
Parents: 50b4b30
Author: Patrick Mueller <pmue...@apache.org>
Authored: Mon Oct 8 12:35:49 2012 -0400
Committer: Patrick Mueller <pmue...@apache.org>
Committed: Mon Oct 8 12:35:49 2012 -0400

----------------------------------------------------------------------
 lib/ios/plugin/ios/console.js |   85 +++++++++++++-----------------------
 1 files changed, 30 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/2033fdc3/lib/ios/plugin/ios/console.js
----------------------------------------------------------------------
diff --git a/lib/ios/plugin/ios/console.js b/lib/ios/plugin/ios/console.js
index cf26b5f..b07c213 100644
--- a/lib/ios/plugin/ios/console.js
+++ b/lib/ios/plugin/ios/console.js
@@ -22,25 +22,6 @@
 var exec = require('cordova/exec');
 
 /**
- * This class provides access to the debugging console.
- * @constructor
- */
-var DebugConsole = function() {
-    this.logLevel = DebugConsole.INFO_LEVEL;
-};
-
-// from most verbose, to least verbose
-DebugConsole.ALL_LEVEL    = 1; // same as first level
-DebugConsole.INFO_LEVEL   = 1;
-DebugConsole.WARN_LEVEL   = 2;
-DebugConsole.ERROR_LEVEL  = 4;
-DebugConsole.NONE_LEVEL   = 8;
-
-DebugConsole.prototype.setLevel = function(level) {
-    this.logLevel = level;
-};
-
-/**
  * create a nice string for an object
  */
 function stringify(message) {
@@ -61,48 +42,42 @@ function stringify(message) {
 }
 
 /**
- * remember the original console and it's methods
+ * Wrapper one of the console logging methods, so that
+ * the Cordova logging native is called, then the original.
  */
-var origConsole = window.console || {},
-    origConsole_log   = origConsole.log   || function(){},
-    origConsole_warn  = origConsole.warn  || function(){},
-    origConsole_error = origConsole.error || function(){};
+function wrappedMethod(console, method) {
+    var origMethod = console[method];
 
+    return function(message) {
+        exec(null, null,
+            'Debug Console', 'log',
+            [ stringify(message), { logLevel: method.toUpperCase() } ]
+        );
 
-/**
- * Print a normal log message to the console
- * @param {Object|String} message Message or object to print to the console
- */
-DebugConsole.prototype.log = function(message) {
-    origConsole_log.apply(origConsole, arguments);
+        if (!origMethod) return;
 
-    if (this.logLevel <= DebugConsole.INFO_LEVEL) {
-        exec(null, null, 'Debug Console', 'log', [ stringify(message), { 
logLevel: 'INFO' } ]);
-    }
-};
+        origMethod.apply(console, arguments);
+    };
+}
 
-/**
- * Print a warning message to the console
- * @param {Object|String} message Message or object to print to the console
- */
-DebugConsole.prototype.warn = function(message) {
-    origConsole_warn.apply(origConsole, arguments);
+var console = window.console || {};
 
-    if (this.logLevel <= DebugConsole.WARN_LEVEL) {
-        exec(null, null, 'Debug Console', 'log', [ stringify(message), { 
logLevel: 'WARN' } ]);
-    }
-};
+// 2012-10-06 pmuellr - marking setLevel() method and logLevel property
+// on console as deprecated;
+// it didn't do anything useful, since the level constants weren't accessible
+// to anyone
 
-/**
- * Print an error message to the console
- * @param {Object|String} message Message or object to print to the console
- */
-DebugConsole.prototype.error = function(message) {
-    origConsole_error.apply(origConsole, arguments);
+console.setLevel = function() {};
+console.logLevel = 0;
 
-    if (this.logLevel <= DebugConsole.ERROR_LEVEL) {
-        exec(null, null, 'Debug Console', 'log', [ stringify(message), { 
logLevel: 'ERROR' } ]);
-    }
-};
+// wrapper the logging messages
+
+var methods = ["log", "debug", "info", "warn", "error"];
+
+for (var i=0; i<methods.length; i++) {
+    var method = methods[i];
+
+    console[method] = wrappedMethod(console, method);
+}
 
-module.exports = new DebugConsole();
+module.exports = console;

Reply via email to