DiskLogListener.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool delayedFlushing = true)
  33. {
  34. DisplayedLogLevel = displayedLogLevel;
  35. int counter = 1;
  36. FileStream fileStream;
  37. while (!Utility.TryOpenFileStream(Path.Combine(Paths.BepInExRootPath, localPath), appendLog ? FileMode.Append : FileMode.Create, out fileStream, share: FileShare.Read, access: FileAccess.Write))
  38. {
  39. if (counter == 5)
  40. {
  41. Logger.LogError("Couldn't open a log file for writing. Skipping log file creation");
  42. return;
  43. }
  44. Logger.LogWarning($"Couldn't open log file '{localPath}' for writing, trying another...");
  45. localPath = $"LogOutput.log.{counter++}";
  46. }
  47. LogWriter = TextWriter.Synchronized(new StreamWriter(fileStream, Encoding.UTF8));
  48. if (delayedFlushing)
  49. {
  50. FlushTimer = new Timer(o => { LogWriter?.Flush(); }, null, 2000, 2000);
  51. }
  52. InstantFlushing = !delayedFlushing;
  53. }
  54. public static HashSet<string> BlacklistedSources = new HashSet<string>();
  55. /// <inheritdoc />
  56. public void LogEvent(object sender, LogEventArgs eventArgs)
  57. {
  58. if (BlacklistedSources.Contains(eventArgs.Source.SourceName))
  59. return;
  60. if ((eventArgs.Level & DisplayedLogLevel) == 0)
  61. return;
  62. LogWriter.WriteLine(eventArgs.ToString());
  63. if (InstantFlushing)
  64. LogWriter.Flush();
  65. }
  66. /// <inheritdoc />
  67. public void Dispose()
  68. {
  69. FlushTimer?.Dispose();
  70. LogWriter?.Flush();
  71. LogWriter?.Dispose();
  72. }
  73. ~DiskLogListener()
  74. {
  75. Dispose();
  76. }
  77. }
  78. }