-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_logger.go
More file actions
75 lines (60 loc) · 2.09 KB
/
Copy pathconditional_logger.go
File metadata and controls
75 lines (60 loc) · 2.09 KB
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
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
*-----------------------------------------------------------------*/
package wipechromium
import (
"log"
)
/* ----------------------------------------------------------------
* G l o b a l s
*-----------------------------------------------------------------*/
var _ ILogger = (*ConditionalLogger)(nil)
/* ----------------------------------------------------------------
* I n t e r f a c e s
*-----------------------------------------------------------------*/
type ILogger interface {
Printf(template string, v ...any)
Print(v ...any)
IsEnabled() bool
InheritAs(string) *ConditionalLogger
}
/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/
type ConditionalLogger struct {
enabled bool
prefix string
}
/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/
func NewConditionalLogger(enabled bool, prefix string) *ConditionalLogger {
return &ConditionalLogger{enabled, prefix}
}
/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/
// Whether log is enabled or not
func (l *ConditionalLogger) IsEnabled() bool {
return l.enabled
}
// A new logger inheriting configuration from the parent but different prefix.
func (l *ConditionalLogger) InheritAs(prefix string) *ConditionalLogger {
return NewConditionalLogger(l.IsEnabled(), prefix)
}
// Formatted logging
func (l *ConditionalLogger) Printf(template string, v ...any) {
if l.enabled {
log.Printf(l.prefix+" "+template, v...)
}
}
// Print items on log stream
func (l *ConditionalLogger) Print(v ...any) {
if l.enabled {
v = append([]any{l.prefix + " "}, v...)
log.Print(v...)
}
}