Skip to content

Commit 95a4907

Browse files
authored
Merge pull request #36 from bugsnag/codehex/mapping-severities
Added custom mapping to severities
2 parents 3b5bc68 + 70aad4c commit 95a4907

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

Bugsnag.unitypackage

-1.9 KB
Binary file not shown.

src/Assets/Standard Assets/Bugsnag/Bugsnag.cs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ public static void SetAppVersion(string version) {}
225225
public static void SetUser(string userId, string userName, string userEmail) {}
226226
#endif
227227
}
228-
229228
// We dont use the LogType enum in Unity as the numerical order doesnt suit our purposes
230229
public enum LogSeverity {
231230
Log = 0,
@@ -242,6 +241,29 @@ public enum Severity {
242241
Warning = 2
243242
}
244243

244+
// Defines a translation between the Unity log types and Bugsnag log types with appropriate ordering
245+
private static Dictionary<LogType, LogSeverity> logTypeMapping = new Dictionary<LogType, LogSeverity>()
246+
{
247+
{ LogType.Assert, LogSeverity.Assert },
248+
{ LogType.Error, LogSeverity.Error },
249+
{ LogType.Exception, LogSeverity.Exception },
250+
{ LogType.Log, LogSeverity.Log },
251+
{ LogType.Warning, LogSeverity.Warning }
252+
};
253+
254+
// Defines a default mapping between Unity severities and Bugsnag severities
255+
private static Dictionary<LogSeverity, Severity> defaultMapping = new Dictionary<LogSeverity, Severity>()
256+
{
257+
{ LogSeverity.Assert, Severity.Warning },
258+
{ LogSeverity.Error, Severity.Warning },
259+
{ LogSeverity.Exception, Severity.Error },
260+
{ LogSeverity.Log, Severity.Info },
261+
{ LogSeverity.Warning, Severity.Warning }
262+
};
263+
264+
// Defines a custom mapping between Unity severities and Bugsnag severities
265+
private static Dictionary<LogSeverity,Severity> customMapping = new Dictionary<LogSeverity,Severity>();
266+
245267
// Defines the strings used for the severities
246268
public static string[] SeverityValues = new string[]{"info", "error", "warning"};
247269

@@ -285,6 +307,10 @@ public static string[] NotifyReleaseStages {
285307
}
286308
}
287309

310+
public static void MapUnityLogToSeverity(LogSeverity unitySeverity, Severity bugsnagSeverity) {
311+
customMapping[unitySeverity] = bugsnagSeverity;
312+
}
313+
288314
string GetLevelName() {
289315
#if UNITY_5_OR_NEWER
290316
return SceneManager.GetActiveScene().name;
@@ -349,32 +375,16 @@ void OnLevelWasLoaded(int level) {
349375
#endif
350376

351377
void HandleLog (string logString, string stackTrace, LogType type) {
352-
LogSeverity severity = LogSeverity.Exception;
378+
// Use any custom log mapping, and if there isn't one, use the default mapping
379+
LogSeverity logSeverity = logTypeMapping[type];
353380
Severity bugsnagSeverity = Severity.Error;
381+
if (customMapping.ContainsKey(logSeverity)) {
382+
bugsnagSeverity = customMapping[logSeverity];
383+
} else if (defaultMapping.ContainsKey(logSeverity)) {
384+
bugsnagSeverity = defaultMapping[logSeverity];
385+
}
354386

355-
switch (type) {
356-
case LogType.Assert:
357-
severity = LogSeverity.Assert;
358-
break;
359-
case LogType.Error:
360-
severity = LogSeverity.Error;
361-
break;
362-
case LogType.Exception:
363-
severity = LogSeverity.Exception;
364-
break;
365-
case LogType.Log:
366-
severity = LogSeverity.Log;
367-
bugsnagSeverity = Severity.Info;
368-
break;
369-
case LogType.Warning:
370-
severity = LogSeverity.Warning;
371-
bugsnagSeverity = Severity.Warning;
372-
break;
373-
default:
374-
break;
375-
}
376-
377-
if(severity >= NotifyLevel && AutoNotify) {
387+
if(logSeverity >= NotifyLevel) {
378388
string errorClass, errorMessage = "";
379389

380390
Regex exceptionRegEx = new Regex(@"^(?<errorClass>\S+):\s*(?<message>.*)");

0 commit comments

Comments
 (0)