<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable enable

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Game.Online.API.Requests.Responses;
using Sentry;

namespace osu.Game.Utils
{
    /// <summary>
    /// Report errors to sentry.
    /// </summary>
    public class SentryLogger : IDisposable
    {
        private Exception? lastException;

        private IBindable<APIUser>? localUser;

        private readonly IDisposable? sentrySession;

        public SentryLogger(OsuGame game)
        {
            sentrySession = SentrySdk.Init(options =>
            {
                // Not setting the dsn will completely disable sentry.
                if (game.IsDeployedBuild)
                    options.Dsn = "https://ad9f78529cef40ac874afb95a9aca04e@sentry.ppy.sh/2";

                options.AutoSessionTracking = true;
                options.IsEnvironmentUser = false;
                options.Release = game.Version;
            });

            Logger.NewEntry += processLogEntry;
        }

        public void AttachUser(IBindable<APIUser> user)
        {
            Debug.Assert(localUser == null);

            localUser = user.GetBoundCopy();
            localUser.BindValueChanged(u =>
            {
                SentrySdk.ConfigureScope(scope => scope.User = new User
                {
                    Username = u.NewValue.Username,
                    Id = u.NewValue.Id.ToString(),
                });
            }, true);
        }

        private void processLogEntry(LogEntry entry)
        {
            if (entry.Level < LogLevel.Verbose) return;

            var exception = entry.Exception;

            if (exception != null)<commit_msg>Add finalizer to sentry logger for safety<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable enable

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Game.Online.API.Requests.Responses;
using Sentry;

namespace osu.Game.Utils
{
    /// <summary>
    /// Report errors to sentry.
    /// </summary>
    public class SentryLogger : IDisposable
    {
        private Exception? lastException;

        private IBindable<APIUser>? localUser;

        private readonly IDisposable? sentrySession;

        public SentryLogger(OsuGame game)
        {
            sentrySession = SentrySdk.Init(options =>
            {
                // Not setting the dsn will completely disable sentry.
                if (game.IsDeployedBuild)
                    options.Dsn = "https://ad9f78529cef40ac874afb95a9aca04e@sentry.ppy.sh/2";

                options.AutoSessionTracking = true;
                options.IsEnvironmentUser = false;
                options.Release = game.Version;
            });

            Logger.NewEntry += processLogEntry;
        }

        ~SentryLogger() => Dispose(false);

        public void AttachUser(IBindable<APIUser> user)
        {
            Debug.Assert(localUser == null);

            localUser = user.GetBoundCopy();
            localUser.BindValueChanged(u =>
            {
                SentrySdk.ConfigureScope(scope => scope.User = new User
                {
                    Username = u.NewValue.Username,
                    Id = u.NewValue.Id.ToString(),
                });
            }, true);
        }

        private void processLogEntry(LogEntry entry)
        {
            if (entry.Level < LogLevel.Verbose) return;

            var exception = entry.Exception;

            if (exception != null)
{'fraction_non_alphanumeric': 0.09180497925311204, 'fraction_numerical': 0.008817427385892116, 'mean_word_length': 1.8381162619573215, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5192009', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1034, 'n_words': 249}