playMaker

Author Topic: Logging output to string  (Read 3673 times)

artician

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 318
Logging output to string
« on: March 06, 2013, 04:01:24 PM »
This is probably a simple problem, but I want to get whatever the debug/console/log spits out and store it in a string variable.  My intention is just to see what errors are being produced when I'm running a web build.

Thanks for any help.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Logging output to string
« Reply #1 on: March 11, 2013, 01:07:27 AM »
Actually there isn't a super easy way to do this with Unity log commands (that I know of anyway).

I assume you want something like this:
http://forum.unity3d.com/threads/108400-Isn-t-there-no-way-to-show-debug-console-in-standalone-build

This might also be useful:
http://answers.unity3d.com/questions/18134/debug-console-in-standalone.html

artician

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 318
Re: Logging output to string
« Reply #2 on: March 20, 2013, 12:49:35 PM »
From the first link, someone shared the perfect script:
http://zaxisgames.blogspot.com/2012/03/let-me-debug-in-your-gui.html

Which is exactly what I needed, and I thought I'd share it here.

Thank you for the help!

(The script itself)::
Code: [Select]
using UnityEngine;
using System.Collections;
 
public class MyLog : MonoBehaviour
{
    static string myLog;
    static Queue myLogQueue = new Queue();
    public string output = "";
    public string stack = "";
    private bool hidden = true;
    private Vector2 scrollPos;
    public int maxLines = 30;
 
    void OnEnable()
    {
        Application.RegisterLogCallback(HandleLog);
    }
 
    void OnDisable()
    {
        Application.RegisterLogCallback(null);
    }
 
    void HandleLog(string logString, string stackTrace, LogType type)
    {
        output = logString;
        stack = stackTrace;
        string newString = "\n [" + type + "] : " + output;
        myLogQueue.Enqueue(newString);
        if (type == LogType.Exception)
        {
            newString = "\n" + stackTrace;
            myLogQueue.Enqueue(newString);
        }
 
        while (myLogQueue.Count > maxLines)
        {
            myLogQueue.Dequeue();
        }
 
        myLog = string.Empty;
        foreach (string s in myLogQueue)
        {
            myLog += s;
        }
    }
 
    void OnGUI()
    {
        if (!hidden)
        {
            GUI.TextArea(new Rect(0, 0, Screen.width / 3, Screen.height), myLog);
            if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Hide"))
            {
                hide(true);
            }
        }
        else
        {
            if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Show"))
            {
                hide(false);
            }
        }
    }
 
    public void hide(bool shouldHide)
    {
        hidden = shouldHide;
    }
}

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Logging output to string
« Reply #3 on: March 20, 2013, 01:05:00 PM »
A word of caution: Apparently Unity only allows one log callback to register at a time, and Playmaker already registers one. So does Script Inspector+, and UnityVS... so this might not be reliable with multiple scripts fighting for access to the log :/ But I also haven't done a lot of testing of this yet... so I'm curious to hear your results...

artician

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 318
Re: Logging output to string
« Reply #4 on: March 20, 2013, 01:15:14 PM »
Good to know.  For my own curiosity: Does playmaker make log callbacks even on standalone builds?  How about when Enable Logging is disabled in the Debugging Preferences?

I have not changed PM preferences or tried this script with different settings, but I have 'Enable Logging' enabled, and 'Forward Playmaker Log to Unity log' disabled.  In the week of use I've had with it, it's worked as expected in both the editor and standalone builds.  If there's any interference I definitely haven't noticed it.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Logging output to string
« Reply #5 on: March 21, 2013, 12:50:49 AM »
Right now Playmaker only uses the log callback in the editor. It snoops on the log to try and intercept runtime errors so it can display them inline with the actions that might have caused them, to make it easier to debug FSMs... It shouldn't effect standalone builds at all. In fact, for some reason I didn't think it would work in standalone builds, which is why I didn't suggest it when you first asked...

Enable Logging refers to Playmaker logging which is separate.

So you're probably fine :)