-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleService.cs
More file actions
55 lines (44 loc) · 1.22 KB
/
ConsoleService.cs
File metadata and controls
55 lines (44 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Text;
using Avalonia.Threading;
namespace JustLauncher;
public class ConsoleService
{
private static readonly Lazy<ConsoleService> _instance = new(() => new ConsoleService());
public static ConsoleService Instance => _instance.Value;
private readonly object _lock = new();
private readonly List<string> _logs = new();
private readonly StringBuilder _buffer = new();
public string FullLog
{
get
{
lock (_lock)
{
return _buffer.ToString();
}
}
}
public event Action<string>? MessageLogged;
private ConsoleService() { }
public void Log(string message)
{
string timestamped = $"[{DateTime.Now:HH:mm:ss}] {message}";
lock (_lock)
{
_logs.Add(timestamped);
_buffer.AppendLine(timestamped);
}
Dispatcher.UIThread.Post(() => MessageLogged?.Invoke(timestamped));
}
public void Clear()
{
lock (_lock)
{
_logs.Clear();
_buffer.Clear();
}
Dispatcher.UIThread.Post(() => MessageLogged?.Invoke(null!));
}
}