Logging.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Diagnostics.Metrics;
  2. using System.Runtime.CompilerServices;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Singleton logging instance class. Do not use console loggers
  8. /// with this class as it will interfere with Terminal.Gui
  9. /// screen output (i.e. use a file logger).
  10. /// </summary>
  11. /// <remarks>
  12. /// Also contains the
  13. /// <see cref="Meter"/> instance that should be used for internal metrics
  14. /// (iteration timing etc).
  15. /// </remarks>
  16. public static class Logging
  17. {
  18. /// <summary>
  19. /// Logger, defaults to NullLogger (i.e. no logging). Set this to a
  20. /// file logger to enable logging of Terminal.Gui internals.
  21. /// </summary>
  22. public static ILogger Logger { get; set; } = NullLogger.Instance;
  23. /// <summary>
  24. /// Metrics reporting meter for internal Terminal.Gui processes. To use
  25. /// create your own static instrument e.g. CreateCounter, CreateHistogram etc
  26. /// </summary>
  27. internal static readonly Meter Meter = new ("Terminal.Gui");
  28. /// <summary>
  29. /// Metric for how long it takes each full iteration of the main loop to occur
  30. /// </summary>
  31. public static readonly Histogram<int> TotalIterationMetric = Meter.CreateHistogram<int> ("Iteration (ms)");
  32. /// <summary>
  33. /// Metric for how long it took to do the 'timeouts and invokes' section of main loop.
  34. /// </summary>
  35. public static readonly Histogram<int> IterationInvokesAndTimeouts = Meter.CreateHistogram<int> ("Invokes & Timers (ms)");
  36. /// <summary>
  37. /// Counter for when we redraw, helps detect situations e.g. where we are repainting entire UI every loop
  38. /// </summary>
  39. public static readonly Counter<int> Redraws = Meter.CreateCounter<int> ("Redraws");
  40. /// <summary>
  41. /// Metric for how long it takes to read all available input from the input stream - at which
  42. /// point input loop will sleep.
  43. /// </summary>
  44. public static readonly Histogram<int> DrainInputStream = Meter.CreateHistogram<int> ("Drain Input (ms)");
  45. /// <summary>
  46. /// Logs a trace message including the
  47. /// </summary>
  48. /// <param name="message"></param>
  49. /// <param name="caller"></param>
  50. /// <param name="filePath"></param>
  51. public static void Trace (
  52. string message,
  53. [CallerMemberName] string caller = "",
  54. [CallerFilePath] string filePath = ""
  55. )
  56. {
  57. string className = Path.GetFileNameWithoutExtension (filePath);
  58. Logger.LogTrace ($"[{className}] [{caller}] {message}");
  59. }
  60. }