From c0e53159535990b98679dc70453ef0d7e9e4e2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 23 Apr 2025 18:19:13 +0800 Subject: [PATCH] feat : Buld the pipeline output the log file. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 构建管线输出构建日志到输出目录下。 --- .../AssetBundleBuilder/AssetBundleBuilder.cs | 8 +- .../BuildSystem/BuildLogger.cs | 73 +++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/AssetBundleBuilder.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/AssetBundleBuilder.cs index fec66722..17b45bf1 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/AssetBundleBuilder.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/AssetBundleBuilder.cs @@ -32,8 +32,9 @@ namespace YooAsset.Editor var buildParametersContext = new BuildParametersContext(buildParameters); _buildContext.SetContextObject(buildParametersContext); - // 初始化日志 - BuildLogger.InitLogger(enableLog); + // 初始化日志系统 + string logFilePath = $"{buildParametersContext.GetPipelineOutputDirectory()}/buildInfo.log"; + BuildLogger.InitLogger(enableLog, logFilePath); // 执行构建流程 BuildLogger.Log($"Begin to build package : {buildParameters.PackageName} by {buildParameters.BuildPipeline}"); @@ -50,6 +51,9 @@ namespace YooAsset.Editor BuildLogger.Error(buildResult.ErrorInfo); } + // 关闭日志系统 + BuildLogger.Shuntdown(); + return buildResult; } } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildLogger.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildLogger.cs index abac194a..b4571229 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildLogger.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildLogger.cs @@ -2,37 +2,100 @@ using System.IO; using System.Collections.Generic; using UnityEngine; +using System.Text; namespace YooAsset.Editor { internal static class BuildLogger { - private static bool _enableLog = true; + private const int MAX_LOG_BUFFER_SIZE = 1024 * 1024 * 2; //2MB - public static void InitLogger(bool enableLog) + private static bool _enableLog = true; + private static string _logFilePath; + + private static readonly object _lockObj = new object(); + private static readonly StringBuilder _logBuilder = new StringBuilder(MAX_LOG_BUFFER_SIZE); + + /// + /// 初始化日志系统 + /// + public static void InitLogger(bool enableLog, string logFilePath) { _enableLog = enableLog; + _logFilePath = logFilePath; + _logBuilder.Clear(); + + if (_enableLog) + { + if (string.IsNullOrEmpty(_logFilePath)) + throw new Exception("Log file path is null or empty !"); + + Debug.Log($"Logger initialized at {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); + } + } + + /// + /// 关闭日志系统 + /// + public static void Shuntdown() + { + if (_enableLog) + { + lock (_lockObj) + { + try + { + if (File.Exists(_logFilePath)) + File.Delete(_logFilePath); + + FileUtility.CreateFileDirectory(_logFilePath); + File.WriteAllText(_logFilePath, _logBuilder.ToString(), Encoding.UTF8); + _logBuilder.Clear(); + } + catch (Exception ex) + { + Debug.LogError($"Failed to write log file: {ex.Message}"); + } + } + } } public static void Log(string message) { if (_enableLog) { + WriteLog("INFO", message); Debug.Log(message); } } public static void Warning(string message) { - Debug.LogWarning(message); + if (_enableLog) + { + WriteLog("WARN", message); + Debug.LogWarning(message); + } } public static void Error(string message) { - Debug.LogError(message); + if (_enableLog) + { + WriteLog("ERROR", message); + Debug.LogError(message); + } } - public static string GetErrorMessage(ErrorCode code, string message) { return $"[ErrorCode{(int)code}] {message}"; } + + private static void WriteLog(string level, string message) + { + lock (_lockObj) + { + string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level}] {message}"; + _logBuilder.AppendLine(logEntry); + } + } } } \ No newline at end of file