Skip to content

Commit

Permalink
fix: log update bottleneck
Browse files Browse the repository at this point in the history
  • Loading branch information
nanikit committed Mar 3, 2020
1 parent 5bcc56b commit 4f154ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
8 changes: 7 additions & 1 deletion eztrans-server/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<RowDefinition Height="40"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBox Name="TbLog" Grid.Row="1" Text="{Binding Log}" TextChanged="LogScrollDown" Margin="0 -1 0 0" />
<RichTextBox Name="TbLog" Grid.Row="1" TextChanged="LogScrollDown" Margin="0 -1 0 0" >
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down
51 changes: 30 additions & 21 deletions eztrans-server/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Threading;

namespace eztrans_server {
/// <summary>
Expand All @@ -15,13 +17,29 @@ namespace eztrans_server {
public partial class MainWindow : Window {
public MainWindow() {
var vm = new HttpServerVM();
DataContext = vm;
InitializeComponent();

PrintProgramVersion(vm);
vm.Logs.CollectionChanged += LogVmItem;
_ = vm.Restart();
}

DataContext = vm;
InitializeComponent();
private void LogVmItem(object sender, NotifyCollectionChangedEventArgs e) {
if (e.Action == NotifyCollectionChangedAction.Add) {
Log(e.NewItems[0] as string ?? "");
}
}

private void Log(string s) => Dispatcher.InvokeAsync(() => {
BlockCollection blocks = TbLog.Document.Blocks;
if (blocks.Count >= 1000) {
blocks.Remove(blocks.FirstBlock);
}
var paragraph = new Paragraph(new Run(s));
blocks.Add(paragraph);
});

private void LogScrollDown(object sender, TextChangedEventArgs e) {
double bottom = TbLog.VerticalOffset + TbLog.ViewportHeight;
bool isBottommost = bottom >= TbLog.ExtentHeight - 10;
Expand All @@ -30,10 +48,11 @@ private void LogScrollDown(object sender, TextChangedEventArgs e) {
}
}

private static void PrintProgramVersion(HttpServerVM vm) {
private void PrintProgramVersion(HttpServerVM vm) {
string build = Properties.Resources.BuildDate;
string date = $"{build.Substring(2, 2)}{build.Substring(5, 2)}{build.Substring(8, 2)}";
vm.Logs.Add($"eztrans-server v{date} by nanikit");
TbLog.Document.Blocks.Clear();
Log($"eztrans-server v{date} by nanikit");
}
}

Expand All @@ -42,12 +61,6 @@ class HttpServerVM : ViewModelBase {

public RelayCommand RestartCommand { get; private set; }

private string _Log = "";
public string Log {
get => _Log;
set => Set(ref _Log, value);
}

private string _Origin = "http://localhost:8000/";
public string Origin {
get => _Origin;
Expand All @@ -66,8 +79,6 @@ public string Title {
public HttpServerVM() {
RestartCommand = new RelayCommand(() => _ = Restart());
Logs = new ObservableCollection<string>();
Logs.CollectionChanged += MergeLogs;

SetOriginFromCommandLine();
}

Expand Down Expand Up @@ -143,15 +154,13 @@ private void OnRequest(IPEndPoint ip, string? req) {
string head = req?.Substring(0, Math.Min(40, req.Length)) ?? "";
string log = $"{datetime} {ip.Address}: {head}";

if (Logs.Count > 1000) {
Logs.RemoveAt(0);
}
Logs.Add(log);
Title = $"요청 수: {++RequestCount}";
}

private void MergeLogs(object sender, NotifyCollectionChangedEventArgs e) {
Log = string.Join("\n", Logs);
Application.Current.Dispatcher.InvokeAsync(() => {
if (Logs.Count > 1000) {
Logs.RemoveAt(0);
}
Logs.Add(log);
Title = $"요청 수: {++RequestCount}";
});
}
}
}
Expand Down

0 comments on commit 4f154ac

Please sign in to comment.