LogController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UI.Common;
  4. using System;
  5. namespace GFGGame
  6. {
  7. public class LogController : SingletonBase<LogController>
  8. {
  9. public const string _all = "All";
  10. public const string _error = "Error";
  11. public const string _log = "Log";
  12. public const int _maxCount = 100;
  13. public Dictionary<string, List<string>> msgDic = new Dictionary<string, List<string>>();
  14. // private UI_BtnLog _btnLog;
  15. public void Init()
  16. {
  17. msgDic.Add(_all, new List<string>());
  18. msgDic.Add(_error, new List<string>());
  19. msgDic.Add(_log, new List<string>());
  20. if (LauncherConfig.onDebug == 1)
  21. {
  22. this.CreatBtnLog();
  23. }
  24. this.AddMsgListener();
  25. }
  26. public void AddMsgListener()
  27. {
  28. Application.logMessageReceived += (condition, stackTrace, type) =>
  29. {
  30. if (type == LogType.Error || type == LogType.Exception || type == LogType.Log)
  31. {
  32. this.AddMsgToDic(condition, stackTrace, type);
  33. }
  34. };
  35. }
  36. private void AddMsgToDic(string condition, string stackTrace, LogType type)
  37. {
  38. string msg = "";
  39. DateTime now = DateTime.Now;
  40. string time = string.Format("[{0}.{1} {2}:{3}:{4}]", now.Month, now.Day, now.Hour, now.Minute, now.Second);
  41. switch (type)
  42. {
  43. case LogType.Error:
  44. case LogType.Exception:
  45. if (msgDic[_all].Count == _maxCount)
  46. {
  47. msgDic[_all].RemoveAt(0);
  48. }
  49. if (msgDic[_error].Count == _maxCount)
  50. {
  51. msgDic[_error].RemoveAt(0);
  52. }
  53. msg = string.Format("error@{2}\n{0}\n{1}", condition, stackTrace, time);
  54. msgDic[_all].Add(msg);
  55. msgDic[_error].Add(msg);
  56. // if (_btnLog != null)
  57. // {
  58. // _btnLog.m_c1.selectedIndex = 1;
  59. // _btnLog.target.alpha = 1;
  60. // }
  61. if (LauncherConfig.onDebug >= 1)
  62. {
  63. #if !UNITY_EDITOR
  64. ViewManager.Show(ViewName.LOG_VIEW);
  65. #endif
  66. }
  67. break;
  68. default:
  69. if (LauncherConfig.onDebug >= 1)
  70. {
  71. if (msgDic[_all].Count == _maxCount)
  72. {
  73. msgDic[_all].RemoveAt(0);
  74. }
  75. if (msgDic[_log].Count == _maxCount)
  76. {
  77. msgDic[_log].RemoveAt(0);
  78. }
  79. msg = string.Format("log@{2}\n{0}\n{1}", condition, stackTrace, time);
  80. msgDic[_all].Add(msg);
  81. msgDic[_log].Add(msg);
  82. }
  83. break;
  84. }
  85. }
  86. private void CreatBtnLog()
  87. {
  88. // if (_btnLog == null)
  89. // {
  90. // _btnLog = UI_BtnLog.Create();
  91. // ViewManager.AddChildToDebugLayer(_btnLog.target);
  92. // this.SetBtnLogVisable(false);
  93. // _btnLog.target.x = 50;
  94. // _btnLog.target.y = 1880;
  95. // _btnLog.target.onClick.Add(this.OnBtnLogClick);
  96. // }
  97. }
  98. private void OnBtnLogClick()
  99. {
  100. ViewManager.Show(ViewName.LOG_VIEW);
  101. // _btnLog.m_c1.selectedIndex = 0;
  102. // _btnLog.target.alpha = 0;
  103. }
  104. public void SetBtnLogVisable(bool visable)
  105. {
  106. // _btnLog.target.visible = visable;
  107. }
  108. }
  109. }