Initial commit
[ta/infra-ansible.git] / roles / baremetal_provision / files / tmux_setup / log.inc
1 #!/bin/bash
2 # vim:noet:sw=4:ts=4:ft=sh
3
4 # Copyright 2019 Nokia
5
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 [ -n "$__AUTOCOM_LIB_LOG_INC__" ] && return || __AUTOCOM_LIB_LOG_INC__=1
19
20 LOGGING_TIMESTAMP_FORMAT='%b %d %H:%M:%S'
21
22 _logging_fmt()
23 {
24         local fmt="%7s : ${1}%s\n"
25
26         [ -n "${LOGGING_TIMESTAMPS}" -a "${LOGGING_TIMESTAMPS}" != "0" ] \
27                 && printf -v fmt "%(${LOGGING_TIMESTAMP_FORMAT})T %s" -1 "${fmt}"
28
29         printf "%s" "${fmt}"
30 }
31
32 debug()
33 {
34         local f
35         local fmt="$(_logging_fmt "${1}")"
36         local src=""
37
38         shift
39
40         [ -n "${LOGGING_SOURCES}" -a "${LOGGING_SOURCES}" != "0" ] \
41                 && src=" [${FUNCNAME[1]}()]"
42
43         [ -n "${LOGGING_DEBUG}" ] && [ "${LOGGING_DEBUG}" -ne 0 ] \
44                 && printf "${fmt}" "DEBUG" "${@}" "${src}" 1>&2
45
46         for f in "${LOGGING_HOOKS_DEBUG[@]}" "${LOGGING_HOOKS_ALL[@]}"; do "${f}" "${@}" ; done
47
48         return 0
49 }
50
51 info()
52 {
53         local f
54         local fmt="$(_logging_fmt "${1}")"
55         local src=""
56
57         shift
58
59         [ -n "${LOGGING_SOURCES}" -a "${LOGGING_SOURCES}" != "0" ] \
60                 && src=" [${FUNCNAME[1]}()]"
61
62         ( [ -z "${LOGGING_QUIET}" ] || [ "${LOGGING_QUIET}" -eq 0 ] ) \
63                 && printf "${fmt}" "INFO" "${@}" "${src}"
64
65         for f in "${LOGGING_HOOKS_INFO[@]}" "${LOGGING_HOOKS_ALL[@]}"; do "${f}" "${@}" ; done
66
67         return 0
68 }
69
70 _logging_log_err()
71 {
72         local f
73         local fmt="$(_logging_fmt "${2}")"
74         local src=""
75         local label="${1:-ERROR}"
76
77         shift 2
78
79         [ -n "${LOGGING_SOURCES}" -a "${LOGGING_SOURCES}" != "0" ] \
80                 && src=" [${FUNCNAME[2]}()]"
81
82         printf "${fmt}" "${label}" "${@}" "${src}" 1>&2
83
84         for f in $(eval "echo \"\${LOGGING_HOOKS_${label}[@]}\"") "${LOGGING_HOOKS_ALL[@]}" ; do "${f}" "${@}" ; done
85 }
86
87 notice()
88 {
89         _logging_log_err NOTICE "${@}"
90 }
91
92 warn()
93 {
94         _logging_log_err WARNING "${@}"
95 }
96
97 error()
98 {
99         _logging_log_err ERROR "${@}"
100 }
101
102 alert()
103 {
104         _logging_log_err ALERT "${@}"
105 }
106
107 emerg()
108 {
109         _logging_log_err EMERG "${@}"
110 }
111
112 crit()
113 {
114         _logging_log_err CRIT "${@}"
115 }
116
117 _main()
118 {
119         local f="${1}"
120         shift
121         "${f}" "$@"
122 }
123
124
125 [ "${BASH_SOURCE}" != "${0}" ] || _main "${@}"
126
127 # Local Variables:
128 # mode: sh
129 # tab-width: 4
130 # End: