Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-12169] Create build log with compilation #6297

Merged
merged 8 commits into from
Nov 18, 2014
86 changes: 86 additions & 0 deletions cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ exports.config = function (logger, config, cli) {
}
}

// start file logging here
logger = patchLogger(logger, cli);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to return logger here. Logger is passed by reference.


// load the tiapp.xml
try {
var tiapp = cli.tiapp = new tiappxml(path.join(projectDir, 'tiapp.xml'));
Expand Down Expand Up @@ -253,3 +256,86 @@ exports.run = function (logger, config, cli, finished) {
}
});
};

/**
* Monkey-patch the logger object to enable file logging during build
* @param {Object} logger - The logger instance
* @param {Object} cli - The CLI instance
*/
function patchLogger(logger, cli) {
var origLoggerLog = logger.log,
platform = ti.resolvePlatform(cli.argv.platform),
logFileStream;
// create our write stream
logFileStream = fs.createWriteStream(path.join(cli.argv['project-dir'], 'build', 'build_' + platform + '.log'), { 'flags': 'w', 'encoding': 'ascii' });
// write the banner to start out the log
logFileStream.write(logBanner(cli));
// override the existing log function
logger.log = function() {
// most of this copied from the CLI's logger.js logger.log() function
var args = Array.prototype.slice.call(arguments),
padLevels = logger.padLevels,
prefix;

// if there are no args (i.e. a blank line), we need at least one space
args.length || args.unshift(' ');

// if we're not being called from info/warn/error/debug, then set this as a general log entry
args[0] in logger.levels || args.unshift('_');

// turn off padding
logger.padLevels = args[0] != '_';

// get rid of any null args
while (args.length && args[args.length-1] == null) args.pop();

// if we're logging an error, we need to cast to a string so that sprintf doesn't complain
if (args[1] instanceof Error || Object.prototype.toString.call(args[1]) == '[object Error]') {
args[1] = (args[1].stack || args[1].toString()) + '\n';
} else if (args[1] == null || args[1] == undefined) {
args[1] = '';
}

typeof type != 'string' && (args[1] = ''+args[1]);

// strip off starting full colons
args[1] = args[1].replace(/:\s{1}/, ' ');

// add [INFO] type prefixes for each line
prefix = (args[0] != "_") ? "[" + args[0].toUpperCase() + "]" + ((args[0].length===5) ? ' ' : ' ') : "";

// log it to our log file, stripping out the color codes
logFileStream.write("\n" + prefix + args[1].replace(/\x1B\[\d+m/g, ''));

// call the original logger with our cleaned up args
origLoggerLog.apply(logger, [args[0], args.length > 2 ? sprintf.apply(null, args.slice(1)) : args[1]]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line fails with:

ReferenceError: sprintf is not defined
    at logger.log (/Users/chris/Library/Application Support/Titanium/mobilesdk/osx/3.5.0/cli/commands/build.js:315:59)
    at target.(anonymous function) (/Users/chris/appc/titanium/node_modules/winston/lib/winston/common.js:45:21)
    at ioslib.simulator.launch.on.on.on.finished (/Users/chris/Library/Application Support/Titanium/mobilesdk/osx/3.5.0/iphone/cli/hooks/run.js:64:24)
    at EventEmitter.emit (events.js:95:17)
    at Tail.<anonymous> (/Users/chris/Library/Application Support/Titanium/mobilesdk/osx/3.5.0/node_modules/ioslib/lib/simulator.js:553:20)
    at Tail.emit (events.js:95:17)
    at /Users/chris/Library/Application Support/Titanium/mobilesdk/osx/3.5.0/node_modules/ioslib/node_modules/always-tail/index.js:73:32
    at wrapper (fs.js:465:17)

You forgot to add sprintf = require('sprintf') at the top, but that's not enough. The Titanium SDK does not ship with sprintf. You will need to add that to the package.json in the root of the timob repo:

    "sprintf": "~0.1.4",

Then you need to add the module to the Titanium SDK repo:

/path/to/titanium_mobile$ npm install --production

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


// restore padding
logger.padLevels = padLevels;

return logger;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to return logger here. Logger is passed by reference.

}
}

/**
* Outputs environment details at the top of the log file
* for each run of `titanium build`
* @param {Object} cli - The CLI instance
*
* See http://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history for
* os.release() to version mapping for OS X. (e.g. 14.0.0 is v10.10 Yosemite)
*/
function logBanner(cli) {
var os = require('os');
return '\n\n---------------------------------\n\n' +
new Date().toLocaleString() + '\n\n' +
'Build Environment \n' +
' Host OS = ' + (os.platform()==='darwin' ? "OS X" : os.platform()) + ' ' + os.release() + ', ' + os.arch() + '\n' +
' Target platform = ' + ti.resolvePlatform(cli.argv.platform) + '\n' +
' CLI version = ' + cli.version + '\n' +
' SDK version = ' + cli.argv.sdk + '\n' +
' SDK path = ' + cli.sdk.path + '\n' +
' Node version = ' + process.version + '\n' +
' Command = ' + cli.argv.$ + ' ' + cli.argv.$_.join(' ') + '\n' +
'\n';
}
22 changes: 11 additions & 11 deletions cli/commands/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ exports.run = function (logger, config, cli) {
cli.fireHook('clean.pre', function () {
async.series(fs.readdirSync(buildDir).map(function (dir) {
return function (next) {
var fulldir = path.join(buildDir, dir);
if (fs.lstatSync(fulldir).isDirectory()) {
cli.fireHook('clean.' + dir + '.pre', function () {
logger.debug(__('Deleting %s', fulldir.cyan));
wrench.rmdirSyncRecursive(fulldir);
cli.fireHook('clean.' + dir + '.post', function () {
next();
});
var file = path.join(buildDir, dir);
cli.fireHook('clean.' + dir + '.pre', function () {
logger.debug(__('Deleting %s', file.cyan));
if (fs.lstatSync(file).isDirectory()) {
wrench.rmdirSyncRecursive(file);
} else {
fs.unlinkSync(file);
}
cli.fireHook('clean.' + dir + '.post', function () {
next();
});
} else {
next();
}
});
};
}), function () {
cli.fireHook('clean.post', function () {
Expand Down