StdErrAdapter.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Versioning;
  6. using System.Threading;
  7. namespace Ryujinx.Common.SystemInterop
  8. {
  9. public partial class StdErrAdapter : IDisposable
  10. {
  11. private bool _disposable = false;
  12. private UnixStream _pipeReader;
  13. private UnixStream _pipeWriter;
  14. private Thread _worker;
  15. public StdErrAdapter()
  16. {
  17. if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  18. {
  19. RegisterPosix();
  20. }
  21. }
  22. [SupportedOSPlatform("linux")]
  23. [SupportedOSPlatform("macos")]
  24. private void RegisterPosix()
  25. {
  26. const int stdErrFileno = 2;
  27. (int readFd, int writeFd) = MakePipe();
  28. dup2(writeFd, stdErrFileno);
  29. _pipeReader = new UnixStream(readFd);
  30. _pipeWriter = new UnixStream(writeFd);
  31. _worker = new Thread(EventWorker);
  32. _disposable = true;
  33. _worker.Start();
  34. }
  35. [SupportedOSPlatform("linux")]
  36. [SupportedOSPlatform("macos")]
  37. private void EventWorker()
  38. {
  39. TextReader reader = new StreamReader(_pipeReader);
  40. string line;
  41. while ((line = reader.ReadLine()) != null)
  42. {
  43. Logger.Error?.PrintRawMsg(line);
  44. }
  45. }
  46. private void Dispose(bool disposing)
  47. {
  48. if (_disposable)
  49. {
  50. if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  51. {
  52. _pipeReader?.Close();
  53. _pipeWriter?.Close();
  54. }
  55. _disposable = false;
  56. }
  57. }
  58. public void Dispose()
  59. {
  60. Dispose(true);
  61. }
  62. [LibraryImport("libc", SetLastError = true)]
  63. private static partial int dup2(int fd, int fd2);
  64. [LibraryImport("libc", SetLastError = true)]
  65. private static unsafe partial int pipe(int* pipefd);
  66. private static unsafe (int, int) MakePipe()
  67. {
  68. int *pipefd = stackalloc int[2];
  69. if (pipe(pipefd) == 0)
  70. {
  71. return (pipefd[0], pipefd[1]);
  72. }
  73. else
  74. {
  75. throw new();
  76. }
  77. }
  78. }
  79. }