Skip to content

Commit

Permalink
quality logging
Browse files Browse the repository at this point in the history
  • Loading branch information
exp111 committed Nov 7, 2023
1 parent f7bfe79 commit cc6235e
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 10 deletions.
19 changes: 19 additions & 0 deletions Turbulence.Core/ViewModels/Design/DesignLogViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Turbulence.Core.ViewModels.Design
{
public class DesignLogViewModel : LogViewModel
{
public DesignLogViewModel()
{
Logs.AddRange(new string[]
{
"[CDN] Request 1",
"[API] Request 2",
});
}

public override void Refresh()
{
Logs.Add($"[REFRESH] New Request {Logs.Count}");
}
}
}
22 changes: 22 additions & 0 deletions Turbulence.Core/ViewModels/LogViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using Turbulence.Discord.Services;

namespace Turbulence.Core.ViewModels;

public partial class LogViewModel : ViewModelBase
{
private readonly ILogger _logger = Ioc.Default.GetService<ILogger>()!;
public ObservableList<string> Logs { get; } = new();

public LogViewModel()
{
Refresh();
}

[RelayCommand]
public virtual void Refresh()
{
Logs.ReplaceAll(_logger.GetLogs());
}
}
32 changes: 32 additions & 0 deletions Turbulence.Desktop/LogWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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"
MinWidth="600" MinHeight="500"
WindowStartupLocation="CenterOwner"
x:Class="Turbulence.Desktop.LogWindow"
x:DataType="vm:LogViewModel"
Title="Logs"
Background="#313338">
<Window.DataContext>
<vm:LogViewModel />
</Window.DataContext>
<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>
</DockPanel>
</Window>
17 changes: 17 additions & 0 deletions Turbulence.Desktop/LogWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia.Controls;
using Turbulence.Core.ViewModels.Design;

namespace Turbulence.Desktop;

public partial class LogWindow : Window
{
public LogWindow()
{
InitializeComponent();
if (Design.IsDesignMode)
{
// Workaround to fix design data context getting overwritten
DataContext = new DesignLogViewModel();
}
}
}
3 changes: 2 additions & 1 deletion Turbulence.Desktop/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
<vm:MainWindowViewModel />
</Window.DataContext>
<DockPanel>
<Menu DockPanel.Dock="Top" Background="#23272a">
<Menu DockPanel.Dock="Top" Background="#23272A">
<MenuItem Header="_File">
<MenuItem Header="_Connect" Click="OnConnect" />
<MenuItem Header="_Settings" Click="OnSettings" />
<MenuItem Header="_Open Logs" Click="OnLogs" />
<MenuItem Header="_Open Dev Tools" Click="OnDevTools" IsVisible="{Binding IsDebug}" />
<Separator />
<MenuItem Header="_Exit" Click="OnExit" />
Expand Down
8 changes: 8 additions & 0 deletions Turbulence.Desktop/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Turbulence.Desktop;
public partial class MainWindow : Window
{
private SettingsWindow? _settingsWindow;
private LogWindow? _logWindow;
private MainWindowViewModel _vm;
public MainWindow()
{
Expand All @@ -29,6 +30,13 @@ public async void OnSettings(object? _1, RoutedEventArgs _2)
_settingsWindow = new SettingsWindow();
await _settingsWindow.ShowDialog(this);
}

public async void OnLogs(object? _1, RoutedEventArgs _2)
{
// TODO: instead show as a independent window?
_logWindow = new LogWindow();
await _logWindow.ShowDialog(this);
}

public void OnDevTools(object? _1, RoutedEventArgs _2)
{
Expand Down
1 change: 1 addition & 0 deletions Turbulence.Desktop/Program.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private static AppBuilder BuildAvaloniaApp()
.AddSingleton<IPlatformClient, Client>()
.AddSingleton<ICache, Cache>()
.AddSingleton<ITypingStorage, TypingStorage>()
.AddSingleton<ILogger, Logger>()
.BuildServiceProvider();
Ioc.Default.ConfigureServices(provider);

Expand Down
11 changes: 5 additions & 6 deletions Turbulence.Desktop/SettingsWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Avalonia.Controls;

namespace Turbulence.Desktop
namespace Turbulence.Desktop;

public partial class SettingsWindow : Window
{
public partial class SettingsWindow : Window
public SettingsWindow()
{
public SettingsWindow()
{
InitializeComponent();
}
InitializeComponent();
}
}
1 change: 0 additions & 1 deletion Turbulence.Discord/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Turbulence.Discord.Models.DiscordGateway;
using Turbulence.Discord.Models.DiscordGuild;
using Turbulence.Discord.Models.DiscordUser;
using Turbulence.Discord.Services;

namespace Turbulence.Discord;

Expand Down
8 changes: 6 additions & 2 deletions Turbulence.Discord/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Client : IPlatformClient
private static readonly HttpClient CdnClient = new();

private readonly ICache _cache = Ioc.Default.GetService<ICache>()!;
private readonly ILogger? _logger = Ioc.Default.GetService<ILogger>();

private ClientWebSocket WebSocket { get; set; }
private const string UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0"; // idk where to move this

Expand Down Expand Up @@ -441,7 +443,8 @@ 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}");

_cache.SetAvatar(user.Id, size, avatar);
return avatar;
}
Expand All @@ -455,7 +458,8 @@ 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}");

_cache.SetEmoji(emoji.Id, size, img);
return img;
}
Expand Down
26 changes: 26 additions & 0 deletions Turbulence.Discord/Services/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Turbulence.Discord.Services;

public interface ILogger
{
public void Log(string message);
public IEnumerable<string> GetLogs();
}


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

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

public void Log(string message)
{
_log.Add(message);
}
}

0 comments on commit cc6235e

Please sign in to comment.