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.

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