Skip to content

Commit 9d808ae

Browse files
committed
Changed log display in console tab
1 parent ef26472 commit 9d808ae

5 files changed

Lines changed: 110 additions & 41 deletions

File tree

ArkBot/Modules/Application/Controls/Console.xaml

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,42 @@
1717
<RowDefinition Height="*" />
1818
</Grid.RowDefinitions>
1919
<ScrollViewer autoscroll:AutoScrollBehavior.AutoScroll="True">
20-
<StackPanel>
21-
<ItemsControl ItemsSource="{Binding ConsoleOutput, Mode=OneWay}">
22-
<ItemsControl.ItemTemplate>
23-
<DataTemplate>
24-
<TextBlock Text="{Binding Path=.}" FontFamily="Consolas" TextWrapping="Wrap" />
25-
</DataTemplate>
26-
</ItemsControl.ItemTemplate>
27-
</ItemsControl>
28-
<!--<TextBox Text="{Binding ConsoleInput, Mode=TwoWay}" Background="Black" Foreground="White" FontFamily="Consolas" Name="InputBlock" BorderBrush="{x:Null}" SelectionBrush="{x:Null}" />-->
29-
</StackPanel>
20+
<ListBox ItemsSource="{Binding ConsoleOutput, Mode=OneWay}" SelectionMode="Extended" AlternationCount="2">
21+
<ListBox.Resources>
22+
<Style TargetType="{x:Type ListBoxItem}">
23+
<Style.Triggers>
24+
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
25+
<Setter Property="Background" Value="#ffffff"></Setter>
26+
</Trigger>
27+
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
28+
<Setter Property="Background" Value="#f0f0f0"></Setter>
29+
</Trigger>
30+
</Style.Triggers>
31+
</Style>
32+
</ListBox.Resources>
33+
<ListBox.Template>
34+
<ControlTemplate>
35+
<ItemsPresenter />
36+
</ControlTemplate>
37+
</ListBox.Template>
38+
<ListBox.ItemsPanel>
39+
<ItemsPanelTemplate>
40+
<WrapPanel Orientation="Vertical" IsItemsHost="True" />
41+
</ItemsPanelTemplate>
42+
</ListBox.ItemsPanel>
43+
<ListBox.ItemTemplate>
44+
<DataTemplate>
45+
<Grid>
46+
<Grid.ColumnDefinitions>
47+
<ColumnDefinition Width="Auto" />
48+
<ColumnDefinition Width="*" />
49+
</Grid.ColumnDefinitions>
50+
<TextBlock Grid.Column="0" Text="{Binding Path=When, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}" FontFamily="Consolas" Margin="0,0,15,0" />
51+
<TextBlock Grid.Column="1" Text="{Binding Path=Message}" FontFamily="Consolas" TextWrapping="Wrap" Foreground="{Binding Path=Color}" />
52+
</Grid>
53+
</DataTemplate>
54+
</ListBox.ItemTemplate>
55+
</ListBox>
3056
</ScrollViewer>
3157
</Grid>
3258
</UserControl>

