|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using Sentry.Protocol; |
| 5 | + |
| 6 | +namespace Sentry.Unity.Integrations; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Factory for creating SentryEvent objects from Unity log messages and stacktraces |
| 10 | +/// </summary> |
| 11 | +internal static class UnityLogEventFactory |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Creates a message event with stacktrace attached via threads (for Debug.LogError) |
| 15 | + /// </summary> |
| 16 | + /// <param name="message">The log message</param> |
| 17 | + /// <param name="stackTrace">The Unity stacktrace string</param> |
| 18 | + /// <param name="level">The Sentry event level</param> |
| 19 | + /// <param name="options">Sentry Unity options</param> |
| 20 | + /// <returns>A SentryEvent with the message and stacktrace as threads</returns> |
| 21 | + public static SentryEvent CreateMessageEvent( |
| 22 | + string message, |
| 23 | + string stackTrace, |
| 24 | + SentryLevel level, |
| 25 | + SentryUnityOptions options) |
| 26 | + { |
| 27 | + var frames = UnityStackTraceParser.Parse(stackTrace, options); |
| 28 | + frames.Reverse(); |
| 29 | + |
| 30 | + var thread = CreateThreadFromStackTrace(frames); |
| 31 | + |
| 32 | + return new SentryEvent |
| 33 | + { |
| 34 | + Message = message, |
| 35 | + Level = level, |
| 36 | + SentryThreads = [thread] |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates an exception event from Unity log data (for exceptions on WebGL) |
| 42 | + /// </summary> |
| 43 | + /// <param name="message">The log message</param> |
| 44 | + /// <param name="stackTrace">The Unity stacktrace string</param> |
| 45 | + /// <param name="handled">Whether the exception was handled or not</param> |
| 46 | + /// /// <param name="options">Sentry Unity options</param> |
| 47 | + /// <returns>A SentryEvent with a synthetic exception</returns> |
| 48 | + public static SentryEvent CreateExceptionEvent( |
| 49 | + string message, |
| 50 | + string stackTrace, |
| 51 | + bool handled, |
| 52 | + SentryUnityOptions options) |
| 53 | + { |
| 54 | + var frames = UnityStackTraceParser.Parse(stackTrace, options); |
| 55 | + frames.Reverse(); |
| 56 | + |
| 57 | + return new SentryEvent |
| 58 | + { |
| 59 | + SentryExceptions = [new SentryException |
| 60 | + { |
| 61 | + Stacktrace = new SentryStackTrace { Frames = frames }, |
| 62 | + Value = message, |
| 63 | + Type = "LogException", |
| 64 | + Mechanism = new Mechanism |
| 65 | + { |
| 66 | + Handled = handled, |
| 67 | + Type = "unity.log", |
| 68 | + Terminal = false, |
| 69 | + Synthetic = true |
| 70 | + } |
| 71 | + }], |
| 72 | + Level = SentryLevel.Error |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + private static SentryThread CreateThreadFromStackTrace(List<SentryStackFrame> frames) |
| 77 | + { |
| 78 | + var currentThread = Thread.CurrentThread; |
| 79 | + return new SentryThread |
| 80 | + { |
| 81 | + Crashed = false, |
| 82 | + Current = true, |
| 83 | + Name = currentThread.Name, |
| 84 | + Id = currentThread.ManagedThreadId, |
| 85 | + Stacktrace = new SentryStackTrace { Frames = frames } |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
0 commit comments