Files
Aaru/Aaru.Core/Logging/DumpLog.cs

199 lines
7.6 KiB
C#
Raw Normal View History

2017-11-20 05:07:16 +00:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2017-11-20 05:07:16 +00:00
// ----------------------------------------------------------------------------
//
// Filename : DumpLog.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
2017-11-20 05:07:16 +00:00
//
// Component : Core algorithms.
2017-11-20 05:07:16 +00:00
//
// --[ Description ] ----------------------------------------------------------
//
// Contains glue logic for writing a dump log.
2017-11-20 05:07:16 +00:00
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2017-11-20 05:07:16 +00:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Core.Logging;
2017-11-20 05:07:16 +00:00
using System;
using System.IO;
using System.Reflection;
2019-07-15 15:33:46 +01:00
using System.Runtime.InteropServices;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interop;
using Aaru.Devices;
using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
using Version = Aaru.CommonTypes.Interop.Version;
2017-12-19 19:33:46 +00:00
2022-03-06 13:29:38 +00:00
/// <summary>Creates a dump log</summary>
public sealed class DumpLog
2017-11-20 05:07:16 +00:00
{
2022-03-06 13:29:38 +00:00
readonly StreamWriter _logSw;
/// <summary>Initializes the dump log</summary>
/// <param name="outputFile">Output log file</param>
/// <param name="dev">Device</param>
/// <param name="private">Disable saving paths or serial numbers in log</param>
public DumpLog(string outputFile, Device dev, bool @private)
2017-11-20 05:07:16 +00:00
{
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(outputFile))
return;
2017-11-20 05:07:16 +00:00
2022-03-06 13:29:38 +00:00
_logSw = new StreamWriter(outputFile, true);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("Start logging at {0}", DateTime.Now);
2017-11-20 05:07:16 +00:00
2022-03-06 13:29:38 +00:00
PlatformID platId = DetectOS.GetRealPlatformID();
string platVer = DetectOS.GetVersion();
2017-11-20 05:07:16 +00:00
2022-03-06 13:29:38 +00:00
var assemblyVersion =
Attribute.GetCustomAttribute(typeof(DumpLog).Assembly, typeof(AssemblyInformationalVersionAttribute)) as
AssemblyInformationalVersionAttribute;
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("################# System information #################");
2017-11-20 05:07:16 +00:00
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("{0} {1} ({2}-bit)", DetectOS.GetPlatformName(platId, platVer), platVer,
Environment.Is64BitOperatingSystem ? 64 : 32);
2022-03-06 13:29:38 +00:00
if(DetectOS.IsMono)
_logSw.WriteLine("Mono {0}", Version.GetMonoVersion());
else if(DetectOS.IsNetCore)
_logSw.WriteLine(".NET Core {0}", Version.GetNetCoreVersion());
else
_logSw.WriteLine(RuntimeInformation.FrameworkDescription);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine();
2017-12-29 01:26:58 +00:00
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("################# Program information ################");
_logSw.WriteLine("Aaru {0}", assemblyVersion?.InformationalVersion);
_logSw.WriteLine("Running in {0}-bit", Environment.Is64BitProcess ? 64 : 32);
_logSw.WriteLine("Running as superuser: {0}", DetectOS.IsAdmin ? "Yes" : "No");
#if DEBUG
_logSw.WriteLine("DEBUG version");
#endif
if(@private)
{
string[] args = Environment.GetCommandLineArgs();
2017-11-20 05:07:16 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < args.Length; i++)
{
2022-03-06 13:29:38 +00:00
if(args[i].StartsWith("/dev", StringComparison.OrdinalIgnoreCase) ||
args[i].StartsWith("aaru://", StringComparison.OrdinalIgnoreCase))
continue;
2022-03-06 13:29:38 +00:00
try
{
2022-03-06 13:29:38 +00:00
args[i] = Path.GetFileName(args[i]);
}
catch
{
// Do nothing
}
}
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("Command line: {0}", string.Join(" ", args));
}
else
_logSw.WriteLine("Command line: {0}", Environment.CommandLine);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine();
if(dev is Aaru.Devices.Remote.Device remoteDev)
2022-03-06 13:29:38 +00:00
{
_logSw.WriteLine("################# Remote information #################");
_logSw.WriteLine("Server: {0}", remoteDev.RemoteApplication);
_logSw.WriteLine("Version: {0}", remoteDev.RemoteVersion);
2019-10-21 23:36:57 +01:00
_logSw.WriteLine("Operating system: {0} {1}", remoteDev.RemoteOperatingSystem,
remoteDev.RemoteOperatingSystemVersion);
_logSw.WriteLine("Architecture: {0}", remoteDev.RemoteArchitecture);
_logSw.WriteLine("Protocol version: {0}", remoteDev.RemoteProtocolVersion);
_logSw.WriteLine("Running as superuser: {0}", remoteDev.IsAdmin ? "Yes" : "No");
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("######################################################");
}
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("################# Device information #################");
_logSw.WriteLine("Manufacturer: {0}", dev.Manufacturer);
_logSw.WriteLine("Model: {0}", dev.Model);
_logSw.WriteLine("Firmware revision: {0}", dev.FirmwareRevision);
2022-03-06 13:29:38 +00:00
if(!@private)
_logSw.WriteLine("Serial number: {0}", dev.Serial);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("Removable device: {0}", dev.IsRemovable);
_logSw.WriteLine("Device type: {0}", dev.Type);
_logSw.WriteLine("CompactFlash device: {0}", dev.IsCompactFlash);
_logSw.WriteLine("PCMCIA device: {0}", dev.IsPcmcia);
_logSw.WriteLine("USB device: {0}", dev.IsUsb);
2022-03-06 13:29:38 +00:00
if(dev.IsUsb)
{
_logSw.WriteLine("USB manufacturer: {0}", dev.UsbManufacturerString);
_logSw.WriteLine("USB product: {0}", dev.UsbProductString);
2017-12-29 01:26:58 +00:00
2022-03-06 13:29:38 +00:00
if(!@private)
_logSw.WriteLine("USB serial: {0}", dev.UsbSerialString);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("USB vendor ID: {0:X4}h", dev.UsbVendorId);
_logSw.WriteLine("USB product ID: {0:X4}h", dev.UsbProductId);
}
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("FireWire device: {0}", dev.IsFireWire);
2022-03-06 13:29:38 +00:00
if(dev.IsFireWire)
{
_logSw.WriteLine("FireWire vendor: {0}", dev.FireWireVendorName);
_logSw.WriteLine("FireWire model: {0}", dev.FireWireModelName);
2017-12-29 01:26:58 +00:00
2022-03-06 13:29:38 +00:00
if(!@private)
_logSw.WriteLine("FireWire GUID: 0x{0:X16}", dev.FireWireGuid);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("FireWire vendor ID: 0x{0:X8}", dev.FireWireVendor);
_logSw.WriteLine("FireWire product ID: 0x{0:X8}", dev.FireWireModel);
2017-11-20 05:07:16 +00:00
}
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("######################################################");
2022-03-06 13:29:38 +00:00
_logSw.WriteLine();
_logSw.WriteLine("################ Dumping progress log ################");
_logSw.Flush();
}
2017-11-20 05:07:16 +00:00
2022-03-06 13:29:38 +00:00
/// <summary>Adds a new line to the dump log</summary>
/// <param name="format">Format string</param>
/// <param name="args">Arguments</param>
public void WriteLine(string format, params object[] args)
{
if(_logSw == null)
return;
2022-03-07 07:36:44 +00:00
var text = string.Format(format, args);
2022-03-06 13:29:38 +00:00
_logSw.WriteLine("{0:s} {1}", DateTime.Now, text);
_logSw.Flush();
}
/// <summary>Finishes and closes the dump log</summary>
public void Close()
{
_logSw?.WriteLine("######################################################");
_logSw?.WriteLine("End logging at {0}", DateTime.Now);
_logSw?.Close();
2017-11-20 05:07:16 +00:00
}
2017-12-19 20:33:03 +00:00
}