using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; using Plugins.Countly.Helpers; using Plugins.Countly.Models; using UnityEngine; namespace Plugins.Countly.Services.Impls.Actual { public class CrushReportsCountlyService : ICrushReportsCountlyService { private readonly Queue _crashBreadcrumbs = new Queue(); private readonly CountlyConfigModel _configModel; private readonly RequestCountlyHelper _requestCountlyHelper; public CrushReportsCountlyService(CountlyConfigModel configModel, RequestCountlyHelper requestCountlyHelper) { _configModel = configModel; _requestCountlyHelper = requestCountlyHelper; } /// /// Called when there is an exception /// /// Exception Class /// Stack Trace /// Excpetion type like error, warning, etc public async void LogCallback(string message, string stackTrace, LogType type) { if (_configModel.EnableAutomaticCrashReporting && (type == LogType.Error || type == LogType.Exception)) { await SendCrashReportAsync(message, stackTrace, type, null, false); } } /// /// Private method that sends crash details to the server. Set param "nonfatal" to true for Custom Logged errors /// /// /// /// /// /// /// public async Task SendCrashReportAsync(string message, string stackTrace, LogType type, IDictionary segments = null, bool nonfatal = true) { //if (ConsentModel.CheckConsent(FeaturesEnum.Crashes.ToString())) //{ var model = CountlyExceptionDetailModel.ExceptionDetailModel; model.Error = stackTrace; model.Name = message; model.Nonfatal = nonfatal; model.Custom = segments as Dictionary; model.Logs = string.Join("\n", _crashBreadcrumbs); #if UNITY_IOS model.Manufacture = UnityEngine.iOS.Device.generation.ToString(); #endif #if UNITY_ANDROID model.Manufacture = SystemInfo.deviceModel; #endif var requestParams = new Dictionary { { "crash", JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}) } }; return await _requestCountlyHelper.GetResponseAsync(requestParams); //} } /// /// Sends custom logged errors to the server. /// /// /// /// /// /// public async Task SendCrashReportAsync(string message, string stackTrace, LogType type, IDictionary segments = null) { return await SendCrashReportAsync(message, stackTrace, type, segments, true); } /// /// Adds string value to a list which is later sent over as logs whenever a cash is reported by system. /// The length of a breadcrumb is limited to 1000 characters. Only first 1000 characters will be accepted in case the length is more /// than 1000 characters. /// /// public void AddBreadcrumbs(string value) { var validBreadcrumb = value.Length > 1000 ? value.Substring(0, 1000) : value; if (_crashBreadcrumbs.Count == _configModel.TotalBreadcrumbsAllowed) _crashBreadcrumbs.Dequeue(); _crashBreadcrumbs.Enqueue(value); } } }