upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/sanitize-logs.sh
blob: 7225fe40aa13590322f96dc217d866026cdf38fc (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# sanitize-logs.sh - Truncates verbose log lines for LLM analysis
#
# Usage:
#   cargo run -- [args] 2>&1 | ./scripts/sanitize-logs.sh
#   ./scripts/sanitize-logs.sh < logfile.txt
#   ./scripts/sanitize-logs.sh --head-chars 150 --tail-chars 30 < logfile.txt

set -euo pipefail

# Default settings
HEAD_CHARS=200
TAIL_CHARS=100
MAX_LINE_LENGTH=$((HEAD_CHARS + TAIL_CHARS + 20))  # buffer for the ellipsis marker

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --head-chars)
            HEAD_CHARS="$2"
            shift 2
            ;;
        --tail-chars)
            TAIL_CHARS="$2"
            shift 2
            ;;
        --max-line)
            MAX_LINE_LENGTH="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Sanitizes log output for LLM analysis by truncating long lines."
            echo "Reads from stdin, writes to stdout."
            echo ""
            echo "Options:"
            echo "  --head-chars N    Show first N chars of long lines (default: 200)"
            echo "  --tail-chars N    Show last N chars of long lines (default: 100)"
            echo "  --max-line N      Lines shorter than this are unchanged (default: head+tail+20)"
            echo "  -h, --help        Show this help"
            echo ""
            echo "Examples:"
            echo "  cargo run -- --sync-bootstrap-relay wss://git.shakespeare.diy 2>&1 | $0"
            echo "  timeout 30s cargo run -- [args] 2>&1 | $0 > sanitized.log"
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            exit 1
            ;;
    esac
done

MAX_LINE_LENGTH=$((HEAD_CHARS + TAIL_CHARS + 20))

# Process each line
while IFS= read -r line; do
    len=${#line}
    
    if [[ $len -le $MAX_LINE_LENGTH ]]; then
        # Short line - pass through unchanged
        echo "$line"
    else
        # Long line - truncate with marker showing omitted char count
        head="${line:0:$HEAD_CHARS}"
        tail="${line: -$TAIL_CHARS}"
        omitted=$((len - HEAD_CHARS - TAIL_CHARS))
        echo "${head}...<${omitted} chars>...${tail}"
    fi
done