- Home /
Capturing log messages
I tried to capture the log output using Application.RegisterLogCallback but it did not work:
int logcalled ;
// Use this for initialization void OnEnable() { UnityEngine.Application.RegisterLogCallback(new Application.LogCallback(MyCallback)); }
private void MyCallback(string condition, string stacktrace, UnityEngine.LogType type) { logcalled++; }
void OnGUI(){ GUI.Label(new Rect(10,10,100,100), logcalled.ToString());
if(GUI.Button(new Rect(100,100,100,100),"press"))
{
Debug.LogError("iada iada");
Debug.Log("iada iada 2");
object a = null;
a.ToString();
}
}
The above code dont work, the variable logcalled not incremented.
- Tried Debug.Log, Debug.LogError and a even a null exception, neither was captured, but they where all logged to the editor console.
- Tried to register the callback on "Start" with the same results.
- Tried a Debug.Log on the "Update" method, generating dozens of log msg/second. In this case 'logcalled' was called 2 times at the very begining and that was it.
- Tried using javascript instead of C#, didnt work either.
Am I missing something?
(question was edited to clarify it)
If your original purpose is to view logs on devices, I recommend you use plugin "Device Console".
It's quite good.
Details: https://www.assetstore.unity3d.com/#!/content/44935
Answer by Tetrad · Mar 02, 2010 at 05:34 PM
There seems to be a bug with this.
The only way I could get RegisterLogCallback to work with our in-game logging system was by calling RegisterLogCallback every frame. If I just called it at startup, it would only call the logs that were in that initial frame.
It seems from my testing that you can't register it until at least two frames after starting. You don't need to call it every frame, since once it "takes," it's set.
Indeed! After some tests, registering on the third frame solve it. But wtf, and if in the next update this changes to the fourht frame? Just kidding of course, its just to strange to be confortable, but whatever, works for now.
The issue apparently was fixed at some point, in 3.4.2 a callback registered in the very first frame works fine.
Unity seems to support only 1 LogCallback at a time. There's probably another place in your code (or in a plugin) that overwrite your registration. Calling it everyframe ensures your callback is always the latest registered.
Answer by jonas-echterhoff · Feb 15, 2010 at 09:09 AM
RegisterLogCallback should not redirect the log output - it should install an additional handler, so the log output goes to the editor log, and your function should be called. Are you seeing logcalled being incremented as log messages are printed? If so, then everything works as expected.
Thats the problem, 'logcalled' is not being incremented!
The example on the unity site uses a Javascript, could this be relevant?
Answer by Fernando Zapata 1 · Jun 18, 2010 at 11:57 PM
The JavaScript example in Unity's documentation also does not work (unless you use the delayed register workaround) . Has anyone logged a bug report?