I think this will be helpful to others.
Intro
- Building from
ti build
or Titanium Studio starts the logs.. - I will often close the app on the simulator make an js file change then reopen the app on the simulator (which will usually pick up the new change).
Problem
Closing & reopening the app on the simulator breaks the stdn link for the build in logger.py.
Solution
This script will separate the log tailing from the app execution. You can close the app & restart it and the tail -f *.log
will just pick off where it was last
log (php)#!/usr/bin/env php
<?php
$path = '~/Library/Application\ Support/iPhone\ Simulator';
$sdk = $argv[1] ?: '7.0.3';
$ti = new SimpleXMLElement(file_get_contents('tiapp.xml'));
$ti->registerXPathNamespace('ti', 'http://ti.appcelerator.org');
$ti = $ti->xpath('//ti:app');
$app_name = (string)$ti[0]->name;
$path = "$path/$sdk/Applications";
$path = str_replace(' ', '\ ',
dirname(`find $path -name $app_name.app`)
);
$path = "$path/Documents/*.log";
passthru("tail -f $path | ./logger");
logger (php)#!/usr/bin/env php
<?php
//maps the different flag levels to a colors
//TODO make $flags a constant
function map($flag){
$flags = array(
'ERROR' => "31;01m", //red
'INFO' => "32;01m", //green
'WARNING' => "33;01m", //yellow
'DEBUG' => "34;01m", //blue
);
return $flags[$flag];
}
// $COL_MAGENTA=$ESC_SEQ"35;01m"
// $COL_CYAN=$ESC_SEQ"36;01m"
//format the colored text
function color($flag, $txt){
$col = map($flag);
$esc = "\x1b[";
$reset = $esc."39;49;00m";
print($esc.$col.$flag.$reset.$txt);
}
//check for flag
$rgx = '/\[(\w+)\](.+)$/';
$formatter = function($m){
$flag = $m[1];
$text = $m[2];
color( $flag, "\n$text\n\n" );
};
//stay open while stdin is going..
while($f = fgets(STDIN)){
preg_replace_callback($rgx, $formatter, $f);
}
Result
Side Note
It wouldn't be too hard to add a node command to titanium like watch
to hit logger.py
I'm not a python developer so I made these two scripts (really quickly. Feal free to give me improvements).