-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using fluXis.Game.Configuration; | ||
using fluXis.Shared.Components.Users; | ||
using osu.Framework.Allocation; | ||
using osu.Framework.Bindables; | ||
using osu.Framework.Configuration; | ||
using osu.Framework.Graphics; | ||
using osu.Framework.Logging; | ||
using Sentry; | ||
|
||
namespace fluXis.Game.Utils.Sentry; | ||
|
||
public partial class SentryClient : Component | ||
{ | ||
private FluXisGame game { get; } | ||
private IDisposable session { get; } | ||
private IBindable<APIUser> user { get; set; } | ||
|
||
public SentryClient(FluXisGame game) | ||
{ | ||
this.game = game; | ||
|
||
Logger.Log("Initializing sentry client..."); | ||
|
||
if (FluXisGameBase.IsDebug) | ||
return; | ||
|
||
session = SentrySdk.Init(opt => | ||
{ | ||
opt.Dsn = "https://[email protected]/2"; | ||
opt.IsEnvironmentUser = false; | ||
opt.AutoSessionTracking = true; | ||
opt.IsGlobalModeEnabled = true; | ||
opt.Release = FluXisGameBase.VersionString.TrimStart('v'); | ||
}); | ||
|
||
Logger.Log("Initialized sentry client."); | ||
|
||
Logger.NewEntry += onEntry; | ||
} | ||
|
||
public void BindUser(IBindable<APIUser> bind) | ||
{ | ||
if (session == null) | ||
return; | ||
|
||
user = bind.GetBoundCopy(); | ||
user.BindValueChanged(e => | ||
{ | ||
SentrySdk.ConfigureScope(s => s.User = new SentryUser | ||
{ | ||
Username = e.NewValue?.Username ?? "Guest User", | ||
Id = $"{e.NewValue?.ID ?? 0}" | ||
}); | ||
}); | ||
} | ||
|
||
private void onEntry(LogEntry entry) | ||
{ | ||
if (entry.Level != LogLevel.Error) | ||
return; | ||
|
||
var ex = entry.Exception; | ||
|
||
if (ex == null) | ||
return; | ||
|
||
SentrySdk.CaptureEvent(new SentryEvent(ex) | ||
{ | ||
Message = entry.Message, | ||
Level = entry.Level switch | ||
{ | ||
LogLevel.Debug => SentryLevel.Debug, | ||
LogLevel.Verbose => SentryLevel.Info, | ||
LogLevel.Important => SentryLevel.Warning, | ||
LogLevel.Error => SentryLevel.Error, | ||
_ => throw new ArgumentOutOfRangeException() | ||
} | ||
}, scope => | ||
{ | ||
scope.Contexts["config"] = new | ||
{ | ||
Game = game.Dependencies.Get<FluXisConfig>()?.GetCurrentConfigurationForLogging(), | ||
Framework = game.Dependencies.Get<FrameworkConfigManager>()?.GetCurrentConfigurationForLogging(), | ||
}; | ||
}); | ||
} | ||
|
||
protected override void Dispose(bool isDisposing) | ||
{ | ||
base.Dispose(isDisposing); | ||
|
||
Logger.NewEntry -= onEntry; | ||
session?.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters