Files
Aaru/Aaru.Gui/ViewModels/Dialogs/ConsoleViewModel.cs

153 lines
5.9 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ConsoleViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the console dialog.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
2025-08-20 21:19:43 +01:00
using System.Windows.Input;
using Aaru.CommonTypes.Interop;
using Aaru.Localization;
using Aaru.Logging;
using Avalonia.Platform.Storage;
2025-08-20 21:19:43 +01:00
using CommunityToolkit.Mvvm.Input;
2023-09-26 01:29:07 +01:00
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using Console = Aaru.Gui.Views.Dialogs.Console;
using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
using Version = Aaru.CommonTypes.Interop.Version;
namespace Aaru.Gui.ViewModels.Dialogs;
2022-03-06 13:29:38 +00:00
public sealed class ConsoleViewModel : ViewModelBase
{
readonly Console _view;
bool _debugChecked;
2022-03-06 13:29:38 +00:00
public ConsoleViewModel(Console view)
{
2022-03-06 13:29:38 +00:00
_view = view;
2025-08-20 21:19:43 +01:00
SaveCommand = new AsyncRelayCommand(SaveAsync);
ClearCommand = new RelayCommand(Clear);
2022-03-06 13:29:38 +00:00
}
2025-08-20 21:19:43 +01:00
public ICommand ClearCommand { get; }
public ICommand SaveCommand { get; }
2022-03-06 13:29:38 +00:00
public ObservableCollection<LogEntry> Entries => ConsoleHandler.Entries;
2023-10-03 23:27:57 +01:00
2022-03-06 13:29:38 +00:00
public bool DebugChecked
{
get => _debugChecked;
set
{
2022-03-06 13:29:38 +00:00
ConsoleHandler.Debug = value;
2025-08-20 21:19:43 +01:00
SetProperty(ref _debugChecked, value);
}
2022-03-06 13:29:38 +00:00
}
2025-08-20 21:19:43 +01:00
async Task SaveAsync()
2022-03-06 13:29:38 +00:00
{
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
2025-10-25 13:05:42 +01:00
FileTypeChoices = [FilePickerFileTypes.Log]
2022-03-06 13:29:38 +00:00
});
2024-05-01 04:05:22 +01:00
if(result is null) return;
2022-03-06 13:29:38 +00:00
try
{
var logFs = new FileStream(result.Path.AbsolutePath, FileMode.Create, FileAccess.ReadWrite);
2022-03-06 13:29:38 +00:00
var logSw = new StreamWriter(logFs);
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(string.Format(UI.Log_saved_at_0, DateTime.Now));
2022-03-06 13:29:38 +00:00
PlatformID platId = DetectOS.GetRealPlatformID();
string platVer = DetectOS.GetVersion();
2022-03-06 13:29:38 +00:00
var assemblyVersion =
2025-08-17 06:11:22 +01:00
Attribute.GetCustomAttribute(typeof(AaruLogging).Assembly,
2022-03-06 13:29:38 +00:00
typeof(AssemblyInformationalVersionAttribute)) as
AssemblyInformationalVersionAttribute;
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(Localization.Core.System_information);
await
logSw.WriteLineAsync($"{DetectOS.GetPlatformName(platId, platVer)} {platVer} ({(Environment.Is64BitOperatingSystem ? 64 : 32)}-bit)");
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync($".NET Core {Version.GetNetCoreVersion()}");
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync();
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(Localization.Core.Program_information);
await logSw.WriteLineAsync($"Aaru {assemblyVersion?.InformationalVersion}");
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(string.Format(Localization.Core.Running_in_0_bit,
Environment.Is64BitProcess ? 64 : 32));
2024-05-01 04:05:22 +01:00
#if DEBUG
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(Localization.Core.DEBUG_version);
2024-05-01 04:05:22 +01:00
#endif
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(string.Format(Localization.Core.Command_line_0, Environment.CommandLine));
await logSw.WriteLineAsync();
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync(UI.Console_with_ornament);
2022-03-06 13:29:38 +00:00
foreach(LogEntry entry in ConsoleHandler.Entries)
2023-10-03 23:27:57 +01:00
{
if(entry.Type != UI.LogEntry_Type_Info)
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync($"{entry.Timestamp}: ({entry.Type.ToLower()}) {entry.Message}");
2022-03-06 13:29:38 +00:00
else
2025-10-25 13:05:42 +01:00
await logSw.WriteLineAsync($"{entry.Timestamp}: {entry.Message}");
2023-10-03 23:27:57 +01:00
}
2022-03-06 13:29:38 +00:00
logSw.Close();
logFs.Close();
}
2022-03-06 13:29:38 +00:00
catch(Exception exception)
{
2023-09-26 01:29:07 +01:00
await MessageBoxManager.GetMessageBoxStandard(UI.Title_Error,
2024-05-01 04:05:22 +01:00
string
.Format(UI
.Exception_0_trying_to_save_logfile_details_has_been_sent_to_console,
exception.Message),
ButtonEnum.Ok,
Icon.Error)
.ShowWindowDialogAsync(_view);
2025-08-20 21:19:43 +01:00
AaruLogging.Exception(exception,
UI.Exception_0_trying_to_save_logfile_details_has_been_sent_to_console,
exception.Message);
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
2025-08-20 21:19:43 +01:00
static void Clear() => ConsoleHandler.Entries.Clear();
}