Skip to content

Commit

Permalink
Add --find
Browse files Browse the repository at this point in the history
  • Loading branch information
reemo3dp committed Jan 30, 2024
1 parent ddd04fc commit 2badd91
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 38 deletions.
93 changes: 60 additions & 33 deletions klipper_log_tool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,55 @@ set -eo pipefail

HASTEBIN_URL=${HASTEBIN_URL:-https://paste.armbian.com/documents}

remove_noise() {
grep -vE '^(Stats |Sent |Receive:|Dumping receive queue)'
}

only_last() {
FILE="$1"
# shellcheck disable=SC2016
sed -n 'H; /^Start printer at/h; ${g;p;}' "$FILE"
}

process_klipper_log() {
FILE="$1"
if [[ $RAW -eq 1 ]]; then
cat "$FILE"
return
fi
if [[ $ALL_STARTS -eq 1 ]]; then
remove_noise <"$FILE"
return
fi
only_last "$FILE" |
remove_noise
}

usage() {
cat >&2 <<EOF
Usage: $0 [ -u | --upload ] [ -y | --yes ] [ -r | --raw ] [ -a | --all-starts ] [KLIPPER_LOG_FILE]
Usage: $0 [ -f | --find ] [ -u | --upload ] [ -y | --yes ] [ -r | --raw ] [ -a | --all-starts ] [KLIPPER_LOG_FILE]...
EOF
exit 1
}

FIND=0
UPLOAD=0
YES=0
RAW=0
ALL_STARTS=0

args=$(getopt -a -o hyura --long help,yes,upload,raw,all-starts -- "$@")
args=$(getopt -a -o fhyura --long find,help,yes,upload,raw,all-starts -- "$@")
# shellcheck disable=SC2181
if [[ $? -gt 0 ]]; then
usage
fi
eval set -- "${args}"
while :; do
case $1 in
-f | --find)
FIND=1
shift
;;
-u | --upload)
UPLOAD=1
shift
Expand Down Expand Up @@ -52,42 +81,41 @@ while :; do
esac
done

if [[ $# -eq 0 ]]; then
FILE="$HOME/printer_data/logs/klippy.log"
if [[ ! -f $FILE ]]; then
echo >&2 "No file specified and default file $FILE does not exist."
usage
# shellcheck disable=SC2206
FILES=($@)

# If empty, check if default exists and replace that with FILES
if [[ ${#FILES[@]} -eq 0 ]]; then
if [[ $FIND -eq 1 ]]; then
FILES=($(find /home -type f -name klippy.log 2>/dev/null))
if [[ ${#FILES[@]} -eq 0 ]]; then
echo >&2 "No klippy.log files found."
exit 1
fi
else
FILE="$HOME/printer_data/logs/klippy.log"
if [[ ! -f $FILE ]]; then
echo >&2 "No file specified and default file $FILE does not exist."
usage
fi
FILES=("$FILE")
fi
else
FILE="$1"
fi

set -u

remove_noise() {
grep -vE '^(Stats |Sent |Receive:|Dumping receive queue)'
}

only_last() {
FILE="$1"
sed -n 'H; /^Start printer at/h; ${g;p;}' "$FILE"
}

process_klipper_log() {
FILE="$1"
if [[ $RAW -eq 1 ]]; then
cat "$FILE"
return
fi
if [[ $ALL_STARTS -eq 1 ]]; then
remove_noise <"$FILE"
return
fi
only_last "$FILE" |
remove_noise
}
# Collect output from processing each file into OUTPUT
OUTPUT=""
for FILE in "${FILES[@]}"; do
set +e
OUTPUT="${OUTPUT}Logfile: $FILE
--------------------------------------------
"
OUTPUT="${OUTPUT}$(process_klipper_log "$FILE")
"
set -e
done

OUTPUT=$(process_klipper_log "$FILE")
echo "$OUTPUT"

if [[ $UPLOAD -eq 1 ]]; then
Expand All @@ -104,4 +132,3 @@ if [[ $UPLOAD -eq 1 ]]; then
echo ""
echo "Share the following url: ${HASTEBIN_URL%documents}$KEY"
fi

60 changes: 55 additions & 5 deletions smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ tests:
- name: Considers only the last start
args:
- test/only_last_log.txt
stdout: |
stdout: |+
Logfile: test/only_last_log.txt
--------------------------------------------
Start printer at Sat May 6 16:29:38 2023 (1683390578.7 4387047.6)
===== Config file =====
[virtual_sdcard]
Expand All @@ -20,13 +22,17 @@ tests:
[mcu]
serial =
- name: Remove stats, received, sent and dumping messages
args:
- test/remove_noise.txt
stdout: |
stdout: |+
Logfile: test/remove_noise.txt
--------------------------------------------
Start printer at Sat May 6 16:29:38 2023 (1683390578.7 4387047.6)
Empty otherwise
Dumping serial stats: bytes_write=5424 bytes_read=4673 bytes_retransmit=9 bytes_invalid=0 send_seq=219 receive_seq=219 retransmit_seq=2 srtt=0.001 rttvar=0.001 rto=0.025 ready_bytes=0 upcoming_bytes=0
- name: Should upload to a service
args:
- test/uploads_to_service.txt
Expand All @@ -38,7 +44,9 @@ tests:
- name: Works without start marker
args:
- test/nostartmarker.txt
stdout: |
stdout: |+
Logfile: test/nostartmarker.txt
--------------------------------------------
webhooks client 139646904459424: Disconnected
Restarting printer
Expand All @@ -60,11 +68,14 @@ tests:
[mcu]
serial =
- name: Raw returns everything
args:
- test/raw.txt
- --raw
stdout: |
stdout: |+
Logfile: test/raw.txt
--------------------------------------------
webhooks client 139646904459424: Disconnected
Restarting printer
Start printer at Sat May 6 16:29:38 2023 (1683390578.7 4387047.6)
Expand All @@ -90,11 +101,14 @@ tests:
[mcu]
serial =
- name: All Starts returns starts but no noise
args:
- test/raw.txt
- --all-starts
stdout: |
stdout: |+
Logfile: test/raw.txt
--------------------------------------------
webhooks client 139646904459424: Disconnected
Restarting printer
Start printer at Sat May 6 16:29:38 2023 (1683390578.7 4387047.6)
Expand All @@ -118,3 +132,39 @@ tests:
[mcu]
serial =
- name: Default looks for $HOME/printer_data/logs/klippy.log
command:
shell:
- bash
script: |
docker run --rm -v $PWD:/mnt alpine:latest sh -c 'apk add bash 2&>1 > /dev/null &&
mkdir -p /root/printer_data/logs &&
touch /root/printer_data/logs/klippy.log &&
bash /mnt/klipper_log_tool.sh'
stdout: |+
Logfile: /root/printer_data/logs/klippy.log
--------------------------------------------
- name: Find looks aggressively for all klipper logs
command:
shell:
- bash
script: |
docker run --rm -v $PWD:/mnt alpine:latest sh -c 'apk add bash 2&>1 > /dev/null &&
mkdir -p /home/woop/printer_data/logs &&
mkdir -p /home/woop/klipper/logs &&
mkdir -p /root/ &&
touch /home/woop/printer_data/logs/klippy.log &&
touch /home/woop/klipper/logs/klippy.log &&
touch /root/klippy.log &&
bash /mnt/klipper_log_tool.sh --find'
stdout: |+
Logfile: /home/woop/printer_data/logs/klippy.log
--------------------------------------------
Logfile: /home/woop/klipper/logs/klippy.log
--------------------------------------------

0 comments on commit 2badd91

Please sign in to comment.