ArkBot/Modules/Application/ViewModel/ConsoleViewModel.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using System.Collections.ObjectModel;
1+
using ArkBot.Utils.Extensions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Collections.Specialized;
6+
using System.ComponentModel;
7+
using System.Linq;
28
using System.Threading.Tasks;
39
using System.Windows;
410
using System.Windows.Controls;
@@ -7,11 +13,11 @@ namespace ArkBot.Modules.Application.ViewModel
713
{
814
public sealed class ConsoleViewModel : TabViewModel
915
{
10-
public ObservableCollection<string> ConsoleOutput { get; set; }
16+
public ObservableCollection<ConsoleLogEntry> ConsoleOutput { get; set; }
1117

1218
private ConsoleViewModel() : base("Console", "Console")
1319
{
14-
ConsoleOutput = new ObservableCollection<string>();
20+
ConsoleOutput = new ObservableCollection<ConsoleLogEntry>();
1521
}
1622

1723
private async Task<ConsoleViewModel> InitializeAsync()
@@ -25,15 +31,40 @@ public static Task<ConsoleViewModel> CreateAsync(bool isVisible = false)
2531
return ret.InitializeAsync();
2632
}
2733

28-
public void AddLog(string message)
34+
public void AddLog(string message, System.Windows.Media.Brush color = null)
2935
{
3036
if (message == null) return;
3137

3238
System.Windows.Application.Current?.Dispatcher.Invoke(delegate
3339
{
34-
ConsoleOutput.Add(message.TrimEnd('\n', '\r'));
40+
while (ConsoleOutput.Count >= 1000) ConsoleOutput.RemoveAt(0);
41+
ConsoleOutput.Add(new ConsoleLogEntry(message.TrimEnd('\n', '\r'), color));
3542
});
3643
}
44+
45+
public void AddLogError(string message)
46+
{
47+
AddLog(message, System.Windows.Media.Brushes.Red);
48+
}
49+
50+
public void AddLogWarning(string message)
51+
{
52+
AddLog(message, System.Windows.Media.Brushes.Orange);
53+
}
54+
}
55+
56+
public class ConsoleLogEntry
57+
{
58+
public ConsoleLogEntry(string message, System.Windows.Media.Brush color = null)
59+
{
60+
When = DateTime.Now;
61+
Message = message;
62+
Color = color ?? System.Windows.Media.Brushes.Black;
63+
}
64+
65+
public DateTime When { get; set; }
66+
public string Message { get; set; }
67+
public System.Windows.Media.Brush Color { get; set; }
3768
}
3869

3970
public static class AutoScrollBehavior

ArkBot/Modules/Application/ViewModel/Workspace.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ internal async Task Init()
259259
}
260260
catch (Exception ex)
261261
{
262-
Console.AddLog($@"Error loading 'ark.config'. Using default config. (""{ex.Message}"")");
262+
Console.AddLogError($@"Error loading 'ark.config'. Using default config. (""{ex.Message}"")");
263263
Logging.LogException("Error loading 'ark.config'. Using default config.", ex, GetType());
264264
}
265265
}
@@ -427,7 +427,7 @@ internal async Task Init()
427427
}
428428
catch (SqlException ex)
429429
{
430-
Console.AddLog($@"Error initializing Microsoft SQL Server (""{ex.Message}"")");
430+
Console.AddLogError($@"Error initializing Microsoft SQL Server (""{ex.Message}"")");
431431
Logging.LogException("Error initializing Microsoft SQL Server.", ex, GetType());
432432
return;
433433
}
@@ -635,7 +635,7 @@ internal async Task Init()
635635
}
636636
catch (Exception ex)
637637
{
638-
Console.AddLog($@"SSL Certificate request failed! (""{ex.Message}"")");
638+
Console.AddLogError($@"SSL Certificate request failed! (""{ex.Message}"")");
639639
Logging.LogException("Failed to issue ssl certificate.", ex, GetType());
640640
}
641641
}
@@ -710,8 +710,8 @@ internal async Task Init()
710710
}
711711
catch (Exception) { /* do nothing */ }
712712

713-
Console.AddLog($@"Failed to start web app! (""{ex.Message}"")");
714-
if (!string.IsNullOrWhiteSpace(portInUseBy)) Console.AddLog(portInUseBy);
713+
Console.AddLogError($@"Failed to start web app! (""{ex.Message}"")");
714+
if (!string.IsNullOrWhiteSpace(portInUseBy)) Console.AddLogError(portInUseBy);
715715
Logging.LogException("Failed to start web app.", ex, GetType());
716716
}
717717
}
@@ -915,14 +915,14 @@ public async Task RunDiscordBot()
915915
try
916916
{
917917
lastAttempt = DateTime.Now;
918-
System.Console.WriteLine("Connecting bot...");
918+
System.Console.WriteLine("Connecting discord bot...");
919919
await _bot.Start();
920-
System.Console.WriteLine("Connected!");
920+
System.Console.WriteLine("Discord bot connected!");
921921
isConnected = true;
922922
}
923923
catch (Exception ex)
924924
{
925-
System.Console.WriteLine($"Failed to connect ({ex.Message})! Will retry in a moment...");
925+
System.Console.WriteLine($"Discord bot failed to connect ({ex.Message})! Will retry in a moment...");
926926
Logging.LogException("Failed to start Discord Bot", ex, GetType(), Utils.LogLevel.DEBUG, ExceptionLevel.Ignored);
927927
}
928928
}

