LogController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. namespace GFGGame
  5. {
  6. public class LogController : SingletonBase<LogController>
  7. {
  8. public const string _all = "All";
  9. public const string _error = "Error";
  10. public const string _log = "Log";
  11. public const int _maxCount = 100;
  12. public Dictionary<string, List<string>> msgDic = new Dictionary<string, List<string>>();
  13. public void Init()
  14. {
  15. msgDic.Add(_all, new List<string>());
  16. msgDic.Add(_error, new List<string>());
  17. msgDic.Add(_log, new List<string>());
  18. this.AddMsgListener();
  19. }
  20. public void AddMsgListener()
  21. {
  22. Application.logMessageReceived += (condition, stackTrace, type) =>
  23. {
  24. if (LauncherConfig.onDebug < 2)
  25. {
  26. return;
  27. }
  28. if (type == LogType.Error || type == LogType.Exception || type == LogType.Log)
  29. {
  30. this.AddMsgToDic(condition, stackTrace, type);
  31. }
  32. };
  33. }
  34. private void AddMsgToDic(string condition, string stackTrace, LogType type)
  35. {
  36. string msg = "";
  37. DateTime now = DateTime.Now;
  38. string time = string.Format("[{0}.{1} {2}:{3}:{4}]", now.Month, now.Day, now.Hour, now.Minute, now.Second);
  39. switch (type)
  40. {
  41. case LogType.Error:
  42. case LogType.Exception:
  43. if (msgDic[_all].Count == _maxCount)
  44. {
  45. msgDic[_all].RemoveAt(0);
  46. }
  47. if (msgDic[_error].Count == _maxCount)
  48. {
  49. msgDic[_error].RemoveAt(0);
  50. }
  51. msg = string.Format("error@{2}\n{0}\n{1}", condition, stackTrace, time);
  52. msgDic[_all].Add(msg);
  53. msgDic[_error].Add(msg);
  54. #if !UNITY_EDITOR
  55. ViewManager.Show(ViewName.LOG_VIEW);
  56. #endif
  57. break;
  58. default:
  59. if (msgDic[_all].Count == _maxCount)
  60. {
  61. msgDic[_all].RemoveAt(0);
  62. }
  63. if (msgDic[_log].Count == _maxCount)
  64. {
  65. msgDic[_log].RemoveAt(0);
  66. }
  67. msg = string.Format("log@{2}\n{0}\n{1}", condition, stackTrace, time);
  68. msgDic[_all].Add(msg);
  69. msgDic[_log].Add(msg);
  70. break;
  71. }
  72. }
  73. }
  74. }