improve log system
This commit is contained in:
Vendored
+30
-3
@@ -9,6 +9,7 @@
|
||||
GITHUB_REPO="https://gitea.spiralragetech.com/tiago.aica/scripts/raw/branch/main/debian"
|
||||
SCRIPT_DIR="/usr/local/bin"
|
||||
LOG_FILE="/var/log/raspberry_setup.log"
|
||||
MAX_LOG_SIZE_KB=1024 # 1MB
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
@@ -17,9 +18,33 @@ YELLOW='\033[1;33m'
|
||||
BLUE='\033[1;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to log messages
|
||||
# Função de Log (modificada para usar logger)
|
||||
log() {
|
||||
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# Escreve no ficheiro de log
|
||||
echo "$timestamp: $message" | tee -a "$LOG_FILE"
|
||||
|
||||
# Envia para o syslog (opcional)
|
||||
logger -t "raspberry_setup" "$message"
|
||||
}
|
||||
|
||||
# Função para limitar o tamanho do log
|
||||
limit_log_size() {
|
||||
local log_file="$1"
|
||||
local max_size_kb="$2"
|
||||
local max_size_bytes=$((max_size_kb * 1024))
|
||||
|
||||
if [ -f "$log_file" ]; then
|
||||
local current_size=$(stat -c %s "$log_file" 2>/dev/null || wc -c < "$log_file" 2>/dev/null)
|
||||
|
||||
if [ "$current_size" -gt "$max_size_bytes" ]; then
|
||||
log "Aviso: Ficheiro de log $log_file excedeu $max_size_kb KB. A truncar..."
|
||||
tail -n 500 "$log_file" > "${log_file}.tmp" && mv "${log_file}.tmp" "$log_file"
|
||||
log "Ficheiro de log truncado. As últimas 500 linhas foram mantidas."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to ask for confirmation
|
||||
@@ -482,7 +507,9 @@ main() {
|
||||
log "Starting Raspberry Pi setup..."
|
||||
|
||||
# Create script directory if it doesn't exist
|
||||
mkdir -p "$SCRIPT_DIR"
|
||||
mkdir -p "$(dirname "$LOG_FILE")"
|
||||
limit_log_size "$LOG_FILE" "$MAX_LOG_SIZE_KB" # Verifica o tamanho do log no início
|
||||
|
||||
|
||||
# Install dependencies
|
||||
install_dependencies
|
||||
|
||||
Reference in New Issue
Block a user