Skip to content

Commit

Permalink
WIP5 - services
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansjfw committed Aug 12, 2024
1 parent e849431 commit 5475356
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 55 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageVersion Include="Markdig.Signed" Version="0.34.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.7" />
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.13" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
// See the LICENSE file in the project root for more information.

using System;
using ManagedCommon;
using Microsoft.Win32;

namespace Microsoft.PowerToys.Settings.Helpers
namespace Microsoft.PowerToys.Telemetry
{
public static class DataDiagnostics
public static class DataDiagnosticsSettings
{
private static readonly string DataDiagnosticsRegistryKey = @"HKEY_CURRENT_USER\Software\Classes\PowerToys\";
private static readonly string DataDiagnosticsRegistryValueName = @"AllowDataDiagnostics";
private static readonly string DataDiagnosticsDataDiagnosticsUserActionRegistryValueName = @"DataDiagnosticsUserAction";
private static readonly string DataDiagnosticsDataDiagnosticsViewDataRegistryValueName = @"DataDiagnosticsViewEnabled";

public static bool GetValue()
public static bool GetEnabledValue()
{
object registryValue = null;
try
Expand All @@ -34,15 +33,14 @@ public static bool GetValue()
return false;
}

public static void SetValue(bool value)
public static void SetEnabledValue(bool value)
{
try
{
Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsRegistryValueName, value ? 1 : 0);
}
catch (Exception ex)
catch (Exception)
{
Logger.LogError($"Failed to set the Data Diagnostics value in the registry: {ex.Message}");
}
}

Expand Down Expand Up @@ -71,9 +69,8 @@ public static void SetUserActionValue(bool value)
{
Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsUserActionRegistryValueName, value ? 1 : 0);
}
catch (Exception ex)
catch (Exception)
{
Logger.LogError($"Failed to set the Data Diagnostics user action value in the registry: {ex.Message}");
}
}

Expand Down Expand Up @@ -102,9 +99,8 @@ public static void SetViewEnabledValue(bool value)
{
Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsViewDataRegistryValueName, value ? 1 : 0);
}
catch (Exception ex)
catch (Exception)
{
Logger.LogError($"Failed to set the Data Diagnostics view enabled value in the registry: {ex.Message}");
}
}
}
Expand Down
131 changes: 131 additions & 0 deletions src/common/ManagedTelemetry/Telemetry/EtwTrace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.IO;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.PowerToys.Telemetry;

namespace WSACrashHelper
{
/// <summary>
/// This class is based loosely on the C++ ETWTrace class in Win32client/Framework project.
/// It is intended to record telemetry events generated by the WSACrashUploader so that end users
/// can view them if they want.
/// </summary>
public class ETWTrace : IDisposable
{
internal const EventKeywords TelemetryKeyword = (EventKeywords)0x0000200000000000;
internal const EventKeywords MeasuresKeyword = (EventKeywords)0x0000400000000000;
internal const EventKeywords CriticalDataKeyword = (EventKeywords)0x0000800000000000;

private readonly bool telemetryEnabled = DataDiagnosticsSettings.GetEnabledValue(); // This is the global telemetry setting on whether to log events
private readonly bool telemetryRecordingEnabled = DataDiagnosticsSettings.GetViewEnabledValue(); // This is the setting for recording telemetry events to disk for vewng
private readonly string etwFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\PowerToys\", "etw");
private bool disposedValue;
private string sessionName;
private string etwFilePath;
private bool started;
#nullable enable
private TraceEventSession? traceSession;
#nullable disable

/// <summary>
/// Initializes a new instance of the <see cref="ETWTrace"/> class.
/// </summary>
public ETWTrace()
{
if (File.Exists(etwFolderPath))
{
File.Delete(etwFolderPath);
}

if (!Directory.Exists(etwFolderPath))
{
Directory.CreateDirectory(etwFolderPath);
}

if (this.telemetryEnabled && this.telemetryRecordingEnabled)
{
this.Start();
}
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Starts the trace session.
/// </summary>
public void Start()
{
lock (this)
{
if (this.started)
{
return;
}

string executable = Process.GetCurrentProcess().ProcessName;
string dateTimeNow = DateTime.Now.ToString("MM-d-yyy__H_mm_ss", CultureInfo.InvariantCulture);
this.sessionName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}-{2}", executable, Environment.ProcessId, dateTimeNow);
this.etwFilePath = Path.Combine(etwFolderPath, $"{this.sessionName}.etl");
this.traceSession = new TraceEventSession(
this.sessionName, this.etwFilePath, (TraceEventSessionOptions)(1 | 0x10 | 0x20));
this.traceSession.EnableProvider(
PowerToysTelemetry.Log.Guid,
matchAnyKeywords: (ulong)TelemetryKeyword | (ulong)MeasuresKeyword | (ulong)CriticalDataKeyword);

this.started = true;
}
}

/// <summary>
/// Stops the trace session.
/// </summary>
public void Stop()
{
lock (this)
{
if (!this.started)
{
return;
}

if (this.traceSession != null)
{
Trace.TraceInformation("Disposing EventTraceSession");
this.traceSession.Dispose();
this.traceSession = null;
this.started = false;
}
}
}

/// <summary>
/// Disposes the object.
/// </summary>
/// <param name="disposing">boolean for disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.Stop();
}

this.disposedValue = true;
}
}
}
}
4 changes: 4 additions & 0 deletions src/common/ManagedTelemetry/Telemetry/ManagedTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<Compile Include="..\..\Telemetry\TelemetryBase.cs" Link="TelemetryBase.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" />
</ItemGroup>

