aboutsummaryrefslogtreecommitdiff
path: root/lib/logging.bash
blob: b54db65ba4e27ea7e4fcd39ad399b19e9e4a80f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2022 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

# The base function to output logging information. This should *not* be used
# directly, but the helper functions can be used safely.
log() {
	local system=$1 ; shift

	printf "\e[32m[%s]\e[m \e[33m%s[\e[m%s\e[0;33m]\e[m: %s\e[m\n" \
		"$(date +%FT%T)" \
		"$system" \
		"$$" \
		"$*" \
		>&2
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     debug
## :param:    The system to log for.
## :param:    The message to log.
##
## Log a debug-level message.  This message only shows if BASHTARD_DEBUG
## evaluates to a truthy value.
debug() {
	[[ -n $BASHTARD_DEBUG ]] && log "$1" "$(printf "\e[0;37m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     info
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an info-level message.
info() {
	log "$1" "$(printf "\e[m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     notice
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an notice-level message.
notice() {
	log "$1" "$(printf "\e[0;34m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     warn
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an warning-level message.
warn() {
	log "$1" "$(printf "\e[1;39m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     crit
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an critical-level message.
crit() {
	log "$1" "$(printf "\e[0;33m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     alert
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an alert-level message.
alert() {
	log "$1" "$(printf "\e[0;31m%s" "${@:2}")"
}

## :bocs:
## :section:  3
## :heading:  functions
## :name:     emerg
## :param:    The system to log for.
## :param:    The message to log.
##
## Log an emergency-level message.
emerg() {
	log "$1" "$(printf "\e[1;31m%s" "${@:2}")"
}