playMaker

Author Topic: Custom action for filtering Unity logging in builds  (Read 1545 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
Custom action for filtering Unity logging in builds
« on: May 08, 2022, 02:56:28 PM »
I have typed this script that handles Unity's logging and blocks it. It's different than Playmaker's own logging which can also be blocked.

Code: [Select]
using UnityEngine;
using System.Collections;
//https://docs.unity3d.com/ScriptReference/Logger-logEnabled.html https://docs.unity3d.com/ScriptReference/Debug-isDebugBuild.html
//code by Broken Stylus (2022)

namespace HutongGames.PlayMaker.Actions
{

[ActionCategory(ActionCategory.Debug)]
[Tooltip("Enable or disable logging at runtime. This only affects the Unity Console, not Playmaker's Fsm Log. Can be set to depend on the option 'Development Build' in Build Settings.")]
public class EnableBuildLogging : FsmStateAction
{

        [Title("Enable logging")]
        [Tooltip("Allow the build to send logs.")]
        public FsmBool logEnabled;

        [Title("Development build only")]
        [Tooltip("Set to TRUE to allow logging only if the build is in development mode. Will not matter if no logging is enabled anyway.")]
        public FsmBool devBuildOnly;

        private static ILogger logger = Debug.unityLogger;

        public override void Reset()
        {
            logEnabled = false;
            devBuildOnly = true;
        }

        public override void OnEnter()
{
            if(logEnabled.Value)
            {
                if (devBuildOnly.Value && !Debug.isDebugBuild) // verifies that the logging must only happen in Development Build mode, and checks if the build is in release mode (not in dev mode), in which case the logging is not allowed and the process is aborted. All other cases in this area of the code will allow the logging.
                {
                    Finish();
                }
                logger.logEnabled = logEnabled.Value;
            }
            Finish();
        }
}
}

But I'm not sure it's working properly. The idea is to render Unity totally mute. I added a condition, up to the user, to restrain the logging to a development build.
Does this action look good?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Custom action for filtering Unity logging in builds
« Reply #1 on: May 08, 2022, 11:11:19 PM »
Hi.
if logEnable is set to false, it will do nothing.

Here's the way i would set it up :)

Code: [Select]
using UnityEngine;
using System.Collections;
//https://docs.unity3d.com/ScriptReference/Logger-logEnabled.html https://docs.unity3d.com/ScriptReference/Debug-isDebugBuild.html
//code by Broken Stylus (2022)

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory(ActionCategory.Debug)]
    [Tooltip("Enable or disable logging at runtime. This only affects the Unity Console, not Playmaker's Fsm Log. Can be set to depend on the option 'Development Build' in Build Settings.")]
    public class EnableBuildLogging : FsmStateAction
    {

        [Title("Enable logging")]
        [Tooltip("Allow the build to send logs.")]
        public FsmBool logEnabled;

        [Title("Development build only")]
        [Tooltip("Set to TRUE to allow logging only if the build is in development mode. Will not matter if no logging is enabled anyway.")]
        public FsmBool devBuildOnly;

        private static ILogger logger = Debug.unityLogger;

        public override void Reset()
        {
            logEnabled = false;
            devBuildOnly = true;
        }

        public override void OnEnter()
        {
            if (logEnabled.Value)
            {
                if (devBuildOnly.Value) // verifies that the logging must only happen in Development Build mode, and checks if the build is in release mode (not in dev mode), in which case the logging is not allowed and the process is aborted. All other cases in this area of the code will allow the logging.
                {
                    if (Debug.isDebugBuild)
                    {
                        // set log Enabled true
                        logger.logEnabled = logEnabled.Value;
                    }
                }
                else
                {
                    // set log Enabled true
                    logger.logEnabled = logEnabled.Value;
                }
            }
            else
            {
                // set log Enabled false
                logger.logEnabled = logEnabled.Value;
            }
            Finish();
        }
    }
}

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
Re: Custom action for filtering Unity logging in builds
« Reply #2 on: May 13, 2022, 05:46:54 AM »
Thanks for the input. I'll check this one.
I wonder if it's also possible to access some of Playmaker's preferences from within the actions' environment. That would be a bit meta to PM I suppose.