</Project>
27 changes: 2 additions & 25 deletions src/common/ManagedTelemetry/Telemetry/PowerToysTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry.Events;
using Microsoft.Win32;

namespace Microsoft.PowerToys.Telemetry
{
/// <summary>
/// Telemetry helper class for PowerToys.
/// </summary>
[EventSource(Name = "Microsoft.PowerToys")]
public class PowerToysTelemetry : TelemetryBase
{
private static readonly string DataDiagnosticsRegistryKey = @"HKEY_CURRENT_USER\Software\Classes\PowerToys\";
private static readonly string DataDiagnosticsRegistryValueName = @"AllowDataDiagnostics";

/// <summary>
/// Name for ETW event.
/// </summary>
Expand All @@ -35,32 +31,13 @@ public PowerToysTelemetry()
/// </summary>
public static PowerToysTelemetry Log { get; } = new PowerToysTelemetry();

private static bool IsDataDiagnosticsEnabled()
{
object registryValue = null;
try
{
registryValue = Registry.GetValue(DataDiagnosticsRegistryKey, DataDiagnosticsRegistryValueName, 0);
}
catch
{
}

if (registryValue is not null)
{
return (int)registryValue == 1 ? true : false;
}

return false;
}

/// <summary>
/// Publishes ETW event when an action is triggered on
/// </summary>
public void WriteEvent<T>(T telemetryEvent)
where T : EventBase, IEvent
{
if (IsDataDiagnosticsEnabled())
if (DataDiagnosticsSettings.GetEnabledValue())
{
this.Write<T>(
telemetryEvent.EventName,
Expand Down
1 change: 1 addition & 0 deletions src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Windows.UI.Popups;
using WinRT.Interop;
using WinUIEx;
using WSACrashHelper;

namespace Microsoft.PowerToys.Settings.UI
{
Expand Down
4 changes: 4 additions & 0 deletions src/settings-ui/Settings.UI/SettingsXAML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.UI.Xaml;
using Windows.Data.Json;
using WinUIEx;
using WSACrashHelper;

namespace Microsoft.PowerToys.Settings.UI
{
Expand All @@ -22,6 +23,8 @@ namespace Microsoft.PowerToys.Settings.UI
/// </summary>
public sealed partial class MainWindow : WindowEx
{
private static ETWTrace etwTrace = new ETWTrace();

public MainWindow(bool createHidden = false)
{
var bootTime = new System.Diagnostics.Stopwatch();
Expand Down Expand Up @@ -217,6 +220,7 @@ private void Window_Closed(object sender, WindowEventArgs args)
NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE);
}

etwTrace.Dispose();
App.ThemeService.ThemeChanged -= OnThemeChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// See the LICENSE file in the project root for more information.

using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.ViewModel;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;

Expand All @@ -32,7 +31,7 @@ public bool EnableDataDiagnostics
{
_enableDataDiagnostics = value;

DataDiagnostics.SetValue(_enableDataDiagnostics);
DataDiagnosticsSettings.SetEnabledValue(_enableDataDiagnostics);

this.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
Expand All @@ -55,7 +54,7 @@ public OobeOverview()
{
this.InitializeComponent();

_enableDataDiagnostics = DataDiagnostics.GetValue();
_enableDataDiagnostics = DataDiagnosticsSettings.GetEnabledValue();

ViewModel = new OobePowerToysModule(OobeShellPage.OobeShellHandler.Modules[(int)PowerToysModules.Overview]);
DataContext = ViewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
using CommunityToolkit.WinUI.UI.Controls;
using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.Helpers;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.ViewModel;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Win32;

namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
{
Expand Down Expand Up @@ -70,14 +67,14 @@ private bool GetShowDataDiagnosticsInfoBar()
return false;
}

bool userActed = DataDiagnostics.GetUserActionValue();
bool userActed = DataDiagnosticsSettings.GetUserActionValue();

if (userActed)
{
return false;
}

bool registryValue = DataDiagnostics.GetValue();
bool registryValue = DataDiagnosticsSettings.GetEnabledValue();

bool isFirstRunAfterUpdate = (App.Current as Microsoft.PowerToys.Settings.UI.App).ShowScoobe;
if (isFirstRunAfterUpdate && registryValue == false)
Expand Down Expand Up @@ -237,14 +234,14 @@ private void DataDiagnostics_InfoBar_YesNo_Click(object sender, Microsoft.UI.Xam
// Set Data Diagnostics registry values
if (commandArg == "Yes")
{
DataDiagnostics.SetValue(true);
DataDiagnosticsSettings.SetEnabledValue(true);
}
else
{
DataDiagnostics.SetValue(false);
DataDiagnosticsSettings.SetEnabledValue(false);
}

DataDiagnostics.SetUserActionValue(true);
DataDiagnosticsSettings.SetUserActionValue(true);

this.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
Expand Down
Loading

0 comments on commit 5475356

Please sign in to comment.