Last active
February 25, 2023 00:37
-
-
Save Low-power/a24ffa00c172fc44390854d8849fc660 to your computer and use it in GitHub Desktop.
Fast, ash-compatible reimplementation of Anthony's logcat-color.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Colorize logcat(1) output | |
# Copyright 2015-2020 Rivoreo | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE | |
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# This is a reimplementation of Anthony Lee's logcat-color.sh, which is a | |
# bash(1) script, available at https://github.com/M0xkLurk3r/logcat-color | |
# | |
# Unlike Anthony Lee's implementation, this implementation is compatible with | |
# Almquist shell (ash), Korn shell (ksh) and of course BASH; it is also 2 | |
# times faster than Anthony Lee's implementation in most cases. | |
# Benchmark (Anthony's script is named logcat-color.bash in this test): | |
# | |
# $ time bash logcat-color.bash < ~/drop/android.log > /dev/null | |
# | |
# real 0m29.998s | |
# user 0m20.436s | |
# sys 0m9.424s | |
# $ time bash logcat-color.sh < ~/drop/android.log > /dev/null | |
# | |
# real 0m16.722s | |
# user 0m14.624s | |
# sys 0m2.088s | |
# $ time dash logcat-color.sh < ~/drop/android.log > /dev/null | |
# | |
# real 0m20.920s | |
# user 0m3.412s | |
# sys 0m17.500s | |
# $ time busybox ash logcat-color.sh < ~/drop/android.log > /dev/null | |
# | |
# real 0m23.596s | |
# user 0m4.436s | |
# sys 0m19.144s | |
# $ time ksh logcat-color.sh < ~/drop/android.log > /dev/null | |
# | |
# real 0m5.552s | |
# user 0m4.296s | |
# sys 0m1.248s | |
IFS= | |
while read -r line; do case "$line" in | |
----*) | |
printf '\033[7;37m%s\033[0m\n' "$line" | |
;; | |
*) | |
IFS=" " | |
set -- $line | |
if [ $# -lt 6 ]; then | |
printf '%s\n' "$line" | |
else | |
printf '\033[0;35m%s %s\033[0m ' "$1" "$2" | |
skip=0 | |
len=${#3} | |
case $len in | |
0) | |
pad1=" " | |
;; | |
1) | |
pad1=" " | |
;; | |
2) | |
pad1=" " | |
;; | |
3) | |
pad1=" " | |
;; | |
4) | |
pad1=" " | |
;; | |
*) | |
pad1= | |
skip=$((len-5)) | |
;; | |
esac | |
len=${#4} | |
case $len in | |
0) | |
pad2=" " | |
;; | |
1) | |
pad2=" " | |
;; | |
2) | |
pad2=" " | |
;; | |
3) | |
pad2=" " | |
;; | |
4) | |
pad2=" " | |
;; | |
*) | |
pad2= | |
skip=$((skip+(len-5))) | |
;; | |
esac | |
printf '%s\033[0;32m%s\033[0m %s\033[2;32m%s\033[0m ' "$pad1" "$3" "$pad2" "$4" | |
case "$5" in | |
F|E) | |
printf '\033[1;31m' | |
;; | |
W) | |
printf '\033[0;33m' | |
;; | |
D) | |
printf '\033[4;37m' | |
;; | |
A) | |
printf '\033[0;32m' | |
;; | |
esac | |
cut="???????????????????????????????" | |
while [ $skip -gt 0 ]; do | |
cut="?$cut" | |
skip=$((skip-1)) | |
done | |
printf '%s\033[0m\n' "${line#$cut}" | |
fi | |
IFS= | |
;; | |
esac done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment