DebugOutputManager.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System.Text;
  6. public class DebugOutputManager{
  7. private string outpathLog;
  8. private string outpathError;
  9. private string outpathWarning;
  10. public DebugOutputManager ()
  11. {
  12. #if UNITY_EDITOR
  13. outpathLog = Application.dataPath + "/StreamingAssets" + "/outLog.txt";
  14. outpathError = Application.dataPath + "/StreamingAssets" + "/outLogError.txt";
  15. outpathWarning = Application.dataPath + "/StreamingAssets" + "/outLogWarining.txt";
  16. #elif UNITY_IPHONE
  17. outpathLog = Application.dataPath +"/Raw"+"/outLog.txt";
  18. outpathError = Application.dataPath +"/Raw"+"/outLogError.txt";
  19. outpathWarning = Application.dataPath +"/Raw"+"/outLogWarining.txt";
  20. #elif UNITY_ANDROID
  21. outpathLog = Application.persistentDataPath + "/outLog.txt";
  22. outpathError = Application.persistentDataPath + "/outLogError.txt";
  23. outpathWarning = Application.persistentDataPath + "/outLogWarining.txt";
  24. #endif
  25. //每次启动客户端删除之前保存的Log
  26. if (System.IO.File.Exists(outpathLog))
  27. {
  28. File.Delete(outpathLog);
  29. }
  30. if (System.IO.File.Exists(outpathError))
  31. {
  32. File.Delete(outpathError);
  33. }
  34. if (System.IO.File.Exists(outpathWarning))
  35. {
  36. File.Delete(outpathWarning);
  37. }
  38. }
  39. public void WriteLog (string log, string trace, LogType lType) {
  40. switch (lType) {
  41. case LogType.Log:
  42. using (StreamWriter writer = new StreamWriter(outpathLog, true, Encoding.UTF8)) { writer.WriteLine(log); }
  43. break;
  44. case LogType.Warning:
  45. using (StreamWriter writer = new StreamWriter(outpathWarning, true, Encoding.UTF8)) { writer.WriteLine(log); }
  46. break;
  47. case LogType.Error:
  48. using (StreamWriter writer = new StreamWriter(outpathError, true, Encoding.UTF8)) { writer.WriteLine(log); }
  49. break;
  50. }
  51. }
  52. }