darwin-shell 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,317 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell log: leveled text logging with an optional file sink.
3
+ # Sourcing is side-effect free: directories and files are created lazily on
4
+ # the first log call. Colors and TTY policy belong to terminal.sh.
5
+
6
+ # Dependencies sit below this module. Load them before the guard so a failed
7
+ # dependency is retryable on the next source attempt.
8
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
9
+ if declare -F dsh::use >/dev/null 2>&1; then
10
+ dsh::use i18n || return $?
11
+ else
12
+ _dsh_private_log_self="${BASH_SOURCE[0]}"
13
+ source "${_dsh_private_log_self%/*}/i18n.sh" || return $?
14
+ unset _dsh_private_log_self
15
+ fi
16
+ fi
17
+ if ! declare -F dsh::terminal::code >/dev/null 2>&1; then
18
+ if declare -F dsh::use >/dev/null 2>&1; then
19
+ dsh::use terminal || return $?
20
+ else
21
+ _dsh_private_log_self="${BASH_SOURCE[0]}"
22
+ source "${_dsh_private_log_self%/*}/terminal.sh" || return $?
23
+ unset _dsh_private_log_self
24
+ fi
25
+ fi
26
+
27
+ if [[ -n "${_dsh_private_log_sourced:-}" ]]; then
28
+ return 0
29
+ fi
30
+ _dsh_private_log_sourced=1
31
+ readonly _dsh_private_log_sourced
32
+
33
+ readonly DSH_LOG_LEVEL_DEBUG=0
34
+ readonly DSH_LOG_LEVEL_INFO=1
35
+ readonly DSH_LOG_LEVEL_WARNING=2
36
+ readonly DSH_LOG_LEVEL_ERROR=3
37
+ readonly DSH_LOG_LEVEL_SILENT=4
38
+
39
+ _dsh_private_log_parsed_level=0
40
+ _dsh_private_log_level=1
41
+ _dsh_private_log_name='dsh'
42
+ _dsh_private_log_dir=''
43
+ _dsh_private_log_explicit_file=''
44
+ _dsh_private_log_resolved_file=''
45
+ _dsh_private_log_configured=0
46
+ _dsh_private_log_config_error=0
47
+ _dsh_private_log_resolved=0
48
+ _dsh_private_log_file_ready=0
49
+ _dsh_private_log_file_failed=0
50
+
51
+ _dsh_private_log_help() {
52
+ case "${1:-}" in
53
+ configure)
54
+ dsh::i18n::t 'Usage: dsh::log::configure [--name N] [--dir D] [--file F] [--level L] [--stderr-only]'
55
+ printf '\n'
56
+ dsh::i18n::t 'Configure logging explicitly. Idempotent and creates no files.'
57
+ dsh::i18n::t 'Level names: DEBUG INFO WARNING WARN ERROR SILENT, or 0-4.'
58
+ ;;
59
+ level|file)
60
+ dsh::i18n::t 'Usage: dsh::log::%1$s' "$1"
61
+ printf '\n'
62
+ if [[ "$1" == "level" ]]; then
63
+ dsh::i18n::t 'Print the current numeric log level.'
64
+ else
65
+ dsh::i18n::t 'Print the resolved log file path, or nothing for stderr-only logging.'
66
+ fi
67
+ ;;
68
+ debug|info|warn|error)
69
+ dsh::i18n::t 'Usage: dsh::log::%1$s <message...>' "$1"
70
+ printf '\n'
71
+ dsh::i18n::t 'Write one leveled line to the log file (if configured) and stderr.'
72
+ ;;
73
+ *)
74
+ dsh::i18n::t 'dsh::log: no help for %1$s' "${1:-<empty>}" >&2
75
+ return "${DSH_RC_USAGE:-2}"
76
+ ;;
77
+ esac
78
+ }
79
+
80
+ _dsh_private_log_fail_config() {
81
+ _dsh_private_log_configured=1
82
+ _dsh_private_log_config_error=1
83
+ dsh::i18n::t "$@" >&2
84
+ return 2
85
+ }
86
+
87
+ _dsh_private_log_parse_level() {
88
+ case "${1^^}" in
89
+ 0|DEBUG) _dsh_private_log_parsed_level=0 ;;
90
+ 1|INFO) _dsh_private_log_parsed_level=1 ;;
91
+ 2|WARNING|WARN) _dsh_private_log_parsed_level=2 ;;
92
+ 3|ERROR) _dsh_private_log_parsed_level=3 ;;
93
+ 4|SILENT) _dsh_private_log_parsed_level=4 ;;
94
+ *) return 2 ;;
95
+ esac
96
+ return 0
97
+ }
98
+
99
+ _dsh_private_log_ensure_configured() {
100
+ if (( _dsh_private_log_configured == 0 )); then
101
+ dsh::log::configure || return $?
102
+ fi
103
+ if (( _dsh_private_log_config_error != 0 )); then
104
+ return 2
105
+ fi
106
+ return 0
107
+ }
108
+
109
+ _dsh_private_log_resolve_file() {
110
+ local day
111
+ (( _dsh_private_log_resolved == 1 )) && return 0
112
+ if [[ -n "$_dsh_private_log_explicit_file" ]]; then
113
+ _dsh_private_log_resolved_file="$_dsh_private_log_explicit_file"
114
+ elif [[ -n "$_dsh_private_log_dir" ]]; then
115
+ printf -v day '%(%Y%m%d)T' -1
116
+ _dsh_private_log_resolved_file="${_dsh_private_log_dir%/}/${_dsh_private_log_name}_${day}_$$.log"
117
+ else
118
+ _dsh_private_log_resolved_file=''
119
+ fi
120
+ _dsh_private_log_resolved=1
121
+ return 0
122
+ }
123
+
124
+ _dsh_private_log_write_file() {
125
+ local line="$1" dir
126
+ _dsh_private_log_resolve_file || return 0
127
+ [[ -n "$_dsh_private_log_resolved_file" ]] || return 0
128
+ (( _dsh_private_log_file_failed == 0 )) || return 0
129
+
130
+ if (( _dsh_private_log_file_ready == 0 )); then
131
+ dir="${_dsh_private_log_resolved_file%/*}"
132
+ if ! mkdir -p "$dir" 2>/dev/null || ! : >>"$_dsh_private_log_resolved_file" 2>/dev/null; then
133
+ _dsh_private_log_file_failed=1
134
+ dsh::i18n::t 'dsh::log: cannot write log file %1$s; using stderr only' \
135
+ "$_dsh_private_log_resolved_file" >&2
136
+ return 0
137
+ fi
138
+ _dsh_private_log_file_ready=1
139
+ fi
140
+
141
+ if ! printf '%s\n' "$line" >>"$_dsh_private_log_resolved_file" 2>/dev/null; then
142
+ _dsh_private_log_file_failed=1
143
+ dsh::i18n::t 'dsh::log: cannot write log file %1$s; using stderr only' \
144
+ "$_dsh_private_log_resolved_file" >&2
145
+ fi
146
+ return 0
147
+ }
148
+
149
+ _dsh_private_log_emit() {
150
+ local level="$1" name="$2" color="$3"
151
+ shift 3
152
+ local ts level_code reset_code
153
+ _dsh_private_log_ensure_configured || return $?
154
+ (( _dsh_private_log_level <= level )) || return 0
155
+
156
+ printf -v ts '%(%Y-%m-%d %H:%M:%S)T' -1
157
+ _dsh_private_log_write_file "${ts} [${name}] ${_dsh_private_log_name}: $*"
158
+
159
+ if dsh::terminal::enabled stderr; then
160
+ level_code="$(dsh::terminal::code "$color" stderr)"
161
+ reset_code="$(dsh::terminal::code reset stderr)"
162
+ printf '%s [%s%s%s] %s: %s\n' \
163
+ "$ts" "$level_code" "$name" "$reset_code" "$_dsh_private_log_name" "$*" >&2
164
+ else
165
+ printf '%s [%s] %s: %s\n' "$ts" "$name" "$_dsh_private_log_name" "$*" >&2
166
+ fi
167
+ return 0
168
+ }
169
+
170
+ # Public: explicit, idempotent configuration. No filesystem access here.
171
+ dsh::log::configure() {
172
+ if [[ "${1:-}" == "--help" ]]; then
173
+ _dsh_private_log_help configure
174
+ return $?
175
+ fi
176
+ local name="${DARWIN_SH_LOG_NAME:-dsh}"
177
+ local dir="${DARWIN_SH_LOG_DIR:-}"
178
+ local file="${DARWIN_SH_LOG_FILE:-}"
179
+ local level="${DARWIN_SH_LOG_LEVEL:-INFO}"
180
+ local stderr_only=0
181
+
182
+ while (( $# )); do
183
+ case "$1" in
184
+ --name)
185
+ if (( $# < 2 )); then
186
+ _dsh_private_log_fail_config 'dsh::log: --name requires a value'
187
+ return $?
188
+ fi
189
+ name="$2"
190
+ shift 2
191
+ ;;
192
+ --name=*)
193
+ name="${1#*=}"
194
+ shift
195
+ ;;
196
+ --dir)
197
+ if (( $# < 2 )); then
198
+ _dsh_private_log_fail_config 'dsh::log: --dir requires a value'
199
+ return $?
200
+ fi
201
+ dir="$2"
202
+ shift 2
203
+ ;;
204
+ --dir=*)
205
+ dir="${1#*=}"
206
+ shift
207
+ ;;
208
+ --file)
209
+ if (( $# < 2 )); then
210
+ _dsh_private_log_fail_config 'dsh::log: --file requires a value'
211
+ return $?
212
+ fi
213
+ file="$2"
214
+ shift 2
215
+ ;;
216
+ --file=*)
217
+ file="${1#*=}"
218
+ shift
219
+ ;;
220
+ --level)
221
+ if (( $# < 2 )); then
222
+ _dsh_private_log_fail_config 'dsh::log: --level requires a value'
223
+ return $?
224
+ fi
225
+ level="$2"
226
+ shift 2
227
+ ;;
228
+ --level=*)
229
+ level="${1#*=}"
230
+ shift
231
+ ;;
232
+ --stderr-only)
233
+ stderr_only=1
234
+ shift
235
+ ;;
236
+ *)
237
+ _dsh_private_log_fail_config 'dsh::log: unknown option: %1$s' "$1"
238
+ return $?
239
+ ;;
240
+ esac
241
+ done
242
+
243
+ if ! _dsh_private_log_parse_level "$level"; then
244
+ _dsh_private_log_fail_config 'dsh::log: invalid level: %1$s' "$level"
245
+ return $?
246
+ fi
247
+
248
+ _dsh_private_log_level="$_dsh_private_log_parsed_level"
249
+ _dsh_private_log_name="$name"
250
+ if (( stderr_only == 1 )); then
251
+ dir=''
252
+ file=''
253
+ fi
254
+ _dsh_private_log_dir="$dir"
255
+ _dsh_private_log_explicit_file="$file"
256
+ _dsh_private_log_resolved_file=''
257
+ _dsh_private_log_resolved=0
258
+ _dsh_private_log_file_ready=0
259
+ _dsh_private_log_file_failed=0
260
+ _dsh_private_log_configured=1
261
+ _dsh_private_log_config_error=0
262
+ return 0
263
+ }
264
+
265
+ # Public: print the current numeric level.
266
+ dsh::log::level() {
267
+ if [[ "${1:-}" == "--help" ]]; then
268
+ _dsh_private_log_help level
269
+ return $?
270
+ fi
271
+ _dsh_private_log_ensure_configured || return $?
272
+ printf '%s\n' "$_dsh_private_log_level"
273
+ }
274
+
275
+ # Public: print the resolved log file path, or nothing in stderr-only mode.
276
+ dsh::log::file() {
277
+ if [[ "${1:-}" == "--help" ]]; then
278
+ _dsh_private_log_help file
279
+ return $?
280
+ fi
281
+ _dsh_private_log_ensure_configured || return $?
282
+ _dsh_private_log_resolve_file || return $?
283
+ printf '%s\n' "$_dsh_private_log_resolved_file"
284
+ }
285
+
286
+ # Public: leveled log writers. They never exit on behalf of the caller.
287
+ dsh::log::debug() {
288
+ if [[ "${1:-}" == "--help" ]]; then
289
+ _dsh_private_log_help debug
290
+ return $?
291
+ fi
292
+ _dsh_private_log_emit "$DSH_LOG_LEVEL_DEBUG" DEBUG cyan "$@"
293
+ }
294
+
295
+ dsh::log::info() {
296
+ if [[ "${1:-}" == "--help" ]]; then
297
+ _dsh_private_log_help info
298
+ return $?
299
+ fi
300
+ _dsh_private_log_emit "$DSH_LOG_LEVEL_INFO" INFO green "$@"
301
+ }
302
+
303
+ dsh::log::warn() {
304
+ if [[ "${1:-}" == "--help" ]]; then
305
+ _dsh_private_log_help warn
306
+ return $?
307
+ fi
308
+ _dsh_private_log_emit "$DSH_LOG_LEVEL_WARNING" WARNING yellow "$@"
309
+ }
310
+
311
+ dsh::log::error() {
312
+ if [[ "${1:-}" == "--help" ]]; then
313
+ _dsh_private_log_help error
314
+ return $?
315
+ fi
316
+ _dsh_private_log_emit "$DSH_LOG_LEVEL_ERROR" ERROR red "$@"
317
+ }