pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://docs.unity3d.com/ScriptReference/../Manual/low-level-native-plugin-logging.html

ataLayer || []; dataLayer.push({ event: 'dataLayer-initialized', user: { user_unity_id: undefined, user_logged_in: 'no' }, environment: { environment_locale: 'en-us', environment_currency: undefined }}); Unity - Manual: Native plug-in API for logging
Version: Unity 6.5 (6000.5)
Language : English
Native plug-in API for profiling
Code optimization

Native plug-in API for logging

Write to the Unity logs from your native plug-inA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary
code using the IUnityLog interface. Low-level native APIs for logging are provided in the IUnityLog.h header file, located in the PluginAPI folder.

The file contains a single Log function with the following decalaration:

void(UNITY_INTERFACE_API * Log)(UnityLogType type, const char* message, const char *fileName, const int fileLine);

You can call this function directly as follows:

s_UnityLog->Log(kUnityLogTypeLog, "Here is a regular log", __FILE__, __LINE__);

However, for convenience the native logging API defines the following macros that wrap different log-level calls to the Log function:

Macro Description
UNITY_LOG(PTR_, MSG_) Uses the log interface passed as a pointer (PTR_) to write the provided char* MSG_ as a regular log message. Equivalent to the managed API Debug.Log.
UNITY_LOG_WARNING(PTR_, MSG_) Uses the log interface passed as a pointer (PTR_) to write the provided char* MSG_ as a warning-level log message. Equivalent to the managed API Debug.LogWarning.
UNITY_LOG_ERROR(PTR_, MSG_) Uses the log interface passed as a pointer (PTR_) to write the provided char* MSG_ as an error-level log message. Equivalent to the managed API Debug.LogError.

The following code example implements the IUnityLog interface in C++ and uses these predefined macros to write different levels of log output:

#include "IUnityLog.h"

static IUnityLog* s_UnityLog = NULL;

// Additional macros to include file and line number from the native code
#define UNITY_LOG_STRINGIZE_DETAIL(x) #x
#define UNITY_LOG_STRINGIZE(x) UNITY_LOG_STRINGIZE_DETAIL(x)
#define COMPOSE(MESSAGE) "[" __FILE__ ":" UNITY_LOG_STRINGIZE(__LINE__) "] " MESSAGE
#define NATIVE_LOG(PTR, MESSAGE) UNITY_LOG(PTR, COMPOSE(MESSAGE))
#define NATIVE_WARNING(PTR, MESSAGE) UNITY_LOG_WARNING(PTR, COMPOSE(MESSAGE))
#define NATIVE_ERROR(PTR, MESSAGE) UNITY_LOG_ERROR(PTR, COMPOSE(MESSAGE))

// Unity plugin load event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces * unityInterfacesPtr)
{
    s_UnityLog = unityInterfacesPtr->Get<IUnityLog>();
}

// Unity plugin unload event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
    s_UnityLog = nullptr;
}

extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GenerateLog()
{
    // Output different log level messages to the Unity console
    UNITY_LOG(s_UnityLog, "Regular log message");
    UNITY_LOG_WARNING(s_UnityLog, "Warning log message");
    UNITY_LOG_ERROR(s_UnityLog, "Error log message");

    // Wrap log functions to provide native file and line number in output
    NATIVE_LOG(s_UnityLog, "Regular log with native file name and line number");
    NATIVE_WARNING(s_UnityLog, "Warning log with native file name and line number");
    NATIVE_ERROR(s_UnityLog, "Error log with native file name and line number");
}

Note: The additional macros to embed the native file and line number in the message are a workaround for a known issue in which Unity fails to include the information when the Log function is invoked via UNITY_LOG and equivalents.

Additional resources

Native plug-in API for profiling
Code optimization
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy