2023-12-04 14:17:13 +01:00
|
|
|
using Ryujinx.Common.Logging.Formatters;
|
2023-06-01 15:47:53 +02:00
|
|
|
using System;
|
2019-12-21 20:52:31 +01:00
|
|
|
using System.IO;
|
2020-02-12 01:18:33 +00:00
|
|
|
using System.Linq;
|
2019-02-11 23:00:32 +11:00
|
|
|
|
2023-06-01 15:47:53 +02:00
|
|
|
namespace Ryujinx.Common.Logging.Targets
|
2019-02-11 23:00:32 +11:00
|
|
|
{
|
|
|
|
|
public class FileLogTarget : ILogTarget
|
|
|
|
|
{
|
2023-06-28 18:41:38 +02:00
|
|
|
private readonly StreamWriter _logWriter;
|
2019-02-11 23:00:32 +11:00
|
|
|
private readonly ILogFormatter _formatter;
|
2023-06-28 18:41:38 +02:00
|
|
|
private readonly string _name;
|
2019-02-11 23:00:32 +11:00
|
|
|
|
2019-12-21 20:52:31 +01:00
|
|
|
string ILogTarget.Name { get => _name; }
|
|
|
|
|
|
2024-01-29 19:58:18 +01:00
|
|
|
public FileLogTarget(string name, FileStream fileStream)
|
|
|
|
|
{
|
|
|
|
|
_name = name;
|
|
|
|
|
_logWriter = new StreamWriter(fileStream);
|
|
|
|
|
_formatter = new DefaultLogFormatter();
|
|
|
|
|
}
|
2019-02-11 23:00:32 +11:00
|
|
|
|
2024-01-29 19:58:18 +01:00
|
|
|
public static FileStream PrepareLogFile(string path)
|
2019-02-11 23:00:32 +11:00
|
|
|
{
|
2020-02-12 01:18:33 +00:00
|
|
|
// Ensure directory is present
|
2024-02-10 19:17:19 -06:00
|
|
|
DirectoryInfo logDir;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logDir = new DirectoryInfo(path);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Logging directory path ('{path}') was invalid: {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 19:58:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logDir.Create();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}': {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-12 01:18:33 +00:00
|
|
|
|
|
|
|
|
// Clean up old logs, should only keep 3
|
|
|
|
|
FileInfo[] files = logDir.GetFiles("*.log").OrderBy((info => info.CreationTime)).ToArray();
|
|
|
|
|
for (int i = 0; i < files.Length - 2; i++)
|
|
|
|
|
{
|
2024-01-29 19:58:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
files[i].Delete();
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Old log file could not be deleted '{files[i].FullName}': {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Old log file could not be deleted '{files[i].FullName}': {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-12 01:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-29 19:58:18 +01:00
|
|
|
string version = ReleaseInformation.Version;
|
2020-12-07 10:29:22 +01:00
|
|
|
|
2020-02-12 01:18:33 +00:00
|
|
|
// Get path for the current time
|
2023-06-28 18:41:38 +02:00
|
|
|
path = Path.Combine(logDir.FullName, $"Ryujinx_{version}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log");
|
2020-02-12 01:18:33 +00:00
|
|
|
|
2024-01-29 19:58:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read);
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Log file could not be created '{path}': {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException exception)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Log file could not be created '{path}': {exception}");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-02-11 23:00:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(object sender, LogEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
_logWriter.WriteLine(_formatter.Format(args));
|
|
|
|
|
_logWriter.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2023-06-28 18:41:38 +02:00
|
|
|
GC.SuppressFinalize(this);
|
2019-02-11 23:00:32 +11:00
|
|
|
_logWriter.WriteLine("---- End of Log ----");
|
|
|
|
|
_logWriter.Flush();
|
|
|
|
|
_logWriter.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|