SplitFileLogger.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using ET;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. public class SplitFilesLogger : ILog
  8. {
  9. StreamWriter swDebug;
  10. StreamWriter swInfo;
  11. StreamWriter swWarning;
  12. StreamWriter swError;
  13. public SplitFilesLogger(string path = null)
  14. {
  15. if (path == null)
  16. {
  17. #if UNITY_EDITOR || UNITY_STANDALONE
  18. path = "./";
  19. #elif UNITY_ANDROID
  20. path = Application.persistentDataPath;
  21. #endif
  22. path = Path.Combine(path, "UnityLogs");
  23. if (!Directory.Exists(path)) {
  24. Directory.CreateDirectory(path);
  25. }
  26. }
  27. FileStream fwDebug = File.Open(Path.Combine(path, $"{DateTime.Now.ToString("yyyyMMddHH")}_Debug.log"),
  28. FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  29. fwDebug.Seek(0, SeekOrigin.End);
  30. swDebug = new StreamWriter(fwDebug);
  31. FileStream fwInfo = File.Open(Path.Combine(path, $"{DateTime.Now.ToString("yyyyMMddHH")}_Info.log"),
  32. FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  33. fwInfo.Seek(0, SeekOrigin.End);
  34. swInfo = new StreamWriter(fwInfo);
  35. FileStream fwWarning = File.Open(Path.Combine(path, $"{DateTime.Now.ToString("yyyyMMddHH")}_Warning.log"),
  36. FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  37. fwWarning.Seek(0, SeekOrigin.End);
  38. swWarning = new StreamWriter(fwWarning);
  39. FileStream fwError = File.Open(Path.Combine(path, $"{DateTime.Now.ToString("yyyyMMddHH")}_Error.log"),
  40. FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  41. fwError.Seek(0, SeekOrigin.End);
  42. swError = new StreamWriter(fwError);
  43. }
  44. public void Debug(string message)
  45. {
  46. swDebug.WriteLine(message);
  47. swDebug.Flush();
  48. }
  49. public void Debug(string message, params object[] args)
  50. {
  51. swDebug.WriteLine(string.Format(message, args));
  52. swDebug.Flush();
  53. }
  54. public void Error(string message)
  55. {
  56. swError.WriteLine(message);
  57. swError.Flush();
  58. }
  59. public void Error(string message, params object[] args)
  60. {
  61. swError.WriteLine(string.Format(message, args));
  62. swError.Flush();
  63. }
  64. public void Info(string message)
  65. {
  66. swInfo.WriteLine(message);
  67. swInfo.Flush();
  68. }
  69. public void Info(string message, params object[] args)
  70. {
  71. swInfo.WriteLine(string.Format(message, args));
  72. swInfo.Flush();
  73. }
  74. public void Trace(string message)
  75. {
  76. swDebug.WriteLine(message);
  77. swDebug.Flush();
  78. }
  79. public void Trace(string message, params object[] args)
  80. {
  81. swDebug.WriteLine(string.Format(message, args));
  82. swDebug.Flush();
  83. }
  84. public void Warning(string message)
  85. {
  86. swWarning.WriteLine(message);
  87. swWarning.Flush();
  88. }
  89. public void Warning(string message, params object[] args)
  90. {
  91. swWarning.WriteLine(string.Format(message, args));
  92. swWarning.Flush();
  93. }
  94. }