ArkBot/Modules/WebApp/WebAppLogger.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
102102
{
103103
if (!_logLevels.TryGetValue(logLevel, out var internalLogLevel)) internalLogLevel = Utils.LogLevel.INFO;
104104

105-
Application.ViewModel.Workspace.Instance.Console.AddLog(@$"{message} (""{exception.Message}"")");
105+
Application.ViewModel.Workspace.Instance.Console.AddLog(@$"{message} (""{exception.Message}"")", System.Windows.Media.Brushes.Red);
106106
Utils.Logging.LogException(message, exception, GetType(), internalLogLevel);
107107
}
108108
else
109109
{
110-
Application.ViewModel.Workspace.Instance.Console.AddLog(message);
110+
if (logLevel == LogLevel.Warning) Application.ViewModel.Workspace.Instance.Console.AddLogWarning(message);
111+
else if (logLevel >= LogLevel.Error) Application.ViewModel.Workspace.Instance.Console.AddLogError(message);
112+
else Application.ViewModel.Workspace.Instance.Console.AddLog(message);
111113
}
112114
}
113115
}

ArkBot/Utils/Helpers/ProcessHelper.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public static async Task<ProcessResult> RunCommandLineTool(string executableAbso
123123
}
124124
var ss = new AutoResetEvent(false);
125125
var tcs = new TaskCompletionSource<int>();
126+
var readFunc = (Func<bool>)null;
126127
process = new Process
127128
{
128129
StartInfo = si,
@@ -145,28 +146,36 @@ public static async Task<ProcessResult> RunCommandLineTool(string executableAbso
145146
var offset = 0L;
146147
powershellOutputStream = new FileStream(tmpFilePathToPowershellOutput, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
147148
powershellOutputStreamReader = new StreamReader(powershellOutputStream);
148-
var task = Task.Run(async () =>
149+
150+
readFunc = new Func<bool>(() =>
149151
{
150-
while (true)
152+
powershellOutputStream.Seek(offset, SeekOrigin.Begin);
153+
if (!powershellOutputStreamReader.EndOfStream)
151154
{
152-
if (tcs.Task.IsCompleted) return;
153-
154-
powershellOutputStream.Seek(offset, SeekOrigin.Begin);
155-
if (!powershellOutputStreamReader.EndOfStream)
155+
do
156156
{
157-
do
158-
{
159-
var line = powershellOutputStreamReader.ReadLine();
157+
var line = powershellOutputStreamReader.ReadLine();
158+
159+
if (line == null) continue;
160160

161-
if (line == null) continue;
161+
sb.AppendLine(line);
162+
onOutputLineRead?.Invoke(line);
163+
} while (!powershellOutputStreamReader.EndOfStream);
162164

163-
sb.AppendLine(line);
164-
onOutputLineRead?.Invoke(line);
165-
} while (!powershellOutputStreamReader.EndOfStream);
165+
offset = powershellOutputStream.Position;
166+
167+
return true;
168+
}
169+
else return false;
170+
});
171+
172+
var task = Task.Run(async () =>
173+
{
174+
while (true)
175+
{
176+
if (tcs.Task.IsCompleted) return;
166177

167-
offset = powershellOutputStream.Position;
168-
}
169-
else await Task.Delay(100);
178+
if (!readFunc()) await Task.Delay(100);
170179
}
171180
});
172181
}
@@ -183,6 +192,7 @@ public static async Task<ProcessResult> RunCommandLineTool(string executableAbso
183192

184193
result = await tcs.Task;
185194
timer.Change(0, Timeout.Infinite);
195+
readFunc?.Invoke();
186196
if (result != 0)
187197
{
188198
return (false, sb.ToString());

0 commit comments

Comments
 (0)