Skip to content

Commit

Permalink
better log
Browse files Browse the repository at this point in the history
  • Loading branch information
exp111 committed Nov 8, 2023
1 parent cc6235e commit 0f25b96
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 37 deletions.
33 changes: 20 additions & 13 deletions Turbulence.Core/ViewModels/Design/DesignLogViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
namespace Turbulence.Core.ViewModels.Design
using Turbulence.Discord.Services;

namespace Turbulence.Core.ViewModels.Design;

public class DesignLogViewModel : LogViewModel
{
public class DesignLogViewModel : LogViewModel
Array types = Enum.GetValues(typeof(LogType));
Array levels = Enum.GetValues(typeof(LogLevel));
Random random = new Random();

public DesignLogViewModel()
{
public DesignLogViewModel()
Logs.AddRange(new LogEntry[]
{
Logs.AddRange(new string[]
{
"[CDN] Request 1",
"[API] Request 2",
});
}
new("Request 1", LogType.Images, LogLevel.Debug, DateTime.Now),
new("Request 2", LogType.Networking, LogLevel.Info, DateTime.Now),
});
}

public override void Refresh()
{
Logs.Add($"[REFRESH] New Request {Logs.Count}");
}
public override void Refresh()
{
var type = (LogType)types.GetValue(random.Next(types.Length))!;
var lvl = (LogLevel)levels.GetValue(random.Next(levels.Length))!;
Logs.Add(new($"New Request {Logs.Count}", type, lvl, DateTime.Now));
}
}
2 changes: 1 addition & 1 deletion Turbulence.Core/ViewModels/LogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Turbulence.Core.ViewModels;
public partial class LogViewModel : ViewModelBase
{
private readonly ILogger _logger = Ioc.Default.GetService<ILogger>()!;
public ObservableList<string> Logs { get; } = new();
public ObservableList<LogEntry> Logs { get; } = new();

public LogViewModel()
{
Expand Down
35 changes: 23 additions & 12 deletions Turbulence.Desktop/LogWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Turbulence.Core.ViewModels;assembly=Turbulence.Core"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="500"
Width="600" Height="500"
xmlns:model="using:Turbulence.Discord.Services"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="500"
Width="800" Height="500"
MinWidth="600" MinHeight="500"
WindowStartupLocation="CenterOwner"
x:Class="Turbulence.Desktop.LogWindow"
Expand All @@ -17,16 +18,26 @@
<DockPanel>
<Menu DockPanel.Dock="Top" Background="#23272A">
<MenuItem Header="Refresh" Command="{Binding RefreshCommand}" />
<!--TODO: add filter-->
</Menu>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Logs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding .}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="5">
<!--TODO: Make these do something-->
<ComboBox Name="LevelBox">
<ComboBoxItem>Debug</ComboBoxItem>
<ComboBoxItem IsSelected="True">Info</ComboBoxItem>
<ComboBoxItem>Warning</ComboBoxItem>
</ComboBox>
<RadioButton GroupName="Types" IsChecked="True">All</RadioButton>
<RadioButton GroupName="Types">Application</RadioButton>
<RadioButton GroupName="Types">Networking</RadioButton>
<RadioButton GroupName="Types">Images</RadioButton>
</StackPanel>
<DataGrid Name="EventList" ItemsSource="{Binding Logs}" IsReadOnly="True" Focusable="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Timestamp" Width="Auto" Binding="{Binding Timestamp}" />
<DataGridTextColumn Header="Level" Width="Auto" Binding="{Binding Level}" />
<DataGridTextColumn Header="Type" Width="Auto" Binding="{Binding Type}" />
<DataGridTextColumn Header="Message" Width="*" Binding="{Binding Message}" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>
1 change: 1 addition & 0 deletions Turbulence.Desktop/Program.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<Application.Styles>
<FluentTheme />
<StyleInclude Source="Style/Style.axaml" />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
</Application.Styles>
</Application>
1 change: 1 addition & 0 deletions Turbulence.Desktop/Turbulence.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.5" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.5" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.5" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.5" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Turbulence.Discord/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public async Task<byte[]> GetAvatarAsync(User user, int size = 128)
{
avatar = await Api.GetAvatarAsync(CdnClient, user, size);
}
_logger?.Log($"[CDN] Requested avatar for user {user.Id}");
_logger?.Log($"Requested avatar for user {user.Id}", LogType.Images, LogLevel.Debug);

_cache.SetAvatar(user.Id, size, avatar);
return avatar;
Expand All @@ -458,7 +458,7 @@ public async Task<byte[]> GetEmojiAsync(Emoji emoji, int size = 32)
return img;

img = await Api.GetEmojiAsync(CdnClient, emoji, size);
_logger?.Log($"[CDN] Requested emoji with ID {emoji.Id}");
_logger?.Log($"Requested emoji with ID {emoji.Id}", LogType.Images, LogLevel.Debug);

_cache.SetEmoji(emoji.Id, size, img);
return img;
Expand Down
35 changes: 26 additions & 9 deletions Turbulence.Discord/Services/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,42 @@

public interface ILogger
{
public void Log(string message);
public IEnumerable<string> GetLogs();
public void Log(string message, LogType type, LogLevel level = LogLevel.Info);
public IEnumerable<LogEntry> GetLogs();
}

public enum LogType
{
Application,
Networking,
Images
}

public enum LogLevel
{
Debug,
Info,
Warning,
}

public record LogEntry(string Message, LogType Type, LogLevel Level, DateTime Timestamp);

public class Logger : ILogger
{
//TODO: add logging severity/level
//TODO: add logging type
//TODO: add timestamp
private readonly List<string> _log = new();
private readonly List<LogEntry> _log = new();

public Logger()
{
Log("Logger started", LogType.Application, LogLevel.Info);
}

public IEnumerable<string> GetLogs()
public IEnumerable<LogEntry> GetLogs()
{
return _log;
}

public void Log(string message)
public void Log(string message, LogType type, LogLevel level = LogLevel.Info)
{
_log.Add(message);
_log.Add(new(message, type, level, DateTime.Now));
}
}

0 comments on commit 0f25b96

Please sign in to comment.