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.

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