You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

  1. using IllusionPlugin.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using zlib;
  9. namespace IllusionInjector.Logging.Printers
  10. {
  11. public class GlobalZFIlePrinter : LogPrinter
  12. {
  13. public override IllusionPlugin.Logging.Logger.LogLevel Filter { get; set; }
  14. private FileInfo fileInfo;
  15. private StreamWriter fileWriter;
  16. private static FileInfo GetFileInfo()
  17. {
  18. var logsDir = new DirectoryInfo("Logs");
  19. logsDir.Create();
  20. var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:YYYY.MM.DD.HH.MM}.log.z"));
  21. finfo.Create().Close();
  22. return finfo;
  23. }
  24. public GlobalZFIlePrinter()
  25. {
  26. fileInfo = GetFileInfo();
  27. }
  28. public override void StartPrint()
  29. {
  30. fileWriter = new StreamWriter(
  31. new ZOutputStream(fileInfo.Open(FileMode.Append, FileAccess.Write, FileShare.Read))
  32. {
  33. FlushMode = zlibConst.Z_FULL_FLUSH
  34. },
  35. Encoding.UTF8
  36. );
  37. }
  38. public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message)
  39. {
  40. foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  41. fileWriter.WriteLine(string.Format("[{3} @ {2:HH:mm:ss} | {1}] {0}", line, logName, time, level.ToString().ToUpper()));
  42. }
  43. public override void EndPrint()
  44. {
  45. fileWriter.Dispose();
  46. }
  47. }
  48. }