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
2.2 KiB

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