Welcome, Guest

Author Topic: Adding Javascript errors to the normal console log.  (Read 1443 times)

Adding Javascript errors to the normal console log.
« on: October 08, 2014, 06:42:50 AM »
I'd like to divert the error log that you can capture inside the coherent debugger to the normal unity output_log to make it easier to find issues that are occuring. Is this something that is possible perhaps from just a javascript level via a binded call?

Nick

  • Administrator
  • *****
  • Posts: 475
    • View Profile
Re: Adding Javascript errors to the normal console log.
« Reply #1 on: October 08, 2014, 07:38:55 AM »
Adding another event handler for console.log seems to be the easiest to me. Here's some sample code:

test.html
Code: [Select]
<html>
<head>
<title>Binding Sample</title>
</head>
<body>
<script type="text/javascript" src="javascript/coherent.js"></script>
<script type="text/javascript">

var counter = 0;
function logSomething() {
console.log("Logging something... " + ++counter);
}

engine.on('Ready', function () {
var originalConsoleLogFunc = console.log.bind(console); // Save original function

console.log = function(msg) {
originalConsoleLogFunc(msg);
engine.trigger('JSLog', msg);
}
});

</script>

<button id='logSomething' onclick="logSomething()">Log Something</button>

</body>
</html>

ConsoleLogBinder.cs
Code: [Select]
using UnityEngine;
using System.Collections;

public class ConsoleLogBinder : MonoBehaviour {

// Use this for initialization
void Start () {
CoherentUIView view = GetComponent<CoherentUIView> ();
view.OnReadyForBindings += (frameId, path, isMainFrame) => {
if (isMainFrame) {
view.View.RegisterForEvent("JSLog", (System.Action<string>)ConsoleLogToDebugLog);
}
};
}

void ConsoleLogToDebugLog(string msg) {
Debug.Log (msg);
}
}
R&D Director @CoherentLabs

Re: Adding Javascript errors to the normal console log.
« Reply #2 on: October 08, 2014, 07:51:02 AM »
That partly got it working, I also needed to add a window.onerror event to capture actual errors, but its all good now, I'm seeing both errors and logs showing up in unity :)

Tags: