LogController.cs 3.7 KB

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