169 lines
4.5 KiB
Plaintext
169 lines
4.5 KiB
Plaintext
|
|
|
||
|
|
#!/bin/bash
|
||
|
|
cd "$(dirname "$(readlink -f "$0")")"
|
||
|
|
|
||
|
|
G_DIR="../logs"
|
||
|
|
LOG_DIR=${G_DIR}
|
||
|
|
|
||
|
|
|
||
|
|
# Help function
|
||
|
|
function help() {
|
||
|
|
echo -e "Usage: \n\t$(basename "$0") <program_name> {-t|-d|-i|-w|-e|-f}\n"
|
||
|
|
echo -e "\t -t|-v : \t Extract lines with [T]race level: 6 "
|
||
|
|
echo -e "\t -d : \t Extract lines with [D]ebug level: 5 "
|
||
|
|
echo -e "\t -i : \t Extract lines with [I]nfo level: 4 "
|
||
|
|
echo -e "\t -w : \t Extract lines with [W]arn level: 3 "
|
||
|
|
echo -e "\t -e : \t Extract lines with [E]rror level: 2 "
|
||
|
|
echo -e "\t -c|-f : \t Extract lines with [C]ritical(Fault) 1 "
|
||
|
|
echo -e "\t -err : \t Extract lines with [E]rror level: 2 for error.log"
|
||
|
|
echo -e "\t -clear: \t Clear the log file for the specified program, but Reverse error.log"
|
||
|
|
echo -e "\t -g : \t Get <program> log level:\t manage logic tcpcomm uartcomm ..."
|
||
|
|
echo -e "\t -s <L>: \t Set <program> log level x: [1-C, 2-E, 3-W, 4-I, 5-D, 6-T] "
|
||
|
|
echo -e ""
|
||
|
|
}
|
||
|
|
|
||
|
|
# Clear log file function
|
||
|
|
function clear_log() {
|
||
|
|
local program_name=$1
|
||
|
|
log_file="${LOG_DIR}/$program_name/$program_name.log"
|
||
|
|
log_err_file="${LOG_DIR}/$program_name/${program_name}_error.log"
|
||
|
|
if [ -f "$log_file" ]; then
|
||
|
|
> "$log_file"
|
||
|
|
echo "Log file for $program_name cleared."
|
||
|
|
# > "$log_err_file"
|
||
|
|
# echo "Log file for ${program_name}_error cleared."
|
||
|
|
else
|
||
|
|
echo "Log file for $program_name does not exist."
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Extract log function
|
||
|
|
function extract_log() {
|
||
|
|
local level=$1
|
||
|
|
local program_name=$2
|
||
|
|
log_file="${LOG_DIR}/$program_name/$program_name.log"
|
||
|
|
log_err_file="${LOG_DIR}/$program_name/${program_name}_error.log"
|
||
|
|
|
||
|
|
if [ -f "$log_file" ]; then
|
||
|
|
|
||
|
|
case "$level" in
|
||
|
|
T|t|Trace|trace|V|v|Verbose|verbose)
|
||
|
|
tail -f "$log_file" #| grep -E "\[T|Trace\]"
|
||
|
|
;;
|
||
|
|
D|d|Debug|debug)
|
||
|
|
tail -f "$log_file" | grep -vE "\[T|Trace\]" #| grep -E "\[D|Debug\]"
|
||
|
|
;;
|
||
|
|
I|i|Info|info)
|
||
|
|
tail -f "$log_file" | grep -vE "\[T|Trace\]|\[D|Debug\]" # | grep -E "\[I|Info\]"
|
||
|
|
;;
|
||
|
|
W|w|Warn|warn)
|
||
|
|
tail -f "$log_file" | grep -vE "\[T|Trace\]|\[D|Debug\]|\[I|Info\]" # | grep -E "\[W|Warn\]"
|
||
|
|
;;
|
||
|
|
E|e|Error)
|
||
|
|
tail -f "$log_file" | grep -vE "\[T|Trace\]|\[D|Debug\]|\[I|Info\]|\[W|Warn\]" # | grep -E "\[E|Error\]"
|
||
|
|
;;
|
||
|
|
err|error)
|
||
|
|
tail -f "$log_err_file"
|
||
|
|
;;
|
||
|
|
C|c|Critical|critical|F|f|Fault|fault)
|
||
|
|
tail -f "$log_file" | grep -E "\[C|Critical\]"
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
help
|
||
|
|
exit 1
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
else
|
||
|
|
echo "Log file for $program_name does not exist."
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Extract log function
|
||
|
|
function level_log() {
|
||
|
|
local cmd=$1
|
||
|
|
local program_name=$2
|
||
|
|
local level=$3
|
||
|
|
log_file="${LOG_DIR}/$program_name/$program_name.log"
|
||
|
|
lv_file="/tmp/prlog/level/$program_name"
|
||
|
|
if [ -f "$log_file" ]; then
|
||
|
|
case "$cmd" in
|
||
|
|
Get)
|
||
|
|
cat "$lv_file" #| grep -E "\[T|Trace\]"
|
||
|
|
;;
|
||
|
|
Set)
|
||
|
|
echo "${level}" > "$lv_file"
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
help
|
||
|
|
exit 1
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
else
|
||
|
|
echo "Log file for $program_name does not exist."
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
# Check if correct number of arguments is passed
|
||
|
|
if [ "$#" -lt 2 ]; then
|
||
|
|
help
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Parse options and arguments
|
||
|
|
option=$1
|
||
|
|
program_name=$2
|
||
|
|
level=$3
|
||
|
|
|
||
|
|
# swap $1 $2
|
||
|
|
if [[ "$2" = -* ]];then
|
||
|
|
option=$2
|
||
|
|
program_name=$1
|
||
|
|
fi
|
||
|
|
|
||
|
|
#echo "1: $1 2: $2"
|
||
|
|
|
||
|
|
case "$option" in
|
||
|
|
-v|--verbose)
|
||
|
|
extract_log "V" "$program_name"
|
||
|
|
;;
|
||
|
|
-t|--trace)
|
||
|
|
extract_log "V" "$program_name"
|
||
|
|
;;
|
||
|
|
-d|--debug)
|
||
|
|
extract_log "D" "$program_name"
|
||
|
|
;;
|
||
|
|
-i|--info)
|
||
|
|
extract_log "I" "$program_name"
|
||
|
|
;;
|
||
|
|
-w|--warn)
|
||
|
|
extract_log "W" "$program_name"
|
||
|
|
;;
|
||
|
|
-e|--error)
|
||
|
|
extract_log "E" "$program_name"
|
||
|
|
;;
|
||
|
|
-err|--err)
|
||
|
|
extract_log "err" "$program_name"
|
||
|
|
;;
|
||
|
|
-c|--critical)
|
||
|
|
extract_log "C" "$program_name"
|
||
|
|
;;
|
||
|
|
-f|--fault)
|
||
|
|
extract_log "C" "$program_name"
|
||
|
|
;;
|
||
|
|
-clear|--clear)
|
||
|
|
clear_log "$program_name"
|
||
|
|
;;
|
||
|
|
-g)
|
||
|
|
level_log "Get" "$program_name"
|
||
|
|
;;
|
||
|
|
-s)
|
||
|
|
level_log "Set" "$program_name" "${level}"
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
echo "[$@]"
|
||
|
|
help
|
||
|
|
exit 1
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
|
||
|
|
|