FileLogger.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace ET
  7. {
  8. public class FileLogger: ILog
  9. {
  10. private readonly StreamWriter stream;
  11. public FileLogger(string filepath = null)
  12. {
  13. if (filepath == null)
  14. {
  15. #if UNITY_EDITOR || UNITY_STANDALONE
  16. filepath = "./";
  17. #elif UNITY_ANDROID
  18. filepath = Application.persistentDataPath;
  19. #endif
  20. filepath = Path.Combine(filepath, "Log.txt");
  21. }
  22. FileStream fs = File.Open(filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  23. fs.Seek(0, SeekOrigin.End);
  24. this.stream = new StreamWriter(fs, Encoding.UTF8);
  25. //this.stream = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
  26. this.stream.AutoFlush = true;
  27. }
  28. public void Trace(string message)
  29. {
  30. this.stream.WriteLine(message);
  31. this.stream.Flush();
  32. }
  33. public void Warning(string message)
  34. {
  35. this.stream.WriteLine(message);
  36. this.stream.Flush();
  37. }
  38. public void Info(string message)
  39. {
  40. this.stream.WriteLine(message);
  41. this.stream.Flush();
  42. }
  43. public void Debug(string message)
  44. {
  45. this.stream.WriteLine(message);
  46. this.stream.Flush();
  47. }
  48. public void Error(string message)
  49. {
  50. this.stream.WriteLine(message);
  51. this.stream.Flush();
  52. }
  53. public void Trace(string message, params object[] args)
  54. {
  55. this.stream.WriteLine(message, args);
  56. this.stream.Flush();
  57. }
  58. public void Warning(string message, params object[] args)
  59. {
  60. this.stream.WriteLine(message, args);
  61. this.stream.Flush();
  62. }
  63. public void Info(string message, params object[] args)
  64. {
  65. this.stream.WriteLine(message, args);
  66. this.stream.Flush();
  67. }
  68. public void Debug(string message, params object[] args)
  69. {
  70. this.stream.WriteLine(message, args);
  71. this.stream.Flush();
  72. }
  73. public void Error(string message, params object[] args)
  74. {
  75. this.stream.WriteLine(message, args);
  76. this.stream.Flush();
  77. }
  78. public void Fatal(string message, params object[] args)
  79. {
  80. this.stream.WriteLine(message, args);
  81. this.stream.Flush();
  82. }
  83. }
  84. }