// Assets/Editor/EditorLogTools.cs #if UNITY_EDITOR using System; using System.IO; using UnityEditor; using UnityEngine; public static class EditorLogTools { // ===== Public Menu ===== [MenuItem("Tools/Logs/Clear Editor.log")] public static void ClearEditorLogMenu() { string path = GetEditorLogPath(); if (string.IsNullOrEmpty(path)) { EditorUtility.DisplayDialog("Editor.log", "Không xác định được đường dẫn Editor.log trên hệ điều hành này.", "OK"); return; } if (!File.Exists(path)) { EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK"); return; } try { TruncateFile(path); EditorUtility.DisplayDialog("Editor.log", "Đã xoá sạch nội dung Editor.log ✅", "OK"); } catch (Exception e) { Debug.LogError($"[EditorLogTools] Clear failed: {e}"); EditorUtility.DisplayDialog("Editor.log", "Xoá thất bại. Xem Console để biết chi tiết.", "OK"); } } [MenuItem("Tools/Logs/Trim Editor.log…")] public static void TrimEditorLogMenu() { string input = EditorUtility.DisplayDialogComplex("Trim Editor.log", "Chọn kích thước còn lại của Editor.log sau khi cắt:", "Giữ 256 KB", "Giữ 1 MB", "Tự nhập (KB)") switch { 0 => "256", 1 => "1024", _ => EditorUtility.DisplayDialog("Nhập dung lượng", "Nhập số KB muốn giữ lại (ví dụ 512):", "OK") ? "512" // fallback, Unity không có input prompt chuẩn; giữ default : null }; if (string.IsNullOrEmpty(input)) return; if (!int.TryParse(input, out int keepKb) || keepKb < 1) keepKb = 512; string path = GetEditorLogPath(); if (!File.Exists(path)) { EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK"); return; } try { TrimTail(path, keepKb * 1024); EditorUtility.DisplayDialog("Editor.log", $"Đã cắt Editor.log, giữ lại ~{keepKb} KB cuối cùng ✅", "OK"); } catch (Exception e) { Debug.LogError($"[EditorLogTools] Trim failed: {e}"); EditorUtility.DisplayDialog("Editor.log", "Trim thất bại. Xem Console để biết chi tiết.", "OK"); } } [MenuItem("Tools/Logs/Open Editor.log")] public static void OpenEditorLogMenu() { string path = GetEditorLogPath(); if (File.Exists(path)) EditorUtility.OpenWithDefaultApp(path); else EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK"); } [MenuItem("Tools/Logs/Reveal log folder")] public static void RevealLogFolder() { string path = GetEditorLogPath(); string dir = string.IsNullOrEmpty(path) ? null : Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir)) EditorUtility.RevealInFinder(dir); else EditorUtility.DisplayDialog("Editor.log", "Không mở được thư mục log.", "OK"); } // ===== Core ===== private static void TruncateFile(string path) { // Cho phép Editor tiếp tục ghi khi ta truncate (FileShare.ReadWrite) using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite); fs.SetLength(0); fs.Flush(true); } private static void TrimTail(string path, int keepBytes) { var fi = new FileInfo(path); long size = fi.Length; if (size <= keepBytes) return; // không cần cắt // đọc phần đuôi rồi ghi đè lại byte[] buffer; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fs.Seek(size - keepBytes, SeekOrigin.Begin); buffer = new byte[keepBytes]; int read = fs.Read(buffer, 0, keepBytes); if (read < keepBytes) { Array.Resize(ref buffer, read); } } using (var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { fs.SetLength(0); fs.Write(buffer, 0, buffer.Length); fs.Flush(true); } } // ===== OS Paths ===== private static string GetEditorLogPath() { // Tham chiếu đường dẫn theo Unity docs switch (Application.platform) { case RuntimePlatform.WindowsEditor: // C:\Users\\AppData\Local\Unity\Editor\Editor.log string localApp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); return Path.Combine(localApp, "Unity", "Editor", "Editor.log"); case RuntimePlatform.OSXEditor: // ~/Library/Logs/Unity/Editor.log string home = Environment.GetFolderPath(Environment.SpecialFolder.Personal); return Path.Combine(home, "Library", "Logs", "Unity", "Editor.log"); case RuntimePlatform.LinuxEditor: // ~/.config/unity3d/Editor.log (đường dẫn phổ biến cho Editor) string homeLinux = Environment.GetFolderPath(Environment.SpecialFolder.Personal); return Path.Combine(homeLinux, ".config", "unity3d", "Editor.log"); default: return null; } } } #endif