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.

53 lines
2.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using IPA.Utilities;
  2. using System;
  3. using System.IO;
  4. namespace IPA.Logging.Printers
  5. {
  6. /// <summary>
  7. /// Prints log messages to the file specified by the name.
  8. /// </summary>
  9. public class PluginLogFilePrinter : GZFilePrinter
  10. {
  11. /// <summary>
  12. /// Provides a filter for this specific printer.
  13. /// </summary>
  14. /// <value>the filter level for this printer</value>
  15. public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All;
  16. private string name;
  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", name));
  24. logsDir.Create();
  25. var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():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="name">the name of the logger</param>
  32. public PluginLogFilePrinter(string name)
  33. {
  34. this.name = name;
  35. }
  36. /// <summary>
  37. /// Prints an entry to the associated file.
  38. /// </summary>
  39. /// <param name="level">the <see cref="Logger.Level"/> of the message</param>
  40. /// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
  41. /// <param name="logName">the name of the log that sent the message</param>
  42. /// <param name="message">the message to print</param>
  43. public override void Print(Logger.Level level, DateTime time, string logName, string message)
  44. {
  45. foreach (var line in removeControlCodes.Replace(message, "").Split(new[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  46. FileWriter.WriteLine(Logger.LogFormat, line, logName, time, level.ToString().ToUpper());
  47. }
  48. }
  49. }