Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Log to console from C++ DLL

Discussion in 'Scripting' started by dershver_unity, Aug 17, 2020.

  1. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8
    I have a C++ DLL and I would like to Log to the Unity Console when in Play mode.
    Is the a way to display text to the Unity console from C++ ? Can I just use 'std::cout' ?
    This is not working for me. Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I don't think so.

    I just pass strings back from my native C functions and then output it via Unity. You could just accumulate all the strings in C++ side and then have another function that returns those strings to Unity.
     
    Joe-Censored and PraetorBlue like this.
  3. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Kurt-Dekker and qoobit like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Interesting! Last time I checked you had to wait until the next frame to send stuff back... what you posted looks like it might happen immediately. Cool.
     
    Zer0Cool likes this.
  5. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I assume its a direct callback to this C# class and a static class method (in memory) and therefore its frame independent, but runs on the main thread so I would use it sparingly or only in a developer build.
     
    Kurt-Dekker likes this.
  6. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8
    1. Thanks. I will try both of these suggestions.
     
  7. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
  8. darkgrouptw

    darkgrouptw

    Joined:
    Mar 4, 2015
    Posts:
    2
    Try this github.
    https://github.com/Darkgrouptw/UnityNativeLogger

    You need to include Native api from PluginAPI (e.g. %UNITY_LOCATION%\Editor\Data\PluginAPI)
    In your cpp:

    static IUnityLog* mLogger;

    extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SendDataFromUnityToLog(float t)
    {
    // Call it using the define below
    string content = "Native log: " + to_string(t);
    const char* c_content = content.c_str();
    UNITY_LOG(mLogger, c_content);
    UNITY_LOG_WARNING(mLogger, c_content);
    UNITY_LOG_ERROR(mLogger, c_content);
    }

    // This will be called when this plugin is loaded
    extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces * pInterfaces)
    {
    // Store this IUnityLog
    mLogger = pInterfaces->Get<IUnityLog>();
    UNITY_LOG(mLogger, "Native Plugin load");
    }
    extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
    {
    UNITY_LOG(mLogger, "Native Plugin unload");
    mLogger = nullptr;
    }


    In your cs file
    Code (CSharp):
    1.  
    2.     [DllImport("NativePlugin")]
    3.     private static extern void SendDataFromUnityToLog(float _);
    4.  
    5.     private void Start()
    6.     {
    7.         UnityEngine.Random.InitState(System.DateTime.Now.Second);
    8.         float randNum = UnityEngine.Random.value;
    9.         Debug.Log("Unity Log: " + randNum.ToString());
    10.  
    11.         // Trigger the UnityPluginLoad(c++) once you call this function
    12.         SendDataFromUnityToLog(randNum);
    13.     }
     
    Last edited: Jun 29, 2023