源战役客户端
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

  1. 
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System;
  8. public class LogInfo
  9. {
  10. public string message = null;
  11. public LogType type = 0;
  12. public LogInfo(string message, LogType type)
  13. {
  14. this.message = message;
  15. this.type = type;
  16. }
  17. }
  18. namespace LuaFramework
  19. {
  20. public class LogHandler : Base
  21. {
  22. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  23. ConsoleWindows.ConsoleWindow console = new ConsoleWindows.ConsoleWindow();
  24. ConsoleWindows.ConsoleInput input = new ConsoleWindows.ConsoleInput();
  25. #endif
  26. private Thread thread;
  27. static readonly object m_lockObject = new object();
  28. static readonly object m_lockObject2 = new object();
  29. static Queue<LogInfo> log_list = new Queue<LogInfo>();
  30. //日志输出路径
  31. private string output_path = null;
  32. void Awake()
  33. {
  34. LogManager.RegisterLogCallback(delegate (string message, LogType type)
  35. {
  36. AddMessage(message, type);
  37. });
  38. string log_dir = Util.DataPath + "/log";
  39. if (!Directory.Exists(log_dir))
  40. Directory.CreateDirectory(log_dir);
  41. output_path = log_dir + "/out_put.txt";
  42. //每次启动先删除旧的
  43. if (File.Exists(output_path))
  44. {
  45. File.Delete(output_path);
  46. }
  47. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  48. console.Initialize();
  49. console.SetTitle("game log");
  50. #endif
  51. thread = new Thread(OnUpdateThread);
  52. Application.logMessageReceived += HandleLog;
  53. }
  54. // Use this for initialization
  55. void Start()
  56. {
  57. thread.Start();
  58. }
  59. public void AddMessage(string message, LogType type)
  60. {
  61. lock (m_lockObject2)
  62. {
  63. LogInfo li = new LogInfo(message, type);
  64. string day_str = System.DateTime.Now.Day.ToString()+"/";
  65. string hour_str = System.DateTime.Now.Hour.ToString()+":";
  66. string minute_str = System.DateTime.Now.Minute.ToString()+":";
  67. string second_str = System.DateTime.Now.Second.ToString()+":";
  68. li.message = day_str+hour_str+minute_str+second_str+li.message;
  69. log_list.Enqueue(li);
  70. }
  71. }
  72. //日志回调
  73. void HandleLog(string message, string stackTrace, LogType type)
  74. {
  75. AddMessage(message, type);
  76. }
  77. //主线程update
  78. void Update()
  79. {
  80. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  81. input.Update();
  82. #endif
  83. }
  84. //多线程update
  85. void OnUpdateThread()
  86. {
  87. while (true)
  88. {
  89. lock (m_lockObject)
  90. {
  91. if (log_list.Count > 0)
  92. {
  93. LogInfo li = log_list.Dequeue();
  94. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  95. if (li.type == LogType.Warning)
  96. System.Console.ForegroundColor = System.ConsoleColor.Yellow;
  97. else if (li.type == LogType.Error || li.type == LogType.Exception)
  98. System.Console.ForegroundColor = System.ConsoleColor.Red;
  99. else
  100. System.Console.ForegroundColor = System.ConsoleColor.White;
  101. if (System.Console.CursorLeft != 0)
  102. input.ClearLine();
  103. System.Console.WriteLine(li.message);
  104. #endif
  105. OnWriteFile(li.message);
  106. }
  107. }
  108. Thread.Sleep(1);
  109. }
  110. }
  111. void OnWriteFile(string message)
  112. {
  113. try
  114. {
  115. StreamWriter writer = new StreamWriter(output_path, true, System.Text.Encoding.UTF8);
  116. writer.WriteLine(message);
  117. writer.Close();
  118. }
  119. catch(Exception e)
  120. {
  121. Debug.Log("write game log error :" + e.Message);
  122. }
  123. }
  124. void OnDestroy()
  125. {
  126. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  127. console.Shutdown();
  128. #endif
  129. }
  130. }
  131. }