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.

59 lines
2.2 KiB

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