Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pipe support and make DEFAULT_TEMPLATE overridable #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
38 changes: 25 additions & 13 deletions b-log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#set -o pipefail # prevents errors in a pipeline from being masked

B_LOG_APPNAME="b-log"
B_LOG_VERSION=1.2.0
B_LOG_VERSION=1.2.2

# --- global variables ----------------------------------------------
# log levels
Expand Down Expand Up @@ -50,7 +50,7 @@ readonly LOG_LEVEL_ALL=-1 # all enabled
# 5: log message
# 6: space
# 7: filename
B_LOG_DEFAULT_TEMPLATE="[@23:1@][@6:2@][@3@:@3:4@] @5@" # default template
: "${B_LOG_DEFAULT_TEMPLATE:="[@23:1@][@6:2@][@3@:@3:4@] @5@"}" # default template

# log levels information
# level code, level name, level template, prefix(colors etc.), suffix(colors etc.)
Expand Down Expand Up @@ -258,10 +258,10 @@ function B_LOG_convert_template() {
log_layout_part="${B_LOG_LOG_LEVEL_NAME}"
;;
3) # function name
log_layout_part="${FUNCNAME[3]}"
log_layout_part="${FUNCNAME[4]}"
;;
4) # line number
log_layout_part="${BASH_LINENO[2]}"
log_layout_part="${BASH_LINENO[3]}"
;;
5) # message
log_layout_part="${B_LOG_LOG_MESSAGE}"
Expand All @@ -270,7 +270,7 @@ function B_LOG_convert_template() {
log_layout_part=" "
;;
7) # file name
log_layout_part="$(basename ${BASH_SOURCE[3]})"
log_layout_part="$(basename ${BASH_SOURCE[4]})"
;;
*)
B_LOG_ERR '1' "unknown template parameter: '$selector'"
Expand All @@ -294,25 +294,37 @@ function B_LOG_convert_template() {
}

function B_LOG_print_message() {
# @description
# @param $1 log type
# $2... the rest are messages
local file_directory=""
local err_ret_code=0
B_LOG_TS=$(date +"${B_LOG_TS_FORMAT}") # get the date
log_level=${1:-"$LOG_LEVEL_ERROR"}
shift

if [ ${log_level} -gt ${LOG_LEVEL} ]; then # check log level
if [ ! ${LOG_LEVEL} -eq ${LOG_LEVEL_ALL} ]; then # check log level
if [ -z "${*:-}" ]; then # if message is empty, consume stdin
cat > /dev/null
fi
return 0;
fi
fi
# log level bigger as LOG_LEVEL? and level is not -1? return

shift
local message=${*:-}
if [ -z "$message" ]; then # if message is empty, get from stdin
message="$(cat /dev/stdin)"
while read -r line; do
B_LOG_print_message_line "${line}"
done < /dev/stdin
else
B_LOG_print_message_line "${message}"
fi

}

B_LOG_print_message_line() {
local message=${1:-}

local file_directory=""
local err_ret_code=0
B_LOG_TS=$(date +"${B_LOG_TS_FORMAT}") # get the date

B_LOG_LOG_MESSAGE="${message}"
B_LOG_get_log_level_info "${log_level}" || true
B_LOG_convert_template ${LOG_FORMAT} || true
Expand Down