ILPostProcessorLogger.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections.Generic;
  2. using Mono.CecilX;
  3. using Unity.CompilationPipeline.Common.Diagnostics;
  4. namespace Mirror.Weaver
  5. {
  6. public class ILPostProcessorLogger : Logger
  7. {
  8. // can't Debug.Log in ILPostProcessor. need to add to this list.
  9. internal List<DiagnosticMessage> Logs = new List<DiagnosticMessage>();
  10. void Add(string message, DiagnosticType logType)
  11. {
  12. Logs.Add(new DiagnosticMessage
  13. {
  14. // TODO add file etc. for double click opening later?
  15. DiagnosticType = logType, // doesn't have .Log
  16. File = null,
  17. Line = 0,
  18. Column = 0,
  19. MessageData = message
  20. });
  21. }
  22. public void LogDiagnostics(string message, DiagnosticType logType = DiagnosticType.Warning)
  23. {
  24. // DiagnosticMessage can't display \n for some reason.
  25. // it just cuts it off and we don't see any stack trace.
  26. // so let's replace all line breaks so we get the stack trace.
  27. // (Unity 2021.2.0b6 apple silicon)
  28. //message = message.Replace("\n", "/");
  29. // lets break it into several messages instead so it's easier readable
  30. string[] lines = message.Split('\n');
  31. // if it's just one line, simply log it
  32. if (lines.Length == 1)
  33. {
  34. // tests assume exact message log.
  35. // don't include 'Weaver: ...' or similar.
  36. Add($"{message}", logType);
  37. }
  38. // for multiple lines, log each line separately with start/end indicators
  39. else
  40. {
  41. // first line with Weaver: ... first
  42. Add("----------------------------------------------", logType);
  43. foreach (string line in lines) Add(line, logType);
  44. Add("----------------------------------------------", logType);
  45. }
  46. }
  47. public void Warning(string message) => Warning(message, null);
  48. public void Warning(string message, MemberReference mr)
  49. {
  50. if (mr != null) message = $"{message} (at {mr})";
  51. LogDiagnostics(message, DiagnosticType.Warning);
  52. }
  53. public void Error(string message) => Error(message, null);
  54. public void Error(string message, MemberReference mr)
  55. {
  56. if (mr != null) message = $"{message} (at {mr})";
  57. LogDiagnostics(message, DiagnosticType.Error);
  58. }
  59. }
  60. }