Skip to content

Commit d088136

Browse files
blackspherefollowerqdot
authored andcommitted
Improving the Log tab
Fixes #190 by fefaulting to 1000 entries. This can be overriden in the %APPDATA%\Buttplug\config.json file: ``` { "buttplug": { "server": { "port": "12345", "secure": "False", }, "log": { "max": "5000" } } } ``` Fixes #185 by allowing multi-line selection and handling Ctrl+C to copy the log entries to the clipboard. Fixes #187 by adding a Clear Log button, which clears the log list.
1 parent c7fba82 commit d088136

4 files changed

Lines changed: 78 additions & 8 deletions

File tree

Buttplug.Apps.WebsocketServerGUI/MainWindow.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public MainWindow()
1717

1818
InitializeComponent();
1919

20+
long logLimit = 1000;
21+
if (long.TryParse(config.GetValue("buttplug.log.max", "1000"), out long res))
22+
{
23+
logLimit = res;
24+
}
25+
26+
ButtplugTab.GetLogControl().MaxLogs = logLimit;
27+
2028
ButtplugTab.SetServerDetails("Websocket Server", ping);
2129
_wsTab = new WebsocketServerControl(ButtplugTab);
2230
ButtplugTab.SetApplicationTab("Websocket Server", _wsTab);

Buttplug.Components.Controls/ButtplugLogControl.xaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
<Grid Background="#FFE5E5E5" >
99
<Grid.RowDefinitions>
1010
<RowDefinition Height="*"/>
11-
<RowDefinition Height="25"/>
11+
<RowDefinition Height="auto"/>
1212
</Grid.RowDefinitions>
13-
<ListBox Grid.Row="0" Name="LogListBox" Margin="10,10,10,0" />
14-
<ComboBox Name="LogLevelComboBox" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" Margin="78,3.667,0,-0.333">
13+
<ListBox Grid.Row="0" Name="LogListBox" Margin="10,10,10,0" SelectionMode="Multiple" KeyUp="LogListBox_KeyUp" />
14+
<Label Grid.Row="1" Content="Log Level:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
15+
<ComboBox Name="LogLevelComboBox" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" Margin="80,2,0,0">
1516
<ComboBoxItem Content="Off" />
1617
<ComboBoxItem Content="Fatal" />
1718
<ComboBoxItem Content="Error" />
1819
<ComboBoxItem Content="Warn"/>
1920
<ComboBoxItem Content="Info"/>
2021
<ComboBoxItem IsSelected="true" Content="Debug" />
2122
<ComboBoxItem Content="Trace"/>
22-
</ComboBox>
23-
<Label Grid.Row="1" Content="Log Level:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
24-
<Button Grid.Row="1" Content="Save To File" HorizontalAlignment="Right" Margin="403,3.667,15,0" VerticalAlignment="Top" Width="70" Click="SaveLogFileButton_Click"/>
23+
</ComboBox>
24+
<Button Grid.Row="1" Content="Clear Log" HorizontalAlignment="Right" Margin="0,5,90,0" VerticalAlignment="Top" Width="70" Click="Button_Click" />
25+
<Button Grid.Row="1" Content="Save To File" HorizontalAlignment="Right" Margin="0,5,10,0" VerticalAlignment="Top" Width="70" Click="SaveLogFileButton_Click"/>
2526
</Grid>
2627
</UserControl>

Buttplug.Components.Controls/ButtplugLogControl.xaml.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using NLog;
1111
using NLog.Config;
1212
using NLog.Targets;
13+
using System.Windows.Input;
14+
using System.Text;
1315

1416
namespace Buttplug.Components.Controls
1517
{
@@ -22,19 +24,28 @@ public sealed class ButtplugGUIMessageNLogTarget : TargetWithLayoutHeaderAndFoot
2224
{
2325
private readonly LogList _logs;
2426
private readonly Thread _winThread;
27+
public long MaxLogs;
2528

26-
public ButtplugGUIMessageNLogTarget(LogList aList, Thread aWinThread)
29+
public ButtplugGUIMessageNLogTarget(LogList aList, Thread aWinThread, long aMaxLogs = 1000)
2730
{
2831
// TODO This totally needs a mutex or something
2932
_logs = aList;
3033
_winThread = aWinThread;
34+
MaxLogs = aMaxLogs;
3135
}
3236

3337
protected override void Write(LogEventInfo aLogEvent)
3438
{
3539
try
3640
{
37-
Dispatcher.FromThread(_winThread).Invoke(() => _logs.Add(Layout.Render(aLogEvent)));
41+
Dispatcher.FromThread(_winThread).Invoke(() =>
42+
{
43+
_logs.Add(Layout.Render(aLogEvent));
44+
while (_logs.Count > MaxLogs)
45+
{
46+
_logs.RemoveAt(0);
47+
}
48+
});
3849
}
3950
catch (TaskCanceledException)
4051
{
@@ -52,6 +63,19 @@ public partial class ButtplugLogControl : IDisposable
5263
private readonly ButtplugGUIMessageNLogTarget _logTarget;
5364
private LoggingRule _outgoingLoggingRule;
5465

66+
public long MaxLogs
67+
{
68+
get
69+
{
70+
return _logTarget.MaxLogs;
71+
}
72+
73+
set
74+
{
75+
_logTarget.MaxLogs = value;
76+
}
77+
}
78+
5579
public ButtplugLogControl()
5680
{
5781
var c = LogManager.Configuration ?? new LoggingConfiguration();
@@ -129,5 +153,37 @@ private void LogLevelSelectionChangedHandler(object aSender, SelectionChangedEve
129153
LogManager.GetCurrentClassLogger().Error($"Log Level \"{level}\" is not a valid log level!");
130154
}
131155
}
156+
157+
private void Button_Click(object sender, RoutedEventArgs e)
158+
{
159+
_logs.Clear();
160+
}
161+
162+
private void LogListBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
163+
{
164+
if (sender != LogListBox)
165+
{
166+
return;
167+
}
168+
169+
if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
170+
&& e.Key == Key.C)
171+
{
172+
var builder = new StringBuilder();
173+
foreach (var item in LogListBox.SelectedItems)
174+
{
175+
if (item is string)
176+
{
177+
builder.AppendLine(item as string);
178+
}
179+
else if (item is ListBoxItem)
180+
{
181+
builder.AppendLine((item as ListBoxItem).Content as string);
182+
}
183+
}
184+
185+
Clipboard.SetText(builder.ToString());
186+
}
187+
}
132188
}
133189
}

Buttplug.Components.Controls/ButtplugTabControl.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public void SetApplicationTab(string aTabName, UserControl aTabControl)
190190
ApplicationTab.Content = aTabControl;
191191
}
192192

193+
public ButtplugLogControl GetLogControl()
194+
{
195+
return LogControl;
196+
}
197+
193198
public void SetServerDetails(string serverName, uint maxPingTime)
194199
{
195200
_serverName = serverName;

0 commit comments

Comments
 (0)