Skip to content

Commit

Permalink
Completed first verison of WpfTraceSpy.
Browse files Browse the repository at this point in the history
  • Loading branch information
smourier committed Mar 10, 2018
1 parent 3d907e2 commit e190121
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 31 deletions.
10 changes: 8 additions & 2 deletions TraceSpyTest/Program.cs
Expand Up @@ -67,7 +67,7 @@ static void SafeMain(string[] args)
var etwProvider = CommandLineUtilities.GetArgument("etw", Guid.Empty);
Console.WriteLine("TraceSpy Test.");
Console.WriteLine();
Console.WriteLine("Press Q (or CTRL-C) to quit");
Console.WriteLine("Press Q, ESC, or CTRL-C to quit");
Console.WriteLine();
Console.WriteLine("OutputDebugString");
Console.WriteLine(" Press O to send an OutputDebugString trace.");
Expand All @@ -92,6 +92,9 @@ static void SafeMain(string[] args)
int num = GetFinalNumber(info);
switch (info.Key)
{
case ConsoleKey.Escape:
return;

case ConsoleKey.Q:
if (_etw != null)
{
Expand Down Expand Up @@ -119,7 +122,10 @@ static void SafeMain(string[] args)
for (int i = 1; i <= num; i++)
{
t = "Trace #" + count + ", " + i + "/" + num + " from TraceSpyTest. Date:" + DateTime.Now;
Console.WriteLine("Sending: '" + t + "'");
if (num < 1000 || (i % 1000) == 0)
{
Console.WriteLine("Sending: '" + t + "'");
}
_etw.WriteMessageEvent(t);
count++;
}
Expand Down
12 changes: 0 additions & 12 deletions WpfTraceSpy/EtwProvidersWindow.xaml
Expand Up @@ -12,18 +12,6 @@
<RowDefinition Height="45" />
</Grid.RowDefinitions>
<ListView Name="LV" SelectionMode="Single" ItemsSource="{Binding Providers}" MouseDoubleClick="LV_MouseDoubleClick">
<ListBox.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Background" Value="Transparent" />
</DataTrigger>
<DataTrigger Binding="{Binding IsActive}" Value="False">
<Setter Property="Background" Value="#E0E0E0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Active" Width="40" DisplayMemberBinding="{Binding IsActive}" ></GridViewColumn>
Expand Down
12 changes: 0 additions & 12 deletions WpfTraceSpy/FiltersWindow.xaml
Expand Up @@ -12,18 +12,6 @@
<RowDefinition Height="45" />
</Grid.RowDefinitions>
<ListView Name="LV" SelectionMode="Single" ItemsSource="{Binding Filters}" MouseDoubleClick="LV_MouseDoubleClick">
<ListBox.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Background" Value="Transparent" />
</DataTrigger>
<DataTrigger Binding="{Binding IsActive}" Value="False">
<Setter Property="Background" Value="#E0E0E0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Active" Width="40" DisplayMemberBinding="{Binding IsActive}" ></GridViewColumn>
Expand Down
2 changes: 1 addition & 1 deletion WpfTraceSpy/MainWindow.xaml
Expand Up @@ -84,7 +84,7 @@
<MenuItem Header="Find _Next" Name="FindNext" InputGestureText="F3" Click="FindNext_Click" />
<MenuItem Header="Find _Prev" Name="FindPrev" InputGestureText="Shift+F3" Click="FindPrev_Click" />
<Separator />
<MenuItem Header="_Clear Traces" Name="ClearTraces" Click="ClearTraces_Click" />
<MenuItem Header="_Clear Traces" Name="ClearTraces" Click="ClearTraces_Click" InputGestureText="Ctrl+X" />
<MenuItem Header="_Send Test Trace" Name="SendTestTrace" Click="SendTestTrace_Click" Visibility="Collapsed" />
</MenuItem>
<MenuItem Header="_Options">
Expand Down
92 changes: 91 additions & 1 deletion WpfTraceSpy/MainWindow.xaml.cs
Expand Up @@ -11,7 +11,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
Expand All @@ -30,6 +29,7 @@ public partial class MainWindow : Window
private ObservableCollection<TraceEvent> _dataSource = new ObservableCollection<TraceEvent>();
private MainWindowState _state;
private ConcurrentDictionary<int, string> _processes = new ConcurrentDictionary<int, string>();
private Dictionary<Guid, EventRealtimeListener> _etwListeners = new Dictionary<Guid, EventRealtimeListener>();
private FindWindow _findWindow;
private int _scrollingTo;
private TraceEvent _scrollTo;
Expand Down Expand Up @@ -126,6 +126,7 @@ private void OnStatePropertyChanged(object sender, PropertyChangedEventArgs e)

protected override void OnClosed(EventArgs e)
{
DisposeEtwEvents();
_findWindow?.Close();
_stopOutputDebugStringTraces = true;
_outputDebugStringThread?.Join(1000);
Expand Down Expand Up @@ -276,6 +277,7 @@ private void OdsTrace_Click(object sender, RoutedEventArgs e)
private void EtwTrace_Click(object sender, RoutedEventArgs e)
{
_state.EtwStarted = !_state.EtwStarted;
UpdateEtwEvents();
}

private string GetProcessName(int id)
Expand Down Expand Up @@ -318,6 +320,7 @@ private void ETWProviders_Click(object sender, RoutedEventArgs e)
var dlg = new EtwProvidersWindow();
dlg.Owner = this;
dlg.ShowDialog();
UpdateEtwEvents();
}

private void Filters_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -463,5 +466,92 @@ private void About_Click(object sender, RoutedEventArgs e)
dlg.Owner = this;
dlg.ShowDialog();
}

private void DisposeEtwEvents()
{
foreach (var kvp in _etwListeners)
{
try
{
kvp.Value.RealtimeEvent -= OnEtwListenerRealtimeEvent;
kvp.Value.Dispose();
}
catch
{
// do nothing, continue
}
}
_etwListeners.Clear();
}

private void ProcessEtwTrace(object state)
{
var listener = (EventRealtimeListener)state;
listener.ProcessTraces();
}

private void OnEtwListenerRealtimeEvent(object sender, EventRealtimeEventArgs e)
{
var listener = (EventRealtimeListener)sender;

var evt = new TraceEvent();
evt.ProcessName = GetProcessName(e.ProcessId);
if (listener.Description != null && _state.ShowEtwDescription)
{
evt.ProcessName += " (" + listener.Description + ")";
}
evt.Text = e.Message;

ThreadPool.QueueUserWorkItem((state) =>
{
Dispatcher.BeginInvoke(() =>
{
AddTrace(evt);
}, DispatcherPriority.SystemIdle);
});
}

private void UpdateEtwEvents()
{
DisposeEtwEvents();
if (!_state.EtwStarted)
return;

foreach (EtwProvider provider in App.Current.Settings.EtwProviders)
{
_etwListeners.TryGetValue(provider.Guid, out EventRealtimeListener listener);
if (listener == null)
{
if (!provider.IsActive)
continue;

// this would be a serialization bug
if (provider.Guid == Guid.Empty)
continue;

var level = (EtwTraceLevel)provider.TraceLevel;

listener = new EventRealtimeListener(provider.Guid, provider.Guid.ToString(), level);
listener.Description = provider.Description;

var t = new Thread(ProcessEtwTrace);
t.Start(listener);

listener.RealtimeEvent += OnEtwListenerRealtimeEvent;
_etwListeners.Add(provider.Guid, listener);
}
else
{
if (!provider.IsActive)
{
listener.RealtimeEvent -= OnEtwListenerRealtimeEvent;
listener.Dispose();
_etwListeners.Remove(provider.Guid);
continue;
}
}
}
}

}
}
3 changes: 0 additions & 3 deletions WpfTraceSpy/WpfSettings.cs
Expand Up @@ -26,7 +26,6 @@ public WpfSettings()
RemoveEmptyLines = true;
AutoScroll = true;
ResolveProcessName = true;
CaptureOutputDebugString = true;
Left = 50;
Top = 50;
FindLeft = 50;
Expand All @@ -43,8 +42,6 @@ public WpfSettings()
public bool ShowTooltips { get; set; }
public bool ShowEtwDescription { get; set; }
public bool AutoScroll { get; set; }
public bool CaptureEtwTraces { get; set; }
public bool CaptureOutputDebugString { get; set; }
public bool RemoveEmptyLines { get; set; }
public bool WrapText { get; set; }
public int Left { get; set; }
Expand Down

0 comments on commit e190121

Please sign in to comment.