Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /// <summary>
- ///
- /// Put this script in the Editor folder of your Unity project.
- ///
- /// This will add a menu item under "Tools/Save console log" to start and stop logging the console output.
- /// Creates a console_output.log file in the Assets folder
- /// Logs all new console messages in real time
- /// Handles the transition between Edit Mode and Play Mode correctly
- /// Includes timestamps to identify log sessions
- /// It is efficient and does not cause performance issues
- ///
- /// </summary>
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- public class ConsoleLogger : EditorWindow
- {
- private const string MENU_PATH = "Tools/Save console log";
- private const string LOG_FILE_PATH = "Assets/console_output.log";
- private static StreamWriter logWriter;
- private static bool isLogging = false;
- private const int FILESIZE = 100; // IN Megabytes
- private static int logCount = 0;
- private const int SIZE_CHECK_INTERVAL = 100; // Check every 10 logs
- [MenuItem(MENU_PATH)]
- public static void ToggleConsoleLogging()
- {
- if (isLogging)
- {
- StopLogging();
- }
- else
- {
- StartLogging();
- }
- }
- [MenuItem(MENU_PATH, true)]
- public static bool ValidateToggleConsoleLogging()
- {
- Menu.SetChecked(MENU_PATH, isLogging);
- return true;
- }
- private static void StartLogging()
- {
- // Crea un nuovo file
- logWriter = new StreamWriter(LOG_FILE_PATH, false);
- logWriter.WriteLine($"--- Console Log Started at {System.DateTime.Now} ---");
- logWriter.Flush();
- // Registra il callback
- Application.logMessageReceivedThreaded += HandleLog;
- isLogging = true;
- Debug.Log("Console log saving STARTED");
- }
- private static void StopLogging()
- {
- Application.logMessageReceivedThreaded -= HandleLog;
- isLogging = false;
- if (logWriter != null)
- {
- logWriter.WriteLine($"--- Console Log Ended at {System.DateTime.Now} ---");
- logWriter.Close();
- logWriter = null;
- }
- Debug.Log("Console log saving STOPPED");
- AssetDatabase.Refresh();
- }
- static void HandleLog(string logString, string stackTrace, LogType type)
- {
- if (logWriter != null)
- {
- if (++logCount % SIZE_CHECK_INTERVAL == 0)
- CheckFileSize();
- logWriter.WriteLine($"[{System.DateTime.Now:HH:mm:ss}] - [{type}] {logString}");
- if (type == LogType.Error || type == LogType.Exception)
- {
- logWriter.WriteLine($"StackTrace: {stackTrace}");
- }
- logWriter.Flush();
- }
- }
- [InitializeOnLoadMethod]
- private static void OnUnityLoad()
- {
- EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
- }
- private static void OnPlayModeStateChanged(PlayModeStateChange state)
- {
- if (state == PlayModeStateChange.ExitingEditMode && isLogging)
- {
- StopLogging();
- StartLogging();
- }
- }
- private static void CheckFileSize()
- {
- try
- {
- logWriter.Flush(); // Forza la scrittura per avere dimensioni accurate
- FileInfo fileInfo = new FileInfo(LOG_FILE_PATH);
- // 10MB = 10 * 1024 * 1024 bytes
- if (fileInfo.Exists && fileInfo.Length > FILESIZE * 1024 * 1024)
- {
- // Ricrea il file
- logWriter.Close();
- logWriter = new StreamWriter(LOG_FILE_PATH, false);
- logWriter.WriteLine($"--- LOG FILE RESET AT {System.DateTime.Now} DUE TO SIZE LIMIT ---");
- logWriter.Flush();
- }
- }
- catch (System.Exception e)
- {
- Debug.LogError($"Error checking log file size: {e}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement