DiskLogListener.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading;
  5. namespace BepInEx.Logging
  6. {
  7. /// <summary>
  8. /// Logs entries using Unity specific outputs.
  9. /// </summary>
  10. public class DiskLogListener : ILogListener
  11. {
  12. /// <summary>
  13. /// Log levels to display.
  14. /// </summary>
  15. public LogLevel DisplayedLogLevel { get; set; }
  16. /// <summary>
  17. /// Writer for the disk log.
  18. /// </summary>
  19. public TextWriter LogWriter { get; protected set; }
  20. /// <summary>
  21. /// Timer for flushing the logs to a file.
  22. /// </summary>
  23. private Timer FlushTimer { get; set; }
  24. private bool InstantFlushing { get; set; }
  25. /// <summary>
  26. /// Creates a new disk log listener.
  27. /// </summary>
  28. /// <param name="localPath">Path to the log.</param>
  29. /// <param name="displayedLogLevel">Log levels to display.</param>
  30. /// <param name="appendLog">Whether to append logs to an already existing log file.</param>
  31. /// <param name="delayedFlushing">Whether to delay flushing to disk to improve performance. Useful to set this to false when debugging crashes.</param>
  32. /// <param name="fileLimit">Maximum amount of concurrently opened log files. Can help with infinite game boot loops.</param>
  33. public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool delayedFlushing = true, int fileLimit = 5)
  34. {
  35. DisplayedLogLevel = displayedLogLevel;
  36. int counter = 1;
  37. FileStream fileStream;
  38. while (!Utility.TryOpenFileStream(Path.Combine(Paths.BepInExRootPath, localPath), appendLog ? FileMode.Append : FileMode.Create, out fileStream, share: FileShare.Read, access: FileAccess.Write))
  39. {
  40. if (counter == fileLimit)
  41. {
  42. Logger.LogError("Couldn't open a log file for writing. Skipping log file creation");
  43. return;
  44. }
  45. Logger.LogWarning($"Couldn't open log file '{localPath}' for writing, trying another...");
  46. localPath = $"LogOutput.{counter++}.log";
  47. }
  48. LogWriter = TextWriter.Synchronized(new StreamWriter(fileStream, Encoding.UTF8));
  49. if (delayedFlushing)
  50. {
  51. FlushTimer = new Timer(o => { LogWriter?.Flush(); }, null, 2000, 2000);
  52. }
  53. InstantFlushing = !delayedFlushing;
  54. }
  55. public static HashSet<string> BlacklistedSources = new HashSet<string>();
  56. /// <inheritdoc />
  57. public void LogEvent(object sender, LogEventArgs eventArgs)
  58. {
  59. if (LogWriter == null)
  60. return;
  61. if (BlacklistedSources.Contains(eventArgs.Source.SourceName))
  62. return;
  63. if ((eventArgs.Level & DisplayedLogLevel) == 0)
  64. return;
  65. LogWriter.WriteLine(eventArgs.ToString());
  66. if (InstantFlushing)
  67. LogWriter.Flush();
  68. }
  69. /// <inheritdoc />
  70. public void Dispose()
  71. {
  72. FlushTimer?.Dispose();
  73. LogWriter?.Flush();
  74. LogWriter?.Dispose();
  75. }
  76. ~DiskLogListener()
  77. {
  78. Dispose();
  79. }
  80. }
  81. }