-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxsbash-utils.sh
More file actions
41 lines (34 loc) · 1.14 KB
/
dxsbash-utils.sh
File metadata and controls
41 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# dxsbash-utils.sh - Shared utilities for dxsbash scripts
# UNDER DEVELOPMENT
# Color codes
RC='\033[0m'
RED='\033[31m'
YELLOW='\033[33m'
GREEN='\033[32m'
# Logging function (silent, doesn't echo to console)
log() {
local level="$1"
local message="$2"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
local log_file="$HOME/.dxsbash/logs/dxsbash.log"
# Create log directory if it doesn't exist
mkdir -p "$(dirname "$log_file")"
# Format the log message
local formatted_log="[$timestamp] [$level] $message"
# Append to log file
echo "$formatted_log" >> "$log_file"
}
# Log rotation function
rotate_logs() {
local log_dir="$HOME/.dxsbash/logs"
local main_log="$log_dir/dxsbash.log"
local max_size=1048576 # 1MB
# Check if log exists and is larger than max size
if [ -f "$main_log" ] && [ $(stat -c %s "$main_log") -gt $max_size ]; then
local timestamp=$(date "+%Y%m%d_%H%M%S")
mv "$main_log" "$log_dir/dxsbash_$timestamp.log"
# Keep only the 5 most recent log files
ls -t "$log_dir"/dxsbash_*.log | tail -n +6 | xargs rm -f 2>/dev/null
fi
}