1 #!/bin/bash |
|
2 # vim: set ft=sh : |
|
3 # |
|
4 # Logging output |
|
5 |
|
6 # Output message to stderr. |
|
7 function log_msg { |
|
8 echo "$*" >&2 |
|
9 } |
|
10 |
|
11 # Output message to stderr, optionally with given color, if TTY. |
|
12 function log_color { |
|
13 local code=$1; shift |
|
14 |
|
15 if [ $IS_TTY ]; then |
|
16 echo $'\e['${code}'m'"$*"$'\e[00m' >&2 |
|
17 else |
|
18 echo "$*" >&2 |
|
19 fi |
|
20 } |
|
21 |
|
22 ## Log at various log-levels |
|
23 |
|
24 function log_error { |
|
25 [ $LOG_ERROR ] && log_color '31' "$*" |
|
26 } |
|
27 |
|
28 function log_warn { |
|
29 [ $LOG_WARN ] && log_color '33' "$*" || true |
|
30 } |
|
31 |
|
32 # plain |
|
33 function log { |
|
34 [ $LOG ] && log_msg "$*" || true |
|
35 } |
|
36 |
|
37 function log_force { |
|
38 [ $LOG_FORCE ] && log_color '2;33' " $*" || true |
|
39 } |
|
40 |
|
41 function log_update { |
|
42 [ $LOG_UPDATE ] && log_color '36' " $*" || true |
|
43 } |
|
44 |
|
45 function log_noop { |
|
46 [ $LOG_NOOP ] && log_color '2;34' " $*" || true |
|
47 } |
|
48 |
|
49 function log_skip { |
|
50 [ $LOG_SKIP ] && log_color '1;34' " $*" || true |
|
51 } |
|
52 |
|
53 function log_debug { |
|
54 [ $LOG_DEBUG ] && log_color 32 " $*" || true |
|
55 } |
|
56 |
|
57 function log_cmd { |
|
58 [ $LOG_CMD ] && log_color 35 " \$ $*" || true |
|
59 } |
|
60 |
|
61 # Output stacktrace, broken. |
|
62 function log_stack { |
|
63 local level=1 |
|
64 |
|
65 while info=$(caller $level); do |
|
66 echo $info | read line sub file |
|
67 |
|
68 log_msg "$file:$lineno $sub()" |
|
69 |
|
70 level=$(($level + 1)) |
|
71 done |
|
72 } |
|
73 |
|
74 # Output calling function's name. |
|
75 function func_caller { |
|
76 caller 1 | cut -d ' ' -f 2 |
|
77 } |
|
78 |
|
79 ### High-level logging output |
|
80 # Log with func_caller at log_debug |
|
81 function debug { |
|
82 printf -v prefix "%s" $(func_caller) |
|
83 |
|
84 log_debug "$prefix: $*" |
|
85 } |
|
86 |
|
87 # Log with func_caller at log_error and exit, intended for internal errors... |
|
88 function fail { |
|
89 log_error "$(func_caller): $*" |
|
90 |
|
91 exit 2 |
|
92 } |
|
93 |
|
94 # Log at log_error and exit |
|
95 function die { |
|
96 log_error "$*" |
|
97 exit 1 |
|
98 } |
|
99 |
|
100 |
|