Log.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Model
  5. {
  6. public static class Log
  7. {
  8. private static readonly StreamWriter info;
  9. private static readonly StreamWriter error;
  10. public static bool IsNeedFlush = true;
  11. private static string dirName = "Logs";
  12. private static string infoFileName = "/Log-Client-Info.txt";
  13. private static string errorFileName = "/Log-Client-Error.txt";
  14. static Log()
  15. {
  16. string dirPath = PathHelp.AppHotfixResPath + dirName;
  17. if (!Directory.Exists(dirPath))
  18. {
  19. Directory.CreateDirectory(dirPath);
  20. }
  21. info = new StreamWriter(dirPath+ infoFileName, false, Encoding.Unicode, 1024);
  22. error = new StreamWriter(dirPath+ errorFileName, false, Encoding.Unicode, 1024);
  23. }
  24. public static void Warning(string msg)
  25. {
  26. DateTime dateTime = DateTime.Now;
  27. string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {msg}";
  28. info.WriteLine(s);
  29. if (IsNeedFlush)
  30. {
  31. info.Flush();
  32. }
  33. #if UNITY_EDITOR
  34. UnityEngine.Debug.LogWarning(s);
  35. #endif
  36. }
  37. public static void Info(string msg)
  38. {
  39. DateTime dateTime = DateTime.Now;
  40. string s = $"{dateTime: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.Log(s);
  48. #endif
  49. }
  50. public static void Error(string msg)
  51. {
  52. DateTime dateTime = DateTime.Now;
  53. string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {TimeHelper.ClientNow()} {msg}";
  54. error.WriteLine(s);
  55. if (IsNeedFlush)
  56. {
  57. error.Flush();
  58. }
  59. info.WriteLine(s);
  60. if (IsNeedFlush)
  61. {
  62. info.Flush();
  63. }
  64. #if UNITY_EDITOR
  65. UnityEngine.Debug.LogError(s);
  66. #endif
  67. }
  68. public static void Debug(string msg)
  69. {
  70. #if UNITY_EDITOR
  71. DateTime dateTime = DateTime.Now;
  72. string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {TimeHelper.ClientNow()} {msg}";
  73. UnityEngine.Debug.Log(s);
  74. info.WriteLine(s);
  75. if (IsNeedFlush)
  76. {
  77. info.Flush();
  78. }
  79. #endif
  80. }
  81. public static void Flush()
  82. {
  83. info.Flush();
  84. error.Flush();
  85. }
  86. }
  87. }