You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2015/09/08 16:11:11 UTC

cordova-windows git commit: CB-8936 Introduced --dump arg to log script. This closes #119

Repository: cordova-windows
Updated Branches:
  refs/heads/master be8c99cd0 -> 7ae75b5db


CB-8936 Introduced --dump arg to log script. This closes #119


Project: http://git-wip-us.apache.org/repos/asf/cordova-windows/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-windows/commit/7ae75b5d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-windows/tree/7ae75b5d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-windows/diff/7ae75b5d

Branch: refs/heads/master
Commit: 7ae75b5dbb796dd6250271f4f9e6a3a5475922fe
Parents: be8c99c
Author: Snay <me...@snay.me>
Authored: Mon Aug 31 20:18:06 2015 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Tue Sep 8 17:10:46 2015 +0300

----------------------------------------------------------------------
 template/cordova/lib/log.js | 128 +++++++++++++++++++++++++++++----------
 template/cordova/log        |  21 ++++---
 2 files changed, 107 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/7ae75b5d/template/cordova/lib/log.js
----------------------------------------------------------------------
diff --git a/template/cordova/lib/log.js b/template/cordova/lib/log.js
index 4b741ab..f095f4b 100644
--- a/template/cordova/lib/log.js
+++ b/template/cordova/lib/log.js
@@ -22,41 +22,64 @@ var path         = require('path'),
     et           = require('elementtree'),
     Q            = require('q'),
     cp           = require('child_process'),
-    ConfigParser = require('./ConfigParser.js');
+    ConfigParser = require('./ConfigParser.js'),
+    nopt         = require('nopt');
 
 // paths
 var platformRoot = path.join(__dirname, '..', '..'),
     projectRoot  = path.join(platformRoot, '..', '..'),
     configPath   = path.join(projectRoot, 'config.xml');
 
+//constants
+var APP_TRACING_LOG = 'Microsoft-Windows-AppHost/ApplicationTracing',
+    ADMIN_LOG       = 'Microsoft-Windows-AppHost/Admin';
+
 // variables
 var appTracingInitialState = null,
     appTracingCurrentState = null,
-    adminInitialState = null,
-    adminCurrentState = null,
+    adminInitialState      = null,
+    adminCurrentState      = null,
+    timers                 = [],
     appName;
 
 /*
  * Gets windows AppHost/ApplicationTracing and AppHost/Admin logs
  * and prints them to console
  */
-module.exports.run = function() {
-    getLogState('Microsoft-Windows-AppHost/Admin').then(function (state) {
+module.exports.run = function(args) {
+    var startTime = new Date(new Date().getTime() - 10 * 60 * 1000).toISOString(), // show last 10 minutes by default
+        knownOpts = { 'minutes' : Number, 'dump' : Boolean, 'help' : Boolean },
+        shortHands = { 'mins' : ['--minutes'], 'h' : ['--help'] },
+        parsedOpts = nopt(knownOpts, shortHands, args, 0);
+
+    if (parsedOpts.help) {
+        module.exports.help();
+        return;
+    }
+    if (parsedOpts.dump) {
+        if (parsedOpts.minutes) {
+            startTime = new Date(new Date().getTime() - parsedOpts.minutes * 60 * 1000).toISOString();
+        }
+        dumpLogs(startTime);
+        return;
+    }
+
+    getLogState(ADMIN_LOG).then(function (state) {
         adminInitialState = adminCurrentState = state;
-        return getLogState('Microsoft-Windows-AppHost/ApplicationTracing');
+        return getLogState(APP_TRACING_LOG);
     }).then(function (state) {
         appTracingInitialState = appTracingCurrentState = state;
         if (!adminCurrentState) {
-            return enableChannel('Microsoft-Windows-AppHost/Admin').then(function () {
-                return getLogState('Microsoft-Windows-AppHost/Admin');
+            return enableChannel(ADMIN_LOG).then(function () {
+                return getLogState(ADMIN_LOG);
             }).then(function (state) {
                 adminCurrentState = state;
             });
         }
     }).then(function () {
         if (!appTracingCurrentState) {
-            return enableChannel('Microsoft-Windows-AppHost/ApplicationTracing').then(function () {
-                return getLogState('Microsoft-Windows-AppHost/ApplicationTracing');
+            return enableChannel(APP_TRACING_LOG).then(function () {
+                return getLogState(APP_TRACING_LOG);
             }).then(function (state) {
                 appTracingCurrentState = state;
             });
@@ -74,8 +97,8 @@ module.exports.run = function() {
         }
     }).then(function () {
         console.log('Now printing logs. To stop, please press Ctrl+C once.');
-        startLogging('Microsoft-Windows-AppHost/Admin');
-        startLogging('Microsoft-Windows-AppHost/ApplicationTracing');
+        startLogging(ADMIN_LOG);
+        startLogging(APP_TRACING_LOG);
     }).catch(function (error) {
         console.error(error);
     });
@@ -97,13 +120,29 @@ module.exports.run = function() {
     });
 };
 
+module.exports.help = function() {
+    console.log();
+    console.log('Usage: ' + path.relative(process.cwd(), path.join(platformRoot, 'cordova', 'log [options]')));
+    console.log('Continuously prints your app logs to the command line.');
+    console.log();
+    console.log('Options:');
+    console.log('  --dump: Dumps logs to console instead of continuous output.');
+    console.log('  --mins <minutes>: Used only with --dump. Dumps logs starting from this much minutes back.');
+    console.log();
+    console.log('  Example: ' + path.relative(process.cwd(), path.join(platformRoot, 'cordova', 'log --dump --mins 5')));
+    process.exit(0);
+};
+
 function exitGracefully(exitCode) {
     if (appTracingInitialState === false && appTracingCurrentState === true) {
-        disableChannel('Microsoft-Windows-AppHost/ApplicationTracing');
+        disableChannel(APP_TRACING_LOG);
     }
     if (adminInitialState === false && adminCurrentState === true) {
-        disableChannel('Microsoft-Windows-AppHost/Admin');
+        disableChannel(ADMIN_LOG);
     }
+    timers.forEach(function (timer) {
+        clearInterval(timer);
+    });
     // give async call some time to execute
     console.log('Exiting in 2 seconds. Please don\'t interrupt the process.');
     setTimeout(function() {
@@ -113,26 +152,51 @@ function exitGracefully(exitCode) {
 
 function startLogging(channel) {
     var startTime = new Date().toISOString();
-    setInterval(function() {
-        var command = 'wevtutil qe ' + channel + ' /q:"*[System [(TimeCreated [@SystemTime>\'' + startTime + '\'])]]" /e:root';
-        cp.exec(command, function (error, stdout, stderr) {
-            if (error) {
-                throw new Error('Failed to run wevtutil command. ' + error);
-            } else {
-                parseEvents(stdout).forEach(function (evt) {
-                    startTime = evt.timeCreated;
-                    console.log(stringifyEvent(evt));
-                });
+    timers.push(setInterval(function() {
+        getEvents(channel, startTime).then(function(events) {
+            events.forEach(function (evt) {
+                startTime = evt.timeCreated;
+                console.log(stringifyEvent(evt));
+            });
+        });
+    }, 1000));
+}
+
+function dumpLogs(startTime) {
+    console.log('Dumping logs starting from ' + startTime);
+    var appTracingEvents, adminEvents;
+    getEvents(APP_TRACING_LOG, startTime).then(function (evts) {
+        appTracingEvents = evts;
+        return getEvents(ADMIN_LOG, startTime);
+    }).then(function(evts) {
+        adminEvents = evts;
+        appTracingEvents.concat(adminEvents)
+        .sort(function(evt1, evt2) {
+            if (evt1.timeCreated < evt2.timeCreated) {
+                return -1;
+            } else if (evt1.timeCreated > evt2.timeCreated) {
+                return 1;
             }
+            return 0;
+        })
+        .forEach(function(evt) {
+            console.log(stringifyEvent(evt));
         });
-    }, 1000);
+    });
 }
 
-module.exports.help = function() {
-    console.log('Usage: ' + path.relative(process.cwd(), path.join(platformRoot, 'cordova', 'log')));
-    console.log('Continuously prints the windows logs output to the command line.');
-    process.exit(0);
-};
+function getEvents(channel, startTime) {
+    var d = Q.defer();
+    var command = 'wevtutil qe ' + channel + ' /q:"*[System [(TimeCreated [@SystemTime>\'' + startTime + '\'])]]" /e:root';
+    cp.exec(command, function (error, stdout, stderr) {
+        if (error) {
+            d.reject('Failed to run wevtutil command. ' + error);
+        } else {
+            d.resolve(parseEvents(stdout));
+        }
+    });
+    return d.promise;
+}
 
 function getElementValue(et, element, attribute) {
     var result;
@@ -156,12 +220,12 @@ function parseEvents(output) {
 
     events.forEach(function (event) {
         // Get only informative logs
-        if ((getElementValue(event, './System/Channel') === 'Microsoft-Windows-AppHost/Admin') &&
+        if ((getElementValue(event, './System/Channel') === ADMIN_LOG) &&
             (typeof getElementValue(event, './UserData/WWAUnhandledApplicationException') === 'undefined') &&
             (typeof getElementValue(event, './UserData/WWATerminateApplication') === 'undefined')) {
             return;
         }
-        if ((getElementValue(event, './System/Channel') === 'Microsoft-Windows-AppHost/ApplicationTracing') &&
+        if ((getElementValue(event, './System/Channel') === APP_TRACING_LOG) &&
             (typeof getElementValue(event, './UserData/WWADevToolBarLog') === 'undefined')) {
             return;
         }

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/7ae75b5d/template/cordova/log
----------------------------------------------------------------------
diff --git a/template/cordova/log b/template/cordova/log
index f03da44..63876a4 100644
--- a/template/cordova/log
+++ b/template/cordova/log
@@ -24,13 +24,14 @@ var log  = require('./lib/log'),
     args = process.argv;
 
 // Usage support for when args are given
-if(args.length > 2) {
-    log.help();
-} else {
-    Q().then(function() {
-        log.run();
-    }, function(err) {
-        console.error('ERROR: ' + err);
-        process.exit(2);
-    });
-}
+Q().then(function() {
+    var argsToPass = [];
+    if (args.length > 2) {
+        argsToPass = args.slice(2);
+    }
+    log.run(argsToPass);
+}, function(err) {
+    console.error('ERROR: ' + err);
+    process.exit(2);
+});
+


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org