You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logger.cs 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using static LLama.Common.ILLamaLogger;
  5. namespace LLama.Common;
  6. public interface ILLamaLogger
  7. {
  8. public enum LogLevel
  9. {
  10. Info,
  11. Debug,
  12. Warning,
  13. Error
  14. }
  15. /// <summary>
  16. /// Write the log in cosutomized way
  17. /// </summary>
  18. /// <param name="source">The source of the log. It may be a method name or class name.</param>
  19. /// <param name="message">The message.</param>
  20. /// <param name="level">The log level.</param>
  21. void Log(string source, string message, LogLevel level);
  22. }
  23. /// <summary>
  24. /// The default logger of LLamaSharp. On default it write to console. User methods of `LLamaLogger.Default` to change the behavior.
  25. /// It's more recommended to inherit `ILLamaLogger` to cosutomize the behavior.
  26. /// </summary>
  27. public sealed class LLamaDefaultLogger : ILLamaLogger
  28. {
  29. private static readonly Lazy<LLamaDefaultLogger> _instance = new Lazy<LLamaDefaultLogger>(() => new LLamaDefaultLogger());
  30. private bool _toConsole = true;
  31. private bool _toFile = false;
  32. private FileStream? _fileStream = null;
  33. private StreamWriter _fileWriter = null;
  34. public static LLamaDefaultLogger Default => _instance.Value;
  35. private LLamaDefaultLogger()
  36. {
  37. }
  38. public LLamaDefaultLogger EnableConsole()
  39. {
  40. _toConsole = true;
  41. return this;
  42. }
  43. public LLamaDefaultLogger DisableConsole()
  44. {
  45. _toConsole = false;
  46. return this;
  47. }
  48. public LLamaDefaultLogger EnableFile(string filename, FileMode mode = FileMode.Append)
  49. {
  50. _fileStream = new FileStream(filename, mode, FileAccess.Write);
  51. _fileWriter = new StreamWriter(_fileStream);
  52. _toFile = true;
  53. return this;
  54. }
  55. public LLamaDefaultLogger DisableFile(string filename)
  56. {
  57. if (_fileWriter is not null)
  58. {
  59. _fileWriter.Close();
  60. _fileWriter = null;
  61. }
  62. if (_fileStream is not null)
  63. {
  64. _fileStream.Close();
  65. _fileStream = null;
  66. }
  67. _toFile = false;
  68. return this;
  69. }
  70. public void Log(string source, string message, LogLevel level)
  71. {
  72. if (level == LogLevel.Info)
  73. {
  74. Info(message);
  75. }
  76. else if (level == LogLevel.Debug)
  77. {
  78. }
  79. else if (level == LogLevel.Warning)
  80. {
  81. Warn(message);
  82. }
  83. else if (level == LogLevel.Error)
  84. {
  85. Error(message);
  86. }
  87. }
  88. public void Info(string message)
  89. {
  90. message = MessageFormat("info", message);
  91. if (_toConsole)
  92. {
  93. Console.ForegroundColor = ConsoleColor.White;
  94. Console.WriteLine(message);
  95. Console.ResetColor();
  96. }
  97. if (_toFile)
  98. {
  99. Debug.Assert(_fileStream is not null);
  100. Debug.Assert(_fileWriter is not null);
  101. _fileWriter.WriteLine(message);
  102. }
  103. }
  104. public void Warn(string message)
  105. {
  106. message = MessageFormat("warn", message);
  107. if (_toConsole)
  108. {
  109. Console.ForegroundColor = ConsoleColor.Yellow;
  110. Console.WriteLine(message);
  111. Console.ResetColor();
  112. }
  113. if (_toFile)
  114. {
  115. Debug.Assert(_fileStream is not null);
  116. Debug.Assert(_fileWriter is not null);
  117. _fileWriter.WriteLine(message);
  118. }
  119. }
  120. public void Error(string message)
  121. {
  122. message = MessageFormat("error", message);
  123. if (_toConsole)
  124. {
  125. Console.ForegroundColor = ConsoleColor.Red;
  126. Console.WriteLine(message);
  127. Console.ResetColor();
  128. }
  129. if (_toFile)
  130. {
  131. Debug.Assert(_fileStream is not null);
  132. Debug.Assert(_fileWriter is not null);
  133. _fileWriter.WriteLine(message);
  134. }
  135. }
  136. private string MessageFormat(string level, string message)
  137. {
  138. DateTime now = DateTime.Now;
  139. string formattedDate = now.ToString("yyyy.MM.dd HH:mm:ss");
  140. return $"[{formattedDate}][{level}]: {message}";
  141. }
  142. }

C#/.NET上易用的LLM高性能推理框架,支持LLaMA和LLaVA系列模型。