Log.cs 2.1 KB

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