Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ room.RegisterByteStreamHandler("my-topic", (reader, identity) =>
);
```

## Verbose Logging

To enable verbose logging, define the `LK_VERBOSE` symbol:

1. Navigate to Project Settings → Player
2. Select your platform tab (e.g., Mac, iOS, Android).
3. Under Other Settings → Scripting Define Symbols, add `LK_VERBOSE`.

<!--BEGIN_REPO_NAV-->
<br/><table>
<thead><tr><th colspan="2">LiveKit Ecosystem</th></tr></thead>
Expand Down
7 changes: 2 additions & 5 deletions Runtime/Scripts/Internal/FFIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ private static void InitializeSdk()
return;
#endif

#if LK_VERBOSE
// TODO: gate behind LK_VERBOSE
const bool captureLogs = true;
#else
const bool captureLogs = false;
#endif

NativeMethods.LiveKitInitialize(FFICallback, captureLogs, "unity", ""); // TODO: Get SDK version

Expand Down Expand Up @@ -218,7 +215,6 @@ out UIntPtr dataLen
}
}


[AOT.MonoPInvokeCallback(typeof(FFICallbackDelegate))]
static unsafe void FFICallback(UIntPtr data, UIntPtr size)
{
Expand All @@ -243,6 +239,7 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size)
switch (r?.MessageCase)
{
case FfiEvent.MessageOneofCase.Logs:
Utils.HandleLogBatch(r.Logs);
break;
case FfiEvent.MessageOneofCase.PublishData:
break;
Expand Down
83 changes: 79 additions & 4 deletions Runtime/Scripts/Internal/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,102 @@
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using LiveKit.Proto;
using System.Collections.Generic;

namespace LiveKit.Internal
{
/// <summary>
/// The class <c>Utils</c> contains internal utilities used for FfiClient
/// The class <c>Utils</c> contains internal utilities used for FfiClient
/// The log part is useful to print messages only when "LK_DEBUG" is defined.
/// </summary>
internal static class Utils
{
private const string PREFIX = "LiveKit";
private const string LK_DEBUG = "LK_DEBUG";

/// <summary>
/// Log a message at the info level.
/// </summary>
public static void Info(object msg)
{
UnityEngine.Debug.unityLogger.Log(PREFIX, msg);
}

/// <summary>
/// Log a message at the error level.
/// </summary>
public static void Error(object msg)
{
UnityEngine.Debug.unityLogger.LogError(PREFIX, msg);
}

/// <summary>
/// Log a message at the warning level.
/// </summary>
public static void Warning(object msg)
{
UnityEngine.Debug.unityLogger.LogWarning(PREFIX, msg);
}

/// <summary>
/// Log a message at the debug level.
/// </summary>
[Conditional(LK_DEBUG)]
public static void Debug(object msg)
{
UnityEngine.Debug.unityLogger.Log(LogType.Log, $"{PREFIX}: {msg}");
UnityEngine.Debug.unityLogger.Log(PREFIX, msg);
}

public static void Error(object msg)
// <summary>
/// Forwards a log batch received over FFI to the Unity logging system.
/// </summary>
internal static void HandleLogBatch(LogBatch batch)
{
UnityEngine.Debug.unityLogger.Log(LogType.Error, $"{PREFIX}: {msg}");
if (batch == null) return;
foreach (var record in batch.Records)
{
if (record == null || !record.HasMessage || string.IsNullOrEmpty(record.Message)) continue;
var formatted = FormatLogMessage(record);
switch (record.Level)
{
case LogLevel.LogError:
Error(formatted);
break;
case LogLevel.LogWarn:
Warning(formatted);
break;
case LogLevel.LogInfo:
Info(formatted);
break;
case LogLevel.LogDebug:
case LogLevel.LogTrace:
Debug(formatted);
break;
default:
Info(formatted);
break;
}
}
}

private static string FormatLogMessage(LogRecord record)
{
var parts = new List<string> { record.Message };
if (record.HasFile && !string.IsNullOrEmpty(record.File))
{
var location = record.HasLine && record.Line > 0
? $"{record.File}:{record.Line}"
: record.File;
parts.Add($"Source: {location} (FFI)");
}
if (record.HasModulePath && !string.IsNullOrEmpty(record.ModulePath))
parts.Add($"Module: {record.ModulePath}");

if (record.HasTarget && !string.IsNullOrEmpty(record.Target))
parts.Add($"Target: {record.Target}");

return string.Join("\n", parts);
}

public static GraphicsFormat GetSupportedGraphicsFormat(GraphicsDeviceType type)
Expand Down
Loading