Last active
January 27, 2021 00:51
-
-
Save justindarc/00c9e0c2bf67cea4f412 to your computer and use it in GitHub Desktop.
Firefox OS Layer Tree Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Firefox OS Layer Tree Utility | |
* | |
* Captures layer tree dumps from `adb logcat` and | |
* outputs them to the console without log noise. | |
* | |
* Usage: | |
* | |
* $ ./layertree.js [number_of_layer_trees] | |
*/ | |
var spawn = require('child_process').spawn; | |
// Wait for `adb` server and device in a separate process | |
console.log('Waiting for device...\n'); | |
spawn('adb', ['wait-for-device'], { detached: true }).on('close', function(code) { | |
// Regular expression for finding layer tree dump | |
var delimiter = /^I\/Gecko\s+\(.+\)\:\sLayerManager\s\(.+\)/g; | |
// Current layer tree dump | |
var tree = null; | |
// Track number of layer tree dumps | |
var count = process.argv[2] || -1; | |
// Start `adb logcat` | |
var logcat = spawn('adb', ['logcat']); | |
// Handle `adb logcat` output data | |
logcat.stdout.on('data', function(data) { | |
var lines = data.toString().split('\n'); | |
lines.forEach(function(line) { | |
if (line.indexOf('I/Gecko') !== 0) { | |
return; | |
} | |
// Start layer tree capture | |
if (!tree && delimiter.exec(line)) { | |
tree = [line]; | |
return; | |
} | |
// Continue capturing layer tree | |
if (tree) { | |
// Dump layer tree to console | |
if (delimiter.exec(line)) { | |
console.log('******** BEGIN LAYER TREE ********'); | |
console.log(tree.join('\n')); | |
console.log('******** END LAYER TREE ********\n\n'); | |
// Terminate if the layer tree dump count is met | |
if (--count === 0) { | |
logcat.kill(); | |
process.exit(); | |
} | |
// Reset layer tree capture | |
tree = [line]; | |
return; | |
} | |
// Add to current layer tree dump | |
tree.push(line); | |
} | |
}); | |
}); | |
// Handle `adb logcat` termination | |
logcat.on('close', function(code) { | |
console.log('[adb process exited with code ' + code + ']'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment