Search Unity

[Free]Log - Compact way to log name and value of variable to console

Discussion in 'Scripting' started by SisusCo, Dec 1, 2019.

  1. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    The Problem

    Printing the name and value of a variable to the console is one of those basic tasks that you have to do time and time again in most projects.

    It feels redundant that every time you want do this you need to type the name of the variable twice: once to make the name show up in the message, and then a second time for the value.

    Code (CSharp):
    1. Debug.Log("myObjectInstance="+myObjectInstance);
    If you want to avoid having to go edit your code if the name of the variable gets changed, then you should use the nameof operator, which adds a couple more characters for you to type.

    Code (CSharp):
    1. Debug.Log(nameof(myObjectInstance)+"="+myObjectInstance);
    Additionally, you very often need to write the same boilerplate code for handling the situation where the variable value is null.

    Code (CSharp):
    1. Debug.Log(nameof(myObjectInstance)+"="+(myObjectInstance == null ? "null" : myObjectInstance.ToString()));
    By using the ?? operator you can shorten this a little bit.

    Code (CSharp):
    1. Debug.Log(nameof(myObjectInstance)+"="+(myObjectInstance ?? (object)"null"));
    But, that's still a lot of code to do a simple thing, and it's pretty difficult to read. Plus you might sometimes forget to add those null checks, and might end up getting a visit from your old friend NullReferenceException.

    When you need to print the values of multiple variables with one Debug.Log call, these problems just exacerbate. It also starts getting really easy to miss a parenthesis, quotation mark or a plus sign somewhere amid all that.

    The Solution: Log

    I'd like to share a little utility class I wrote that makes it easier to print out the names and values of your variables. It's called "Log". While the name might not be the most imaginative, it gives you the ability to condense the code shown earlier to just this:

    Code (CSharp):
    1. Log.Value(()=>myObjectInstance);
    Example output: "myObjectInstance=null".

    You can include multiple variables in one messages like this:

    Code (CSharp):
    1. Log.Value(()=>myBool, ()=>myInt);
    Example output: "myBool=True, myInt=100".

    It is also possible to log the values of all class members of a target with just this:

    Code (CSharp):
    1. Log.State(this);
    Example output: "MyClass state: arrayField=[1,2,3], BoolProperty=True".

    Even static class members can be logged easily:

    Code (CSharp):
    1. Log.State(typeof(UnityEngine.Time));
    Example output:
    "Time state:
    time=67.29188
    timeSinceLevelLoad=67.29188
    deltaTime=0.3333333
    ...
    "

    Double-Clicking a Console Message

    In addition to the C# script file, I have also included a DLL file that contains the Log class inside it. If you choose to use the DLL in your project, you can double-click a console message printed by the Log class, and it will jump to the point in your code where you are calling the Log class (instead of jumping inside the Log class itself, which isn't as helpful).

    If instead you choose to use the C# script file, the same thing does not happen by default. It is however possible to enable the same behaviour in this case too, with the help of some add-ons available from asset store, such as Console Enhanced Pro(*) or Editor Console Pro(*).

    (*) If you purchase one of these assets through these links I receive a commission. This will help support me in the development of my own assets.

    Download Link

    You can get Log from here. Feel free to use in whatever projects you'd like.

    I hope you will find this useful. Cheers!
     
    Last edited: Feb 11, 2020