From f67eb6dd25fabb5982b6f7db936a2cd5f6fbd9fb Mon Sep 17 00:00:00 2001 From: Simo Kinnunen Date: Tue, 7 Jan 2014 10:55:41 +0900 Subject: [PATCH] Minimal start with a custom logger and device tracker. --- .editorconfig | 11 +++++++++ .gitignore | 1 + bin/stf | 2 ++ lib/cli.js | 15 ++++++++++++ lib/provider.js | 26 ++++++++++++++++++++ lib/util/logger.js | 59 +++++++++++++++++++++++++++++++++++++++++++++ test/util/logger.js | 12 +++++++++ 7 files changed, 126 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100755 bin/stf create mode 100644 lib/cli.js create mode 100644 lib/provider.js create mode 100644 lib/util/logger.js create mode 100644 test/util/logger.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..9912b2af --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# http://editorconfig.org/ + +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2ccbe465 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/bin/stf b/bin/stf new file mode 100755 index 00000000..fa67b84f --- /dev/null +++ b/bin/stf @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('../lib/cli') diff --git a/lib/cli.js b/lib/cli.js new file mode 100644 index 00000000..81757cee --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,15 @@ +var program = require('commander') + +var pkg = require('../package') + +program + .version(pkg.version) + +program + .command('provider') + .description('run STF provider') + .action(function() { + require('./provider') + }) + +program.parse(process.argv) diff --git a/lib/provider.js b/lib/provider.js new file mode 100644 index 00000000..7144932e --- /dev/null +++ b/lib/provider.js @@ -0,0 +1,26 @@ +var adb = require('adbkit') +var async = require('async') + +var log = require('./util/logger').createLogger('provider') +var client = adb.createClient() + +client.trackDevices(function(err, tracker) { + if (err) { + log.fatal('Unable to track devices: %s', err.message) + throw err + } + + log.info('Tracking devices') + + tracker.on('add', function(device) { + log.info('Found device "%s" (%s)', device.id, device.type) + }) + + tracker.on('change', function(device) { + log.info('Device "%s" is now "%s"', device.id, device.type) + }) + + tracker.on('remove', function(device) { + log.info('Lost device "%s" (%s)', device.id, device.type) + }) +}) diff --git a/lib/util/logger.js b/lib/util/logger.js new file mode 100644 index 00000000..aad83ff7 --- /dev/null +++ b/lib/util/logger.js @@ -0,0 +1,59 @@ +var util = require('util') + +function Log(tag, stream) { + this.tag = tag + this.stream = stream || process.stderr + this.levels = { + DEBUG: 'DBG' + , VERBOSE: 'VRB' + , INFO: 'INF' + , WARNING: 'WRN' + , ERROR: 'ERR' + , FATAL: 'FTL' + } +} + +Log.prototype.debug = function() { + this._write(this._format(this.levels.DEBUG, arguments)) +} + +Log.prototype.verbose = function() { + this._write(this._format(this.levels.VERBOSE, arguments)) +} + +Log.prototype.info = function() { + this._write(this._format(this.levels.INFO, arguments)) +} + +Log.prototype.warn = function() { + this._write(this._format(this.levels.WARNING, arguments)) +} + +Log.prototype.error = function() { + this._write(this._format(this.levels.ERROR, arguments)) +} + +Log.prototype.fatal = function() { + this._write(this._format(this.levels.FATAL, arguments)) +} + +Log.prototype._format = function(priority, args) { + return util.format('%s %s/%s %d %s\n', + Log.prefix, priority, this.tag, process.pid, util.format.apply(util, args)) +} + +Log.prototype._write = function(out) { + this.stream.write(out) +} + +Log.prefix = '*' + +Log.createLogger = function(tag) { + return new Log(tag) +} + +Log.setPrefix = function(prefix) { + Log.prefix = prefix +} + +exports = module.exports = Log diff --git a/test/util/logger.js b/test/util/logger.js new file mode 100644 index 00000000..4afca5d3 --- /dev/null +++ b/test/util/logger.js @@ -0,0 +1,12 @@ +var chai = require('chai') +var expect = chai.expect + +var logger = require('../../lib/util/logger') + +describe('Logger', function() { + + it('should have a createLogger method', function() { + expect(logger).itself.to.respondTo('createLogger') + }) + +})