日志系统优化

This commit is contained in:
Nanako 2025-03-08 15:28:06 +08:00
parent b74c4497b4
commit fcfb091bda

View File

@ -31,7 +31,7 @@ declare -A CUSTOM_PACKAGES=(
["golang"]="feeds/packages/lang/golang=https://github.com/kenzok8/golang=main=true"
["partexp"]="package/luci-app-partexp=https://github.com/sirpdboy/luci-app-partexp=main=false"
["openclash"]="package/openclash=https://github.com/vernesong/OpenClash=dev=true"
["zerotier"]="package/luci-app-zerotier=https://github.com/mmc1987/luci-app-zerotier.git=main=false"
["zerotier"]="package/luci-app-zerotier=https://github.com/zhengmz/luci-app-zerotier=master=false"
["5gsupport"]="package/openwrt-app-actions=https://github.com/Siriling/openwrt-app-actions.git=main=true"
["kucat"]="package/luci-theme-kucat=https://github.com/sirpdboy/luci-theme-kucat.git=main=false"
["mt76"]="package/firmware/mt76=https://github.com/openwrt/mt76.git=master=false"
@ -77,6 +77,42 @@ create_build_log() {
return 0
}
# 重定向输出到指定的编译日志文件
redirect_to_build_log() {
local log_file=$1
# 保存当前的输出重定向状态
LOGGING_TO_BUILD_LOG=true
# 重定向输出到编译日志文件
exec > >(while read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line" >> "$log_file"
done)
# 重定向错误输出到编译日志文件
exec 2> >(while read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line" >> "$log_file"
done >&2)
}
# 恢复输出到主日志
restore_main_logging() {
LOGGING_TO_BUILD_LOG=false
# 恢复到主日志
if [ "$LOGGING_ENABLED" = true ]; then
# 标准输出重定向
exec > >(while read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line" | tee -a "$LOG_FILE"
done)
# 标准错误重定向
exec 2> >(while read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line" | tee -a "$LOG_FILE"
done >&2)
fi
}
# 禁用日志输出
disable_logging() {
LOGGING_ENABLED=false
@ -128,6 +164,20 @@ log() {
printf "[%s] ${color}[%s]${NC}: %s\n" "$timestamp" "$level_str" "$message"
}
# 同时记录到主日志和编译日志的函数
log_to_both() {
local level=$1
local message=$2
local build_log=$3
# 记录到主日志
log "$level" "$message"
# 记录到编译日志
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$timestamp] [$level]: $message" >> "$build_log"
}
#---------------自定义包管理函数---------------#
# 更新单个自定义包
update_single_package() {
@ -265,6 +315,27 @@ update_feeds() {
return 0
}
# 执行编译命令并仅记录到指定日志文件
run_build_command() {
local cmd=$1
local log_file=$2
local msg=$3
log_to_both "INFO" "$msg" "$log_file"
# 重定向输出到编译日志
redirect_to_build_log "$log_file"
# 执行编译命令
local result=0
eval "$cmd" || result=1
# 恢复输出到主日志
restore_main_logging
return $result
}
# 编译固件
build_firmware() {
# 创建新的编译日志
@ -276,8 +347,8 @@ build_firmware() {
# 下载依赖
log "INFO" "下载依赖包..."
make download -j$(nproc) V=s 2>&1 | tee -a "$current_log" || {
log "ERROR" "依赖下载失败"
run_build_command "make download -j$(nproc) V=s" "$current_log" "正在下载依赖包..." || {
log "ERROR" "依赖下载失败,详见日志: $current_log"
return 1
}
@ -285,7 +356,7 @@ build_firmware() {
local cpu_cores=$(nproc)
log "INFO" "使用 $cpu_cores 线程开始编译..."
if make -j$cpu_cores V=s 2>&1 | tee -a "$current_log"; then
if run_build_command "make -j$cpu_cores V=s" "$current_log" "多线程编译进行中..."; then
log "INFO" "多线程编译成功完成"
return 0
else
@ -301,16 +372,16 @@ build_firmware() {
# 重新下载依赖
log "INFO" "重新下载依赖包..."
make download -j1 V=s 2>&1 | tee -a "$current_log" || {
log "ERROR" "单线程依赖下载也失败"
run_build_command "make download -j1 V=s" "$current_log" "单线程模式重新下载依赖..." || {
log "ERROR" "单线程依赖下载也失败,详见日志: $current_log"
return 1
}
if make -j1 V=s 2>&1 | tee -a "$current_log"; then
if run_build_command "make -j1 V=s" "$current_log" "单线程编译进行中..."; then
log "INFO" "单线程编译成功完成"
return 0
else
log "ERROR" "编译失败,即使在单线程模式下"
log "ERROR" "编译失败,即使在单线程模式下。详见日志: $current_log"
return 1
fi
fi
@ -514,4 +585,4 @@ main() {
}
# 执行主函数
main "$@"
main "$@"