1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 16:10:08 +00:00

fix(log): preserve Mole runtime logs during clean

This commit is contained in:
Tw93
2026-03-19 09:52:27 +08:00
parent 3b46b4ade6
commit 584e10cdc4
6 changed files with 107 additions and 26 deletions

View File

@@ -802,6 +802,10 @@ should_protect_path() {
*/Library/Preferences/com.apple.dock.plist | */Library/Preferences/com.apple.finder.plist)
return 0
;;
# Protect Mole's own runtime logs so cleanup cannot delete its active log targets.
*/Library/Logs/mole | */Library/Logs/mole/ | */Library/Logs/mole/*)
return 0
;;
# Bluetooth and WiFi configurations
*/ByHost/com.apple.bluetooth.* | */ByHost/com.apple.wifi.*)
return 0

View File

@@ -37,6 +37,22 @@ fi
# Log Rotation
# ============================================================================
append_log_line() {
local file_path="$1"
local line="${2:-}"
ensure_user_file "$file_path"
printf '%s\n' "$line" >> "$file_path" 2> /dev/null || true
}
append_log_lines() {
local file_path="$1"
shift
ensure_user_file "$file_path"
printf '%s\n' "$@" >> "$file_path" 2> /dev/null || true
}
# Rotate log file if it exceeds maximum size
rotate_log_once() {
# Skip if already checked this session
@@ -81,9 +97,9 @@ log_info() {
echo -e "${BLUE}$1${NC}"
local timestamp
timestamp=$(get_timestamp)
echo "[$timestamp] INFO: $1" >> "$LOG_FILE" 2> /dev/null || true
append_log_line "$LOG_FILE" "[$timestamp] INFO: $1"
if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] INFO: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "[$timestamp] INFO: $1"
fi
}
@@ -92,9 +108,9 @@ log_success() {
echo -e " ${GREEN}${ICON_SUCCESS}${NC} $1"
local timestamp
timestamp=$(get_timestamp)
echo "[$timestamp] SUCCESS: $1" >> "$LOG_FILE" 2> /dev/null || true
append_log_line "$LOG_FILE" "[$timestamp] SUCCESS: $1"
if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] SUCCESS: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "[$timestamp] SUCCESS: $1"
fi
}
@@ -103,9 +119,9 @@ log_warning() {
echo -e "${YELLOW}$1${NC}"
local timestamp
timestamp=$(get_timestamp)
echo "[$timestamp] WARNING: $1" >> "$LOG_FILE" 2> /dev/null || true
append_log_line "$LOG_FILE" "[$timestamp] WARNING: $1"
if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] WARNING: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "[$timestamp] WARNING: $1"
fi
}
@@ -114,9 +130,9 @@ log_error() {
echo -e "${YELLOW}${ICON_ERROR}${NC} $1" >&2
local timestamp
timestamp=$(get_timestamp)
echo "[$timestamp] ERROR: $1" >> "$LOG_FILE" 2> /dev/null || true
append_log_line "$LOG_FILE" "[$timestamp] ERROR: $1"
if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] ERROR: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "[$timestamp] ERROR: $1"
fi
}
@@ -126,7 +142,7 @@ debug_log() {
echo -e "${GRAY}[DEBUG]${NC} $*" >&2
local timestamp
timestamp=$(get_timestamp)
echo "[$timestamp] DEBUG: $*" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "[$timestamp] DEBUG: $*"
fi
}
@@ -163,7 +179,7 @@ log_operation() {
local log_line="[$timestamp] [$command] $action $path"
[[ -n "$detail" ]] && log_line+=" ($detail)"
echo "$log_line" >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
append_log_line "$OPERATIONS_LOG_FILE" "$log_line"
}
# Log session start marker
@@ -175,10 +191,10 @@ log_operation_session_start() {
local timestamp
timestamp=$(get_timestamp)
{
echo ""
echo "# ========== $command session started at $timestamp =========="
} >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
append_log_lines \
"$OPERATIONS_LOG_FILE" \
"" \
"# ========== $command session started at $timestamp =========="
}
# shellcheck disable=SC2329
@@ -198,9 +214,9 @@ log_operation_session_end() {
size_human="0B"
fi
{
echo "# ========== $command session ended at $timestamp, $items items, $size_human =========="
} >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
append_log_line \
"$OPERATIONS_LOG_FILE" \
"# ========== $command session ended at $timestamp, $items items, $size_human =========="
}
# Enhanced debug logging for operations
@@ -214,11 +230,18 @@ debug_operation_start() {
[[ -n "$operation_desc" ]] && echo -e "${GRAY}[DEBUG] $operation_desc${NC}" >&2
# Also log to file
{
echo ""
echo "=== $operation_name ==="
[[ -n "$operation_desc" ]] && echo "Description: $operation_desc"
} >> "$DEBUG_LOG_FILE" 2> /dev/null || true
if [[ -n "$operation_desc" ]]; then
append_log_lines \
"$DEBUG_LOG_FILE" \
"" \
"=== $operation_name ===" \
"Description: $operation_desc"
else
append_log_lines \
"$DEBUG_LOG_FILE" \
"" \
"=== $operation_name ==="
fi
fi
}
@@ -232,7 +255,7 @@ debug_operation_detail() {
echo -e "${GRAY}[DEBUG] $detail_type: $detail_value${NC}" >&2
# Also log to file
echo "$detail_type: $detail_value" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "$detail_type: $detail_value"
fi
}
@@ -252,7 +275,7 @@ debug_file_action() {
echo -e "${GRAY}[DEBUG] $action: $msg${NC}" >&2
# Also log to file
echo "$action: $msg" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
append_log_line "$DEBUG_LOG_FILE" "$action: $msg"
fi
}