blob: 8153be555bac3c6e6e88dadd9d67cb83c01449e7 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2016 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package loggo
5
6// Do not change rootName: modules.resolve() will misbehave if it isn't "".
7const (
8 rootString = "<root>"
9 defaultRootLevel = WARNING
10 defaultLevel = UNSPECIFIED
11)
12
13type module struct {
14 name string
15 level Level
16 parent *module
17 context *Context
18}
19
20// Name returns the module's name.
21func (module *module) Name() string {
22 if module.name == "" {
23 return rootString
24 }
25 return module.name
26}
27
28func (m *module) willWrite(level Level) bool {
29 if level < TRACE || level > CRITICAL {
30 return false
31 }
32 return level >= m.getEffectiveLogLevel()
33}
34
35func (module *module) getEffectiveLogLevel() Level {
36 // Note: the root module is guaranteed to have a
37 // specified logging level, so acts as a suitable sentinel
38 // for this loop.
39 for {
40 if level := module.level.get(); level != UNSPECIFIED {
41 return level
42 }
43 module = module.parent
44 }
45 panic("unreachable")
46}
47
48// setLevel sets the severity level of the given module.
49// The root module cannot be set to UNSPECIFIED level.
50func (module *module) setLevel(level Level) {
51 // The root module can't be unspecified.
52 if module.name == "" && level == UNSPECIFIED {
53 level = WARNING
54 }
55 module.level.set(level)
56}
57
58func (m *module) write(entry Entry) {
59 entry.Module = m.name
60 m.context.write(entry)
61}