LogComponent.cs 2.4 KB

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