| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace DirectSight.Logging; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Logger that writes to the console. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class ConsoleLogger |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Whether to show degugging messages. |
| | | 12 | | /// </summary> |
| | 228 | 13 | | public static bool ShowDebugMessages { get; set; } = false; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Log a formatted message at DEBUG level. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="format">The template string.</param> |
| | | 19 | | /// <param name="args">The arguments.</param> |
| | | 20 | | public static void Debug(string format, params object[] args) |
| | 228 | 21 | | { |
| | 228 | 22 | | if (ShowDebugMessages == true) |
| | 0 | 23 | | { |
| | 0 | 24 | | WriteLine("DEBUG", format, args); |
| | 0 | 25 | | } |
| | 228 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Log a formatted message at INFO level. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="format">The template string.</param> |
| | | 32 | | /// <param name="args">The arguments.</param> |
| | | 33 | | public static void Info(string format, params object[] args) |
| | 6 | 34 | | { |
| | 6 | 35 | | WriteLine("INFO", format, args); |
| | 6 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Log a formatted message at WARNING level. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="format">The template string.</param> |
| | | 42 | | /// <param name="args">The arguments.</param> |
| | | 43 | | public static void Warn(string format, params object[] args) |
| | 1 | 44 | | { |
| | 1 | 45 | | WriteLine("WARNING", format, args); |
| | 1 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Log a formatted message at ERROR level. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="format">The template string.</param> |
| | | 52 | | /// <param name="args">The arguments.</param> |
| | | 53 | | public static void Error(string format, params object[] args) |
| | 7 | 54 | | { |
| | 7 | 55 | | WriteLine("ERROR", format, args); |
| | 7 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Write a formatted message to the console. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="logLevel">The importance level of the message.</param> |
| | | 62 | | /// <param name="format">The template string.</param> |
| | | 63 | | /// <param name="args">The argument for the template string.</param> |
| | | 64 | | private static void WriteLine(string logLevel, string format, params object[] args) |
| | 14 | 65 | | { |
| | 14 | 66 | | if (args.Length > 0) |
| | 9 | 67 | | { |
| | 9 | 68 | | format = string.Format(format, args); |
| | 9 | 69 | | } |
| | 14 | 70 | | Console.WriteLine($"{DateTime.Now:s} {logLevel}: {format}"); |
| | 14 | 71 | | } |
| | | 72 | | } |