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.

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