diff options
| author | merry <git@mary.rs> | 2023-02-25 15:07:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-25 15:07:23 +0000 |
| commit | 9b1cc2cec6135602efc5dc5afa45ed3db261eb42 (patch) | |
| tree | 7c73dcbcadcd1430ca7229a8ddd85054bb7b85ce /Ryujinx.Common/SystemInterop/StdErrAdapter.cs | |
| parent | e691622f0a118d550a7891896e40b0d9ab39fb60 (diff) | |
Logging: Redirect StdErr into logging system (#4427)
* Logging: Redirect StdErr into logging system
* Remove Mono.Unix
* Apply suggestions from code review
Co-authored-by: riperiperi <rhy3756547@hotmail.com>
* Address comments
---------
Co-authored-by: Mary <thog@protonmail.com>
Co-authored-by: riperiperi <rhy3756547@hotmail.com>
Co-authored-by: Mary <mary@mary.zone>
Diffstat (limited to 'Ryujinx.Common/SystemInterop/StdErrAdapter.cs')
| -rw-r--r-- | Ryujinx.Common/SystemInterop/StdErrAdapter.cs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/Ryujinx.Common/SystemInterop/StdErrAdapter.cs b/Ryujinx.Common/SystemInterop/StdErrAdapter.cs new file mode 100644 index 00000000..12e240ad --- /dev/null +++ b/Ryujinx.Common/SystemInterop/StdErrAdapter.cs @@ -0,0 +1,93 @@ +using System; +using System.IO; +using System.Runtime.Versioning; +using System.Threading; +using Ryujinx.Common.Logging; +using System.Runtime.InteropServices; + +namespace Ryujinx.Common.SystemInterop +{ + public partial class StdErrAdapter : IDisposable + { + private bool _disposable = false; + private UnixStream _pipeReader; + private UnixStream _pipeWriter; + private Thread _worker; + + public StdErrAdapter() + { + if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) + { + RegisterPosix(); + } + } + + [SupportedOSPlatform("linux")] + [SupportedOSPlatform("macos")] + private void RegisterPosix() + { + const int stdErrFileno = 2; + + (int readFd, int writeFd) = MakePipe(); + dup2(writeFd, stdErrFileno); + + _pipeReader = new UnixStream(readFd); + _pipeWriter = new UnixStream(writeFd); + + _worker = new Thread(EventWorker); + _disposable = true; + _worker.Start(); + } + + [SupportedOSPlatform("linux")] + [SupportedOSPlatform("macos")] + private void EventWorker() + { + TextReader reader = new StreamReader(_pipeReader); + string line; + while ((line = reader.ReadLine()) != null) + { + Logger.Error?.PrintRawMsg(line); + } + } + + private void Dispose(bool disposing) + { + if (_disposable) + { + if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) + { + _pipeReader?.Close(); + _pipeWriter?.Close(); + } + + _disposable = false; + } + } + + public void Dispose() + { + Dispose(true); + } + + [LibraryImport("libc", SetLastError = true)] + private static partial int dup2(int fd, int fd2); + + [LibraryImport("libc", SetLastError = true)] + private static unsafe partial int pipe(int* pipefd); + + private static unsafe (int, int) MakePipe() + { + int *pipefd = stackalloc int[2]; + + if (pipe(pipefd) == 0) + { + return (pipefd[0], pipefd[1]); + } + else + { + throw new(); + } + } + } +} |
