Skip to content

Commit

Permalink
Xaml diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Jan 18, 2024
1 parent 834cfcc commit d13625f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Telegram/Common/WatchDog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Microsoft.AppCenter.Crashes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Telegram.Common;
using Telegram.Controls;
Expand All @@ -18,6 +20,7 @@
using Windows.System;
using Windows.System.Profile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using File = System.IO.File;
Expand Down Expand Up @@ -68,6 +71,9 @@ public class WatchDog
private static string _lastSessionErrorReportId;
private static bool _lastSessionTerminatedUnexpectedly;

[DllImport("Telegram.Diagnostics.dll")]
private static extern int start(uint pid, uint framework);

static WatchDog()
{
_crashLog = Path.Combine(ApplicationData.Current.LocalFolder.Path, "crash.log");
Expand Down Expand Up @@ -107,6 +113,18 @@ public static void Initialize()
args.Handled = args.Exception is not LayoutCycleException;
};

if (Constants.DEBUG && !Debugger.IsAttached)
{
var process = Process.GetCurrentProcess();
var hr = start((uint)process.Id, 1);

var ex = Marshal.GetExceptionForHR(hr);
if (ex != null)
{
Crashes.TrackError(ex);
}
}

if (_disabled)
{
return;
Expand Down Expand Up @@ -144,6 +162,13 @@ public static void Initialize()
if (path.Length > 0 && File.Exists(path))
{
var data = File.ReadAllText(path);
var layout = GetLayoutCyclePath();
if (layout.Length > 0 && File.Exists(layout))
{
data += "\n\n" + File.ReadAllText(layout);
}
return new[] { ErrorAttachmentLog.AttachmentWithText(data, "crash.txt") };
}
Expand All @@ -157,6 +182,18 @@ public static void Initialize()
{ "DeviceFamily", AnalyticsInfo.VersionInfo.DeviceFamily },
{ "Architecture", Package.Current.Id.Architecture.ToString() }
});

if (SettingsService.Current.Diagnostics.XamlDiagnostics)
{
var process = Process.GetCurrentProcess();
var hr = start((uint)process.Id, 1);

var ex = Marshal.GetExceptionForHR(hr);
if (ex != null)
{
Crashes.TrackError(ex);
}
}
}

public static void TrackEvent(string name)
Expand Down Expand Up @@ -286,6 +323,11 @@ private static string GetErrorReportPath(string reportId)
return Path.Combine(ApplicationData.Current.LocalFolder.Path, _reports, reportId + ".appcenter");
}

private static string GetLayoutCyclePath()
{
return Path.Combine(ApplicationData.Current.LocalFolder.Path, "LayoutCycle.txt");
}

public static void Suspend()
{
if (File.Exists(_crashLog))
Expand Down
14 changes: 14 additions & 0 deletions Telegram/Services/Settings/DiagnosticsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public bool EnableWebViewDevTools
set => AddOrUpdateValue(ref _enableWebViewDevTools, "EnableWebViewDevTools", value);
}

private bool? _bridgeDebug;
public bool BridgeDebug
{
get => _bridgeDebug ??= GetValueOrDefault("BridgeDebug", false);
set => AddOrUpdateValue(ref _bridgeDebug, "BridgeDebug", value);
}

private long? _storageMaxTimeFromLastAccess;
public long StorageMaxTimeFromLastAccess
{
Expand All @@ -94,6 +101,13 @@ public bool UseStorageOptimizer
set => AddOrUpdateValue(ref _useStorageOptimizer, "UseStorageOptimizer", value);
}

private bool? _xamlDiagnostics;
public bool XamlDiagnostics
{
get => _xamlDiagnostics ??= GetValueOrDefault("XamlDiagnostics", true);
set => AddOrUpdateValue(ref _xamlDiagnostics, "XamlDiagnostics", value);
}

private bool? _lastCrashWasLayoutCycle;
public bool LastCrashWasLayoutCycle
{
Expand Down
Binary file added Telegram/Telegram.Diagnostics.dll
Binary file not shown.
1 change: 1 addition & 0 deletions Telegram/Telegram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@
<Compile Include="Controls\ProgressVoice.cs" />
<Compile Include="Converters\LastSeenConverter.cs" />
<Compile Include="ViewModels\InstantViewModel.cs" />
<Content Include="Telegram.Diagnostics.dll" />
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
Expand Down
4 changes: 4 additions & 0 deletions Telegram/Views/DiagnosticsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
Content="Use Test DC"
Style="{StaticResource SettingsCheckBoxStyle}" />

<CheckBox IsChecked="{x:Bind services:SettingsService.Current.Diagnostics.XamlDiagnostics, Mode=TwoWay}"
Content="Xaml Diagnostics"
Style="{StaticResource SettingsCheckBoxStyle}" />

<CheckBox IsChecked="{x:Bind services:SettingsService.Current.Diagnostics.AllowRightToLeft, Mode=TwoWay}"
Content="Allow Right to Left"
Style="{StaticResource SettingsCheckBoxStyle}" />
Expand Down

0 comments on commit d13625f

Please sign in to comment.