Skip to content

Commit

Permalink
Add LSEXCEPTION. Minor modifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvinev committed Sep 15, 2020
1 parent 411be71 commit ce0d329
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions log.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash
# Logging library for Bash
# Copyright (c) 2012 Yu-Jie Lin
#
#
# 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
Expand All @@ -24,14 +24,15 @@ LS_VERSION=0.3

LS_OUTPUT=${LS_OUTPUT:-/dev/stdout}
# XXX need more flexible templating, currently manual padding for level names
LS_DEFAULT_FMT=${LS_DEFAULT_FMT:-'[$TS][$_LS_LEVEL_STR][${FUNCNAME[1]}:${BASH_LINENO[0]}]'}
CURRENT_FILE=$(basename $0)
LS_DEFAULT_FMT=${LS_DEFAULT_FMT:-'[$TS][$_LS_LEVEL_STR][${CURRENT_FILE}:${BASH_LINENO[0]}]'}

LS_DEBUG_LEVEL=10
LS_INFO_LEVEL=20
LS_WARNING_LEVEL=30
LS_ERROR_LEVEL=40
LS_CRITICAL_LEVEL=50
LS_LEVEL=${LS_LEVEL:-$LS_WARNING_LEVEL}
LS_LEVEL=${LS_LEVEL:-$LS_DEBUG_LEVEL} # Modified
# LS_LEVELS structure:
# Level, Level Name, Level Format, Before Log Entry, After Log Entry
LS_LEVELS=(
Expand Down Expand Up @@ -78,11 +79,11 @@ LSLOG () {
[[ $# -ne 0 ]] && _MSG="$@" || _MSG="$(cat)"
if [[ "$LS_OUTPUT" = "/dev/stdout" ]] ; then
echo -ne "$_LS_LEVEL_BEGIN$OUTPUT "
echo -n "$_MSG"
echo -ne "$_MSG"
echo -e "$_LS_LEVEL_END"
elif [[ "$LS_OUTPUT" = "/dev/stderr" ]] ; then
echo -ne "$_LS_LEVEL_BEGIN$OUTPUT " >&2
echo -n "$_MSG" >&2
echo -ne "$_MSG" >&2
echo -e "$_LS_LEVEL_END" >&2
else
echo -ne "$_LS_LEVEL_BEGIN$OUTPUT " >> "$LS_OUTPUT"
Expand All @@ -99,6 +100,11 @@ alias LSERROR='LSLOG 40'
alias LSCRITICAL='LSLOG 50'
alias LSLOGSTACK='LSDEBUG Traceback ; LSCALLSTACK'

LSEXCEPTION () {
LSLOG 50 $@; LSDEBUG Traceback ; LSCALLSTACK 1
exit 1
}

# TODO Log Bash information
LSLOGBASH () {
:
Expand All @@ -113,11 +119,12 @@ LSLOGUSER () {
LSCALLSTACK () {
local i=0
local FRAMES=${#BASH_LINENO[@]}
local OFFSET=${1:-0}
# FRAMES-2 skips main, the last one in arrays
for ((i=FRAMES-2; i>=0; i--)); do
for ((i=FRAMES-2; i>=$OFFSET; i--)); do
echo ' File' \"${BASH_SOURCE[i+1]}\", line ${BASH_LINENO[i]}, in ${FUNCNAME[i+1]}
# Grab the source code of the line
sed -n "${BASH_LINENO[i]}{s/^/ /;p}" "${BASH_SOURCE[i+1]}"
sed -n "${BASH_LINENO[i]}{s/^[ \t]*/ /;p}" "${BASH_SOURCE[i+1]}"
# TODO extract arugments from "${BASH_ARGC[@]}" and "${BASH_ARGV[@]}"
# It requires `shopt -s extdebug'
done
Expand Down

0 comments on commit ce0d329

Please sign in to comment.