• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dynamotn / dybatpho / 13771606036

10 Mar 2025 06:03PM UTC coverage: 99.194% (-0.8%) from 100.0%
13771606036

push

github

dynamotn
style: follow my new style guide

2 of 3 new or added lines in 2 files covered. (66.67%)

1 existing line in 1 file now uncovered.

246 of 248 relevant lines covered (99.19%)

38.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.92
/src/logging.sh
1
#!/usr/bin/env bash
2
# @file logging.sh
3
# @brief Utilities for logging to stdout/stderr
4
# @description
5
#   This module contains functions to log messages to stdout/stderr.
6
#
7
# **LOG_LEVEL** (string): Run time log level of all messages (trace|debug|info|warn|error|fatal). Default is `info`
8
: "${DYBATPHO_DIR:?DYBATPHO_DIR must be set. Please source dybatpho/init.sh before other scripts from dybatpho.}"
134✔
9

10
LOG_LEVEL=$(dybatpho::lower "${LOG_LEVEL:-info}")
268✔
11
export LOG_LEVEL
134✔
12

13
#######################################
14
# @description Verify log level from input.
15
# @arg $1 string String of log level
16
# @exitcode 0 If is valid log level
17
# @exitcode 1 If invalid
18
#######################################
19
function __verify_log_level {
20
  local level="$1"
107✔
21
  level=$(dybatpho::lower "${level}")
214✔
22
  if [[ "${level}" =~ trace|debug|info|warn|error|fatal ]]; then
107✔
23
    return 0
106✔
24
  else
25
    echo "${level} is not a valid LOG_LEVEL, it should be trace|debug|info|warn|error|fatal"
1✔
26
    return 1
1✔
27
  fi
28
}
29

30
#######################################
31
# @description Log a message to stdout/stderr with color and caution.
32
# @set LOG_LEVEL string Log level of script
33
# @arg $1 string Log level of message
34
# @arg $2 string Message
35
# @arg $3 string `stderr` to output to stderr, otherwise then to stdout
36
# @arg $4 string ANSI escape color code
37
# @arg $5 string Command to run after log
38
# @stdout Show message if log level of message is less than runtime log level and $3 is not `stderr`
39
# @stderr Show message if log level of message is less than runtime log level and $3 is `stderr`
40
#######################################
41
function __log {
42
  declare -A log_levels=([trace]=5 [debug]=4 [info]=3 [warn]=2 [error]=1 [fatal]=0)
104✔
43
  declare -A log_colors=([trace]="1;30;47" [debug]="0;37;40" [info]="0;40" [warn]="0;33;40" [error]="1;31;40" [fatal]="1;37;41")
104✔
44
  local show_log_level="$1"
52✔
45
  local msg="$2"
52✔
46
  local out="${3:-stdout}"
52✔
47
  local color="${4:-${log_colors[${show_log_level}]}}"
52✔
48

49
  __verify_log_level "${LOG_LEVEL}"
52✔
50
  __verify_log_level "${show_log_level}"
52✔
51

52
  local runtime_level_num="${log_levels[${LOG_LEVEL}]}"
52✔
53
  local write_level_num="${log_levels[${show_log_level}]}"
52✔
54

55
  [ "${write_level_num}" -le "${runtime_level_num}" ] || return 0
66✔
56

57
  if [[ "${out}" == "stderr" ]]; then
38✔
58
    echo -e "\e[${color}m${msg}\e[0m" 1>&2
21✔
59
  else
60
    echo -e "\e[${color}m${msg}\e[0m"
17✔
61
  fi
62
}
63

64
#######################################
65
# @description Show debug message.
66
# @arg $1 string Message
67
# @stderr Show message if log level of message is less than debug level
68
#######################################
69
function dybatpho::debug {
70
  __log debug "DEBUG: $*" stderr
15✔
71
}
72

73
#######################################
74
# @description Show info message.
75
# @arg $1 string Message
76
# @stderr Show message if log level of message is less than info level
77
#######################################
78
function dybatpho::info {
79
  __log info "INFO: $*" stderr
1✔
80
}
81

82
#######################################
83
# @description Show normal message.
84
# @arg $1 string Message
85
# @stdout Show message if log level of message is less than info level
86
#######################################
87
function dybatpho::print {
NEW
88
  __log info "$*" stdout "0;0"
×
89
}
90

91
#######################################
92
# @description Show in progress message.
93
# @arg $1 string Message
94
# @stdout Show message if log level of message is less than info level
95
#######################################
96
function dybatpho::progress {
97
  __log info "$*..." stdout "0;36"
12✔
98
}
99

100
#######################################
101
# @description Show progress bar.
102
# @arg $1 number Elapsed percentage
103
# @arg $2 number Total length of progress bar in chars. Default is 50
104
# @stdout Show progress bar and it's disappeared after done
105
#######################################
106
function dybatpho::progress_bar {
107
  local percentage="$1"
4✔
108
  local length="${2:-50}"
4✔
109
  local elapsed=$((percentage * length / 100))
4✔
110

111
  printf -v prog "%${elapsed}s"
4✔
112
  printf -v total "%$((length - elapsed))s"
4✔
113
  printf '%s\r' "[${prog// /#}${total}]"
4✔
114
}
115

116
#######################################
117
# @description Show notice message with banner.
118
# @arg $1 string Message
119
# @stdout Show message if log level of message is less than info level
120
#######################################
121
function dybatpho::notice {
122
  local color="1;30;44"
1✔
123
  __log info \
1✔
124
    "================================================================================" \
125
    stdout "${color}"
126
  __log info "$*" stdout "${color}"
1✔
127
  __log info \
1✔
128
    "================================================================================" \
129
    stdout "${color}"
130
}
131

132
#######################################
133
# @description Show success message.
134
# @arg $1 string Message
135
# @stdout Show message if log level of message is less than info level
136
#######################################
137
function dybatpho::success {
138
  __log info "DONE: $1" stdout "1;32;40"
1✔
139
}
140

141
#######################################
142
# @description Show warning message.
143
# @arg $1 string Message
144
# @arg $2 string Indicator of message, default is `<invoke file>:<line number of invoke file>`
145
# @stderr Show message if log level of message is less than warning level
146
#######################################
147
function dybatpho::warn {
148
  local indicator="${2:-"${BASH_SOURCE[-1]}:${BASH_LINENO[0]}"}"
5✔
149
  __log warn "$(date +"%FT%T") ${indicator} [WARNING]: ${1}" stderr
10✔
150
}
151

152
#######################################
153
# @description Show error message.
154
# @arg $1 string Message
155
# @arg $2 string Indicator of message, default is `<invoke file>:<line number of invoke file>`
156
# @stderr Show message if log level of message is less than error level
157
#######################################
158
function dybatpho::error {
159
  local indicator="${2:-"${BASH_SOURCE[-1]}:${BASH_LINENO[0]}"}"
1✔
160
  __log error "$(date +"%FT%T") ${indicator} [ERROR]: ${1}" stderr
2✔
161
}
162

163
#######################################
164
# @description Show fatal message and exit process.
165
# @arg $1 string Message
166
# @arg $2 string Indicator of message, default is `<invoke file>:<line number of invoke file>`
167
# @stderr Show message if log level of message is less than fatal level
168
#######################################
169
function dybatpho::fatal {
170
  local indicator="${2:-"${BASH_SOURCE[-1]}:${BASH_LINENO[0]}"}"
10✔
171
  __log fatal "$(date +"%FT%T") ${indicator} [FATAL]: ${1}" stderr
20✔
172
}
173

174
#######################################
175
# @description Start tracing script.
176
# @noargs
177
#######################################
178
function dybatpho::start_trace {
179
  [ "${LOG_LEVEL}" != "trace" ] && return
3✔
180
  __log trace "START TRACE" stderr
1✔
181
  export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
2✔
182
  trap -p EXIT || (trap 'set +xv' EXIT && set -xv) # kcov(skip)
183
}
184

185
#######################################
186
# @description End tracing script.
187
# @noargs
188
#######################################
189
function dybatpho::end_trace {
190
  set +xv
2✔
191
  # kcov(disabled)
192
  [ "${LOG_LEVEL}" != "trace" ] && return
193
  __log trace "END TRACE" stderr
194
  # kcov(enabled)
195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc