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.

56 lines
2.2 KiB

  1. using System;
  2. using System.IO;
  3. using IPA.Utilities;
  4. namespace IPA.Logging.Printers
  5. {
  6. /// <summary>
  7. /// Prints log messages to the file specified by the name.
  8. /// </summary>
  9. public class PluginSubLogPrinter : GZFilePrinter
  10. {
  11. /// <summary>
  12. /// Provides a filter for this specific printer.
  13. /// </summary>
  14. /// <value>the filter for this printer</value>
  15. public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All;
  16. private string name;
  17. private string mainName;
  18. /// <summary>
  19. /// Gets the <see cref="FileInfo"/> for the target file.
  20. /// </summary>
  21. /// <returns>the file to write to</returns>
  22. protected override FileInfo GetFileInfo()
  23. {
  24. var logsDir = new DirectoryInfo(Path.Combine("Logs", mainName, name));
  25. logsDir.Create();
  26. var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log"));
  27. return finfo;
  28. }
  29. /// <summary>
  30. /// Creates a new printer with the given name.
  31. /// </summary>
  32. /// <param name="mainname">the name of the main logger</param>
  33. /// <param name="name">the name of the logger</param>
  34. public PluginSubLogPrinter(string mainname, string name)
  35. {
  36. this.name = name;
  37. mainName = mainname;
  38. }
  39. /// <summary>
  40. /// Prints an entry to the associated file.
  41. /// </summary>
  42. /// <param name="level">the <see cref="Logger.Level"/> of the message</param>
  43. /// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
  44. /// <param name="logName">the name of the log that sent the message</param>
  45. /// <param name="message">the message to print</param>
  46. public override void Print(Logger.Level level, DateTime time, string logName, string message)
  47. {
  48. foreach (var line in removeControlCodes.Replace(message, "").Split(new[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  49. FileWriter.WriteLine("[{2} @ {1:HH:mm:ss}] {0}", line, time, level.ToString().ToUpper());
  50. }
  51. }
  52. }