Advertisement
MaximilianPs

ConsoleLogger

Jun 9th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1.  
  2. using System;
  3.  
  4. /// <summary>
  5. ///
  6. /// Put this script in the Editor folder of your Unity project.
  7. ///
  8. /// This will add a menu item under "Tools/Save console log" to start and stop logging the console output.
  9. /// Creates a console_output.log file in the Assets folder
  10. /// Logs all new console messages in real time
  11. /// Handles the transition between Edit Mode and Play Mode correctly
  12. /// Includes timestamps to identify log sessions
  13. /// It is efficient and does not cause performance issues
  14. ///
  15. /// </summary>
  16. using UnityEngine;
  17. using UnityEditor;
  18. using System.IO;
  19.  
  20. public class ConsoleLogger : EditorWindow
  21. {
  22. private const string MENU_PATH = "Tools/Save console log";
  23. private const string LOG_FILE_PATH = "Assets/console_output.log";
  24.  
  25. private static StreamWriter logWriter;
  26. private static bool isLogging = false;
  27.  
  28. private const int FILESIZE = 100; // IN Megabytes
  29. private static int logCount = 0;
  30. private const int SIZE_CHECK_INTERVAL = 100; // Check every 10 logs
  31.  
  32. [MenuItem(MENU_PATH)]
  33. public static void ToggleConsoleLogging()
  34. {
  35. if (isLogging)
  36. {
  37. StopLogging();
  38. }
  39. else
  40. {
  41. StartLogging();
  42. }
  43. }
  44.  
  45. [MenuItem(MENU_PATH, true)]
  46. public static bool ValidateToggleConsoleLogging()
  47. {
  48. Menu.SetChecked(MENU_PATH, isLogging);
  49. return true;
  50. }
  51.  
  52. private static void StartLogging()
  53. {
  54. // Crea un nuovo file
  55. logWriter = new StreamWriter(LOG_FILE_PATH, false);
  56. logWriter.WriteLine($"--- Console Log Started at {System.DateTime.Now} ---");
  57. logWriter.Flush();
  58.  
  59. // Registra il callback
  60. Application.logMessageReceivedThreaded += HandleLog;
  61. isLogging = true;
  62.  
  63. Debug.Log("Console log saving STARTED");
  64. }
  65.  
  66. private static void StopLogging()
  67. {
  68. Application.logMessageReceivedThreaded -= HandleLog;
  69. isLogging = false;
  70.  
  71. if (logWriter != null)
  72. {
  73. logWriter.WriteLine($"--- Console Log Ended at {System.DateTime.Now} ---");
  74. logWriter.Close();
  75. logWriter = null;
  76. }
  77.  
  78. Debug.Log("Console log saving STOPPED");
  79. AssetDatabase.Refresh();
  80. }
  81.  
  82. static void HandleLog(string logString, string stackTrace, LogType type)
  83. {
  84. if (logWriter != null)
  85. {
  86. if (++logCount % SIZE_CHECK_INTERVAL == 0)
  87. CheckFileSize();
  88.  
  89. logWriter.WriteLine($"[{System.DateTime.Now:HH:mm:ss}] - [{type}] {logString}");
  90. if (type == LogType.Error || type == LogType.Exception)
  91. {
  92. logWriter.WriteLine($"StackTrace: {stackTrace}");
  93. }
  94. logWriter.Flush();
  95. }
  96. }
  97.  
  98. [InitializeOnLoadMethod]
  99. private static void OnUnityLoad()
  100. {
  101. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  102. }
  103.  
  104. private static void OnPlayModeStateChanged(PlayModeStateChange state)
  105. {
  106. if (state == PlayModeStateChange.ExitingEditMode && isLogging)
  107. {
  108. StopLogging();
  109. StartLogging();
  110. }
  111. }
  112.  
  113. private static void CheckFileSize()
  114. {
  115. try
  116. {
  117. logWriter.Flush(); // Forza la scrittura per avere dimensioni accurate
  118. FileInfo fileInfo = new FileInfo(LOG_FILE_PATH);
  119.  
  120. // 10MB = 10 * 1024 * 1024 bytes
  121. if (fileInfo.Exists && fileInfo.Length > FILESIZE * 1024 * 1024)
  122. {
  123. // Ricrea il file
  124. logWriter.Close();
  125. logWriter = new StreamWriter(LOG_FILE_PATH, false);
  126. logWriter.WriteLine($"--- LOG FILE RESET AT {System.DateTime.Now} DUE TO SIZE LIMIT ---");
  127. logWriter.Flush();
  128. }
  129. }
  130. catch (System.Exception e)
  131. {
  132. Debug.LogError($"Error checking log file size: {e}");
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement