-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.mk
115 lines (105 loc) · 3.59 KB
/
log.mk
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
104
105
106
107
108
109
110
111
112
113
114
115
# Set the default logging level to INFO
LOGGING_LEVEL ?= INFO
# Define the logging functions
# log.debug: Prints a debug message if the debug level is set to DEBUG
# Usage: $(call log.debug, VAR_OR_STRING)
define log.debug
$(if $(filter $(LOGGING_LEVEL),DEBUG), \
$(if $(call variable_defined,$1), \
$(info DEBUG:: $(strip $1) = "$($(strip $1))"), \
$(if $(strip $1), \
$(info DEBUG:: $(strip $1)), \
$(info DEBUG:: "$(strip $1)") \
) \
) \
)
endef
# log.info: Prints an info message if the debug level is set to INFO or DEBUG
# Usage: $(call log.info, VAR_OR_STRING)
define log.info
$(if $(or $(filter $(LOGGING_LEVEL),INFO),$(filter $(LOGGING_LEVEL),DEBUG)), \
$(if $(call variable_defined,$1), \
$(info INFO:: $(strip $1) = "$($(strip $1))"), \
$(if $(strip $1), \
$(info INFO:: $(strip $1)), \
$(info INFO:: "$(strip $1)") \
) \
) \
)
endef
# log.warning: Prints a warning message if the debug level is set to WARNING, INFO, or DEBUG
# Usage: $(call log.warning, VAR_OR_STRING)
define log.warning
$(if $(or $(filter $(LOGGING_LEVEL),WARNING),$(filter $(LOGGING_LEVEL),INFO),$(filter $(LOGGING_LEVEL),DEBUG)), \
$(if $(call variable_defined,$1), \
$(info WARNING:: $(strip $1) = "$($(strip $1))"), \
$(if $(strip $1), \
$(info WARNING:: $(strip $1)), \
$(info WARNING:: "$(strip $1)") \
) \
) \
)
endef
# log.error: Prints an error message if the debug level is set to ERROR, WARNING, INFO, or DEBUG
# Usage: $(call log.error, VAR_OR_STRING)
define log.error
$(if $(or $(filter $(LOGGING_LEVEL),ERROR),$(filter $(LOGGING_LEVEL),WARNING),$(filter $(LOGGING_LEVEL),INFO),$(filter $(LOGGING_LEVEL),DEBUG)), \
$(if $(call variable_defined,$1), \
$(info ERROR:: $(strip $1) = "$($(strip $1))"), \
$(if $(strip $1), \
$(info ERROR:: $(strip $1)), \
$(info ERROR:: "$(strip $1)") \
) \
) \
)
endef
# Define a function to check if a variable is defined
define variable_defined
$(strip $(foreach v,$(1),$(if $(value $(v)),$(v))))
endef
# Example usage
FOO = bar
test_debug_level: LOGGING_LEVEL=DEBUG
test_debug_level:
# Debug level set to DEBUG, prints debug and info messages
$(call log.debug, FOO)
$(call log.info, FOO)
$(call log.warning, FOO)
$(call log.error, FOO)
$(call log.debug, "This is a debug message")
$(call log.info, "This is an info message")
$(call log.warning, "This is a warning message")
$(call log.error, "This is an error message")
test_info_level: LOGGING_LEVEL=INFO
test_info_level:
# Debug level set to INFO, prints info, warning, and error messages
$(call log.debug, FOO)
$(call log.info, FOO)
$(call log.warning, FOO)
$(call log.error, FOO)
$(call log.debug, "This is a debug message")
$(call log.info, "This is an info message")
$(call log.warning, "This is a warning message")
$(call log.error, "This is an error message")
test_warning_level: LOGGING_LEVEL=WARNING
test_warning_level:
# Debug level set to WARNING, prints warning and error messages
$(call log.debug, FOO)
$(call log.info, FOO)
$(call log.warning, FOO)
$(call log.error, FOO)
$(call log.debug, "This is a debug message")
$(call log.info, "This is an info message")
$(call log.warning, "This is a warning message")
$(call log.error, "This is an error message")
test_error_level: LOGGING_LEVEL=ERROR
test_error_level:
# Debug level set to ERROR, prints only error messages
$(call log.debug, FOO)
$(call log.info, FOO)
$(call log.warning, FOO)
$(call log.error, FOO)
$(call log.debug, "This is a debug message")
$(call log.info, "This is an info message")
$(call log.warning, "This is a warning message")
$(call log.error, "This is an error message")