You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mm...@apache.org on 2014/05/06 21:20:50 UTC

[4/4] git commit: Make a working on screen logger

Make a working on screen logger


Project: http://git-wip-us.apache.org/repos/asf/cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-labs/commit/8ae8e033
Tree: http://git-wip-us.apache.org/repos/asf/cordova-labs/tree/8ae8e033
Diff: http://git-wip-us.apache.org/repos/asf/cordova-labs/diff/8ae8e033

Branch: refs/heads/cdvtest
Commit: 8ae8e03361e3dce02b0c37924e8e9195cd233b79
Parents: d992412
Author: Michal Mocny <mm...@gmail.com>
Authored: Tue May 6 15:20:34 2014 -0400
Committer: Michal Mocny <mm...@gmail.com>
Committed: Tue May 6 15:20:34 2014 -0400

----------------------------------------------------------------------
 .../www/assets/main.css                         | 17 +++-
 cordova-plugin-test-framework/www/main.js       | 83 +++++++++++++++-----
 2 files changed, 79 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/8ae8e033/cordova-plugin-test-framework/www/assets/main.css
----------------------------------------------------------------------
diff --git a/cordova-plugin-test-framework/www/assets/main.css b/cordova-plugin-test-framework/www/assets/main.css
index 43f5ca4..4e33267 100644
--- a/cordova-plugin-test-framework/www/assets/main.css
+++ b/cordova-plugin-test-framework/www/assets/main.css
@@ -27,6 +27,7 @@ html, body {
   position: absolute;
   top: 30px;
   bottom: 20px;
+  padding-bottom: 40%;
   width: 100%;
   overflow-y: auto;
   overflow-x: auto;
@@ -43,7 +44,7 @@ html, body {
 }
 
 #log.expanded {
-  height: 30%;
+  height: 40%;
 }
 
 #log--title {
@@ -64,7 +65,19 @@ html, body {
   background-color: white;
 }
 
-#log--content--line {
+.log--content--line {
   border-bottom: 1px solid #ccc;
   white-space: pre;
 }
+
+.log--content--line--log {
+  background-color: white;
+}
+
+.log--content--line--warn {
+  background-color: #FCFFA6;
+}
+
+.log--content--line--error {
+  background-color: #FFA6A6;
+}

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/8ae8e033/cordova-plugin-test-framework/www/main.js
----------------------------------------------------------------------
diff --git a/cordova-plugin-test-framework/www/main.js b/cordova-plugin-test-framework/www/main.js
index 54b1d64..d5ecad0 100644
--- a/cordova-plugin-test-framework/www/main.js
+++ b/cordova-plugin-test-framework/www/main.js
@@ -57,13 +57,19 @@ function clearContent() {
   log.innerHTML = '';
   var buttons = document.getElementById('buttons');
   buttons.innerHTML = '';
+
+  setLogVisibility(false);
 }
 
+/******************************************************************************/
+
 function setTitle(title) {
   var el = document.getElementById('title');
   el.textContent = title;
 }
 
+/******************************************************************************/
+
 function setLogVisibility(visible) {
   if (visible) {
     document.getElementById('log').classList.add('expanded');
@@ -72,6 +78,57 @@ function setLogVisibility(visible) {
   }
 }
 
+function toggleLogVisibility() {
+  var log = document.getElementById('log');
+  if (log.classList.contains('expanded')) {
+    log.classList.remove('expanded');
+  } else {
+    log.classList.add('expanded');
+  }
+}
+
+/******************************************************************************/
+
+function attachEvents() {
+  document.getElementById('log--title').addEventListener('click', toggleLogVisibility);
+}
+
+/******************************************************************************/
+
+function wrapConsole() {
+  var origConsole = window.console;
+
+  function appendToOnscreenLog(type, args) {
+    var el = document.getElementById('log--content');
+    var div = document.createElement('div');
+    div.classList.add('log--content--line');
+    div.classList.add('log--content--line--' + type);
+    div.textContent = Array.prototype.slice.apply(args).map(function(arg) {
+        return (typeof arg === 'string') ? arg : JSON.stringify(arg);
+      }).join(' ');
+    el.appendChild(div);
+    // scroll to bottom
+    el.scrollTop = el.scrollHeight;
+  }
+
+  function createCustomLogger(type) {
+    return function() {
+      origConsole[type].apply(origConsole, arguments);
+      //window.medic.log.apply(window.medic.log, arguments);
+      appendToOnscreenLog(type, arguments);
+      setLogVisibility(true);
+    }
+  }
+
+  window.console = {
+    log: createCustomLogger('log'),
+    warn: createCustomLogger('warn'),
+    error: createCustomLogger('error'),
+  }
+}
+
+/******************************************************************************/
+
 function createActionButton(title, callback) {
   var buttons = document.getElementById('buttons');
   var div = document.createElement('div');
@@ -86,27 +143,12 @@ function createActionButton(title, callback) {
   buttons.appendChild(div);
 }
 
-// TODO: make a better logger
-function logger() {
-  console.log.apply(console, arguments);
-  //window.medic.log.apply(window.medic.log, arguments);
-
-  var el = document.getElementById('log--content');
-  var div = document.createElement('div');
-  div.classList.add('log--content--line');
-  div.textContent = Array.prototype.slice.apply(arguments).map(function(arg) {
-      return (typeof arg === 'string') ? arg : JSON.stringify(arg);
-    }).join(' ');
-  el.appendChild(div);
-  // scroll to bottom
-  el.scrollTop = el.scrollHeight;
-}
-
+/******************************************************************************/
+/******************************************************************************/
 /******************************************************************************/
 
 function runAutoTests() {
   setTitle('Auto Tests');
-  setLogVisibility(true);
 
   createActionButton('Again', setMode.bind(null, 'auto'));
   createActionButton('Reset App', location.reload.bind(location));
@@ -124,7 +166,6 @@ function runAutoTests() {
 
 function runManualTests() {
   setTitle('Manual Tests');
-  setLogVisibility(true);
 
   createActionButton('Reset App', location.reload.bind(location));
   createActionButton('Back', setMode.bind(null, 'main'));
@@ -143,7 +184,6 @@ function runManualTests() {
 
 function runMain() {
   setTitle('Cordova Tests');
-  setLogVisibility(false);
 
   createActionButton('Auto Tests', setMode.bind(null, 'auto'));
   createActionButton('Manual Tests', setMode.bind(null, 'manual'));
@@ -161,6 +201,11 @@ exports.init = function() {
     }
   });
   */
+  // TODO: have a way to opt-out of console wrapping in case line numbers are important.
+  // ...Or find a custom way to print line numbers using stack or something.
+  attachEvents();
+  wrapConsole();
+
   getMode(setMode);
 };