LogComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Base
  5. {
  6. [ObjectEvent]
  7. public class LogComponentEvent : ObjectEvent<LogComponent>, IAwake
  8. {
  9. public void Awake()
  10. {
  11. this.GetValue().Awake();
  12. }
  13. }
  14. public class LogComponent : Component
  15. {
  16. private StreamWriter info;
  17. private StreamWriter error;
  18. // 每多少秒发一次
  19. public long SendToServerFrequency = 20 * 1000;
  20. public long SendToServerTime;
  21. #if UNITY_EDITOR
  22. private static bool IsNeedFlush = true;
  23. #else
  24. private static bool IsNeedFlush = false;
  25. #endif
  26. public void Awake()
  27. {
  28. if (!Directory.Exists("../Log"))
  29. {
  30. Directory.CreateDirectory("../Log");
  31. }
  32. string s = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  33. info = new StreamWriter($"../Log/log-{s}.info.log", false, Encoding.Unicode, 1024);
  34. error = new StreamWriter($"../Log/log-{s}.error.log", false, Encoding.Unicode, 1024);
  35. }
  36. public void Warning(string msg)
  37. {
  38. DateTime dateTime = DateTime.Now;
  39. string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {msg}";
  40. info.WriteLine(s);
  41. if (IsNeedFlush)
  42. {
  43. info.Flush();
  44. }
  45. #if UNITY_EDITOR
  46. UnityEngine.Debug.LogWarning(s);
  47. #endif
  48. }
  49. public void Info(string msg)
  50. {
  51. DateTime dateTime = DateTime.Now;
  52. string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {msg}";
  53. info.WriteLine(s);
  54. if (IsNeedFlush)
  55. {
  56. info.Flush();
  57. }
  58. #if UNITY_EDITOR
  59. UnityEngine.Debug.Log(s);
  60. #endif
  61. }
  62. public void Error(string msg)
  63. {
  64. DateTime dateTime = DateTime.Now;
  65. string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {TimeHelper.ClientNow()} {msg}";
  66. error.WriteLine(s);
  67. if (IsNeedFlush)
  68. {
  69. error.Flush();
  70. }
  71. #if UNITY_EDITOR
  72. UnityEngine.Debug.LogError(s);
  73. #endif
  74. long timeNow = TimeHelper.ClientNow();
  75. if (timeNow - SendToServerTime > SendToServerFrequency)
  76. {
  77. SendToServerTime = timeNow;
  78. Share.Scene.GetComponent<EventComponent>().Run(EventIdType.LogError, s);
  79. }
  80. }
  81. public void Debug(string msg)
  82. {
  83. #if UNITY_EDITOR
  84. DateTime dateTime = DateTime.Now;
  85. string s = $"{dateTime.ToString("yyyy-MM-dd HH:mm:ss")} {TimeHelper.ClientNow()} {msg}";
  86. UnityEngine.Debug.Log(s);
  87. #endif
  88. }
  89. public void Flush()
  90. {
  91. info.Flush();
  92. error.Flush();
  93. }
  94. public override void Dispose()
  95. {
  96. if (this.Id == 0)
  97. {
  98. return;
  99. }
  100. base.Dispose();
  101. this.info.Close();
  102. this.error.Close();
  103. }
  104. }
  105. }