Added timestamps to project logging
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
# ./monitor.sh -b 9600 Override baud rate
|
||||
# ./monitor.sh --board mega Use baud from a named board
|
||||
# ./monitor.sh --watch Reconnect after reset/replug
|
||||
# ./monitor.sh --timestamps Prepend [HH:MM:SS.mmm] to each line
|
||||
# ./monitor.sh --log session.log Also write output to a file
|
||||
#
|
||||
# Prerequisites: arduino-cli in PATH
|
||||
|
||||
@@ -63,6 +65,8 @@ fi
|
||||
PORT=""
|
||||
DO_WATCH=0
|
||||
BOARD_NAME=""
|
||||
DO_TIMESTAMPS=0
|
||||
LOG_FILE=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
@@ -70,10 +74,15 @@ while [[ $# -gt 0 ]]; do
|
||||
-b|--baud) BAUD="$2"; shift 2 ;;
|
||||
--board) BOARD_NAME="$2"; shift 2 ;;
|
||||
--watch) DO_WATCH=1; shift ;;
|
||||
--timestamps) DO_TIMESTAMPS=1; shift ;;
|
||||
--log) LOG_FILE="$2"; shift 2 ;;
|
||||
-h|--help)
|
||||
echo "Usage: ./monitor.sh [-p PORT] [-b BAUD] [--board NAME] [--watch]"
|
||||
echo " [--timestamps] [--log FILE]"
|
||||
echo " Opens serial monitor. Baud rate from .anvil.toml."
|
||||
echo " --board NAME selects a board from [boards.NAME]."
|
||||
echo " --board NAME selects a board from [boards.NAME]."
|
||||
echo " --timestamps prepend [HH:MM:SS.mmm] to each line."
|
||||
echo " --log FILE also write output to a file."
|
||||
exit 0
|
||||
;;
|
||||
*) die "Unknown option: $1" ;;
|
||||
@@ -180,10 +189,33 @@ if [[ -z "$PORT" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# -- Output filter ---------------------------------------------------------
|
||||
# Pipes stdin through optional timestamping and file logging.
|
||||
monitor_filter() {
|
||||
if [[ $DO_TIMESTAMPS -eq 1 ]] && [[ -n "$LOG_FILE" ]]; then
|
||||
while IFS= read -r line; do
|
||||
local ts
|
||||
ts="[$(date '+%H:%M:%S.%3N')]"
|
||||
printf "%s %s\n" "$ts" "$line"
|
||||
printf "%s %s\n" "$ts" "$line" >> "$LOG_FILE"
|
||||
done
|
||||
elif [[ $DO_TIMESTAMPS -eq 1 ]]; then
|
||||
while IFS= read -r line; do
|
||||
printf "[%s] %s\n" "$(date '+%H:%M:%S.%3N')" "$line"
|
||||
done
|
||||
elif [[ -n "$LOG_FILE" ]]; then
|
||||
tee -a "$LOG_FILE"
|
||||
else
|
||||
cat
|
||||
fi
|
||||
}
|
||||
|
||||
# -- Watch mode ------------------------------------------------------------
|
||||
if [[ $DO_WATCH -eq 1 ]]; then
|
||||
echo "${CYN}${BLD}Persistent monitor on ${PORT} at ${BAUD} baud${RST}"
|
||||
echo "Reconnects after upload / reset / replug."
|
||||
[[ $DO_TIMESTAMPS -eq 1 ]] && echo "Timestamps enabled."
|
||||
[[ -n "$LOG_FILE" ]] && echo "Logging to: $LOG_FILE"
|
||||
echo "Press Ctrl+C to exit."
|
||||
echo ""
|
||||
|
||||
@@ -191,7 +223,8 @@ if [[ $DO_WATCH -eq 1 ]]; then
|
||||
|
||||
while true; do
|
||||
if [[ -e "$PORT" ]]; then
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD" 2>/dev/null || true
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD" 2>/dev/null \
|
||||
| monitor_filter || true
|
||||
echo "${YLW}--- ${PORT} disconnected ---${RST}"
|
||||
else
|
||||
echo "${CYN}--- Waiting for ${PORT} ...${RST}"
|
||||
@@ -205,7 +238,9 @@ if [[ $DO_WATCH -eq 1 ]]; then
|
||||
done
|
||||
else
|
||||
echo "Opening serial monitor on $PORT at $BAUD baud..."
|
||||
[[ $DO_TIMESTAMPS -eq 1 ]] && echo "Timestamps enabled."
|
||||
[[ -n "$LOG_FILE" ]] && echo "Logging to: $LOG_FILE"
|
||||
echo "Press Ctrl+C to exit."
|
||||
echo ""
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD"
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD" | monitor_filter
|
||||
fi
|
||||
Reference in New Issue
Block a user