Skip to content

Commit b8b3335

Browse files
author
Jim Zhang
committed
feat(log): support verbosing logging
1 parent 903fd74 commit b8b3335

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

example/verbose.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
source "$(cd "${BASH_SOURCE[0]%/*}" && pwd)/../lib/oo-bootstrap.sh"
3+
4+
import util/log
5+
6+
namespace test/verbose
7+
VERBOSE=1
8+
9+
Log::AddOutput test/verbose DEBUG
10+
11+
Log "this log will be printed"
12+
V=1 Log "this log will be printed"
13+
V=2 Log "this log will be filtered"

lib/util/log.sh

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ declare -Ag __oo__logScopeOutputs
66
declare -Ag __oo__logDisabledFilter
77
declare -Ag __oo__loggers
88

9+
# Controls verbosity of the script output and logging.
10+
VERBOSE="${VERBOSE:-5}"
11+
912
Log::NameScope() {
1013
local scopeName="$1"
1114
local script="${BASH_SOURCE[1]}"
@@ -36,11 +39,16 @@ Log::DisableFilter() {
3639
}
3740

3841
Log() {
42+
# support verbosing logging
43+
local V="${V:-0}"
44+
if [[ $VERBOSE < $V ]]; then
45+
return
46+
fi
47+
3948
local callingFunction="${FUNCNAME[1]}"
4049
local callingScript="${BASH_SOURCE[1]}"
4150
local scope
42-
if [[ ! -z "${__oo__logScopes["$callingScript"]}" ]]
43-
then
51+
if [[ ! -z "${__oo__logScopes["$callingScript"]}" ]]; then
4452
scope="${__oo__logScopes["$callingScript"]}"
4553
else # just the filename without extension
4654
scope="${callingScript##*/}"
@@ -51,49 +59,38 @@ Log() {
5159
local logger
5260
local logged
5361

54-
if [[ ! -z "$subject" ]]
55-
then
56-
if [[ ! -z "${__oo__logScopeOutputs["$scope/$callingFunction/$subject"]}" ]]
57-
then
62+
if [[ ! -z "$subject" ]]; then
63+
if [[ ! -z "${__oo__logScopeOutputs["$scope/$callingFunction/$subject"]}" ]]; then
5864
loggerList="${__oo__logScopeOutputs["$scope/$callingFunction/$subject"]}"
59-
elif [[ ! -z "${__oo__logScopeOutputs["$scope/$subject"]}" ]]
60-
then
65+
elif [[ ! -z "${__oo__logScopeOutputs["$scope/$subject"]}" ]]; then
6166
loggerList="${__oo__logScopeOutputs["$scope/$subject"]}"
62-
elif [[ ! -z "${__oo__logScopeOutputs["$subject"]}" ]]
63-
then
67+
elif [[ ! -z "${__oo__logScopeOutputs["$subject"]}" ]]; then
6468
loggerList="${__oo__logScopeOutputs["$subject"]}"
6569
fi
6670

67-
loggers=( ${loggerList//;/ } )
68-
for logger in "${loggers[@]}"
69-
do
71+
loggers=(${loggerList//;/ })
72+
for logger in "${loggers[@]}"; do
7073
subject="${subject:-LOG}" Log::Using "$logger" "$@"
7174
logged=true
7275
done
7376
fi
7477

75-
if [[ ! -z "${__oo__logScopeOutputs["$scope/$callingFunction"]}" ]]
76-
then
77-
if [[ -z $logged ]] || [[ ${__oo__logDisabledFilter["$scope/$callingFunction"]} == true || ${__oo__logDisabledFilter["$scope"]} == true ]]
78-
then
78+
if [[ ! -z "${__oo__logScopeOutputs["$scope/$callingFunction"]}" ]]; then
79+
if [[ -z $logged ]] || [[ ${__oo__logDisabledFilter["$scope/$callingFunction"]} == true || ${__oo__logDisabledFilter["$scope"]} == true ]]; then
7980
loggerList="${__oo__logScopeOutputs["$scope/$callingFunction"]}"
80-
loggers=( ${loggerList//;/ } )
81-
for logger in "${loggers[@]}"
82-
do
83-
subject="${subject:-LOG}" Log::Using "$logger" "$@"
84-
logged=true
81+
loggers=(${loggerList//;/ })
82+
for logger in "${loggers[@]}"; do
83+
subject="${subject:-LOG}" Log::Using "$logger" "$@"
84+
logged=true
8585
done
8686
fi
8787
fi
8888

89-
if [[ ! -z "${__oo__logScopeOutputs["$scope"]}" ]]
90-
then
91-
if [[ -z $logged ]] || [[ ${__oo__logDisabledFilter["$scope"]} == true ]]
92-
then
89+
if [[ ! -z "${__oo__logScopeOutputs["$scope"]}" ]]; then
90+
if [[ -z $logged ]] || [[ ${__oo__logDisabledFilter["$scope"]} == true ]]; then
9391
loggerList="${__oo__logScopeOutputs["$scope"]}"
94-
loggers=( ${loggerList//;/ } )
95-
for logger in "${loggers[@]}"
96-
do
92+
loggers=(${loggerList//;/ })
93+
for logger in "${loggers[@]}"; do
9794
subject="${subject:-LOG}" Log::Using "$logger" "$@"
9895
done
9996
fi
@@ -109,29 +106,28 @@ Log::RegisterLogger() {
109106
Log::Using() {
110107
local logger="$1"
111108
shift
112-
if [[ ! -z ${__oo__loggers["$logger"]} ]]
113-
then
114-
${__oo__loggers["$logger"]} "$@"
109+
if [[ ! -z ${__oo__loggers["$logger"]} ]]; then
110+
${__oo__loggers["$logger"]} "$@"
115111
fi
116112
}
117113

118114
Logger::DEBUG() {
119-
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) DEBUG "$@"
115+
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) DEBUG "$@"
120116
}
121117
Logger::ERROR() {
122-
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Red) ERROR "$@"
118+
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Red) ERROR "$@"
123119
}
124120
Logger::INFO() {
125-
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Blue) INFO "$@"
121+
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Blue) INFO "$@"
126122
}
127123
Logger::WARN() {
128-
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) WARN "$@"
124+
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) WARN "$@"
129125
}
130126
Logger::CUSTOM() {
131-
Console::WriteStdErr "$(UI.Color.Yellow)[${subject^^}] $(UI.Color.Default)$* "
127+
Console::WriteStdErr "$(UI.Color.Yellow)[${subject^^}] $(UI.Color.Default)$* "
132128
}
133129
Logger::DETAILED() {
134-
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) "${subject^^}" "$@"
130+
Console::WriteStdErrAnnotated "${BASH_SOURCE[3]##*/}" ${BASH_LINENO[2]} $(UI.Color.Yellow) "${subject^^}" "$@"
135131
}
136132

137133
Log::RegisterLogger STDERR Console::WriteStdErr

0 commit comments

Comments
 (0)