Files
NebulaLauncher/Nebula.Launcher/Models/ContentLogConsumer.cs

60 lines
1.5 KiB
C#
Raw Normal View History

2025-12-11 21:47:54 +03:00
using System;
using System.Collections.Generic;
2025-07-02 21:32:51 +03:00
using Nebula.Launcher.ProcessHelper;
using Nebula.Launcher.ViewModels.Popup;
using Nebula.Shared.Services;
namespace Nebula.Launcher.Models;
public sealed class ContentLogConsumer : IProcessLogConsumer
{
2025-12-11 21:47:54 +03:00
private readonly List<string> _outMessages = [];
private LogPopupModelView? _currentLogPopup;
public int MaxMessages { get; set; } = 100;
2025-07-02 21:32:51 +03:00
2025-12-11 21:47:54 +03:00
public void Popup(PopupMessageService popupMessageService)
2025-07-02 21:32:51 +03:00
{
2025-12-11 21:47:54 +03:00
if(_currentLogPopup is not null)
return;
_currentLogPopup = new LogPopupModelView(popupMessageService);
_currentLogPopup.OnDisposing += OnLogPopupDisposing;
foreach (var message in _outMessages.ToArray())
{
_currentLogPopup.Append(message);
}
popupMessageService.Popup(_currentLogPopup);
}
private void OnLogPopupDisposing(PopupViewModelBase obj)
{
if(_currentLogPopup == null)
return;
_currentLogPopup.OnDisposing -= OnLogPopupDisposing;
_currentLogPopup = null;
2025-07-02 21:32:51 +03:00
}
public void Out(string text)
{
2025-12-11 21:47:54 +03:00
_outMessages.Add(text);
if(_outMessages.Count >= MaxMessages)
_outMessages.RemoveAt(0);
_currentLogPopup?.Append(text);
2025-07-02 21:32:51 +03:00
}
public void Error(string text)
{
2025-12-11 21:47:54 +03:00
Out(text);
2025-07-02 21:32:51 +03:00
}
public void Fatal(string text)
{
2025-12-11 21:47:54 +03:00
throw new Exception("Error while running programm: " + text);
2025-07-02 21:32:51 +03:00
}
}