Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add script to pretty print server log#146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
juruen merged 2 commits intomainfromjuruen/pretty-print-log
Apr 7, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletionsscript/prettyprint-log
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
#!/bin/bash

# Script to pretty print the output of the github-mcp-server
# log.
#
# It uses colored output when running on a terminal.

# show script help
show_help() {
cat <<EOF
Usage: $(basename "$0") [file]

If [file] is provided, input is read from that file.
If no argument is given, input is read from stdin.

Options:
-h, --help Show this help message and exit
EOF
}

# choose color for stdin or stdout if we are printing to
# an actual terminal
color(){
io="$1"
if [[ "$io" == "stdin" ]]; then
color="\033[0;32m" # green
else
color="\033[0;36m" # cyan
fi
if [ ! $is_terminal = "1" ]; then
color=""
fi
echo -e "${color}[$io]"
}

# reset code if we are printing to an actual terminal
reset(){
if [ ! $is_terminal = "1" ]; then
return
fi
echo -e "\033[0m"
}


# Handle -h or --help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi

# Determine input source
if [[ -n "$1" ]]; then
if [[ ! -r "$1" ]]; then
echo "Error: File '$1' not found or not readable." >&2
exit 1
fi
input="$1"
else
input="/dev/stdin"
fi

# check if we are in a terminal for showing colors
if test -t 1; then
is_terminal="1"
else
is_terminal="0"
fi

# Processs each log line, print whether is stdin or stdout, using different
# colors if we output to a terminal, and pretty print json data using jq
sed -nE 's/^.*\[(stdin|stdout)\]:.* ([0-9]+) bytes: (.*)\\n"$/\1 \2 \3/p' $input |
while read -r io bytes json; do
# Unescape the JSON string safely
unescaped=$(echo "$json" | awk '{ print "echo -e \"" $0 "\" | jq ." }' | bash)
echo "$(color $io)($bytes bytes):$(reset)"
echo "$unescaped" | jq .
echo
done

[8]ページ先頭

©2009-2025 Movatter.jp