Home > Uncategorized > Console.log – Remote

Console.log – Remote

If you have a PhoneGap/Cordova App, then you surely use Console.log statements to help with the debugging of your app while it is running in a simulator, or USB-attached device.

However, if your device cannot be attached to your development machine, or you cannot debug in the normal way, then this simple script lets you see Console.log output on a web browser.

Firstly, you’ll need access to a windows web-server, and upload this script:

<script language="C#" runat="server">

public static System.Collections.Generic.List<LogItem> logList = new 
System.Collections.Generic.List<LogItem>();

protected void Page_Load(object sender, EventArgs e)
{
string strText = Request.QueryString["text"];
if (strText != null)
{
logList.Add(new LogItem(strText));
return;
}
Response.Write("Log Running...<hr>");
Response.Flush();
DateTime startTime = DateTime.UtcNow;

while(DateTime.UtcNow - startTime < TimeSpan.FromMinutes(1))
{

foreach(LogItem logItem in logList)
{
if (!logItem.isRead)
{
Response.Write(logItem.text + "<br>");
Response.Flush();
logItem.isRead = true;
}
}
System.Threading.Thread.Sleep(100);
}
Response.Redirect(Request.RawUrl);
}

public class LogItem
{
public LogItem(string text)
{
this.text = text;
}

public string text = "";

public bool isRead = false;
}
</script>

Let’s imagine that you put this script at http://www.yourserver.com/log.aspx

Then, you need to override the default action of console.log with this piece of javascript (Which requires JQuery)

console = {};
console.log = function(text)
{
$.get(“http://www.yourserver.com/log.aspx?text=&#8221; + encodeURIComponent(text) + “&nocache=” + Math.random());
}

This, then means that instead of writing to the console output, it makes an AJAX call to the logging script. Visiting the logging script webpage will display the text passed to console.log.

There are some limitations, such that it is only suitable for one-app-one-developer type environments, and you have to remember to remove the override for console.log before submitting the app to Google / Apple / BlackBerry …

Advertisement
Categories: Uncategorized
  1. November 13, 2013 at 9:10 am

    **IMPORTANT**
    Overriding console.log will crash a webworks app running on a real device, but not ripple

    Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: