Sunday, 30 May 2010

Logging Errors and Information

So, we've established that it's a JavaScript environment, so when changing it'd be nice to see any error messages. At Panasonic they presumably have development kit that has a "console", so they can see error messages. My telly didn't come with that strangely!

I haven't yet figured out a way of capturing all JavaScript errors, but I have got a way of logging some information so at least you can track progress through the code and output debugging information.

Firstly, take a copy of home-screen.js to modify and add the following at the start: -

console.log = function (msg) {

http_request({ url: "http://X.X.X.X/log.php?message=" + msg,
method: "GET",
sync: false,


replace X.X.X.X with the IP address of your Apache server.

Now find the line that reads "var level = " and change the number to 4.

Below this line you will see function definitions for fatal, error, warn and info. These are called within the script, which call console.log, which we've just overridden to perform a HTTP get from a (yet non-existent) PHP file on our web server. I think it's a good idea to duplicate one of these, call it something unique (mydebug for example) and change what it outputs to make it easier to find your debug messages in the code.

Once you've done this, you need to create /var/www/log.php containing the following: -

echo $_REQUEST['message'];
$file = fopen("/var/logs/console.log","a");
fwrite($file,strftime('%c') . " : " . $_REQUEST['message'] . "\n");

Now, whenever your script calls console.log, it'll be overridden to request log.php from the webserver, passing the message as a parameter. The log.php will then write any output to console.log. Test this by browsing to http://yourserver/log.php?message=test and then to http://yourserver/console.log

You will now see all the informational messages that panasonic have added, and can also (assuming you created a function) call mydebug (or whatever) with your own progress and informational messages.

If anyone figures out a way to somehow override and capture syntax or runtime errors in a similar way I'd love to know! For now debugging basically includes adding mydebug() calls all over the place so I know how far through the code it's got!

No comments:

Post a Comment