File: module.go

package info (click to toggle)
golang-github-juju-loggo 0.0~git20170605.8232ab8-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 220 kB
  • sloc: makefile: 10
file content (61 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download
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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package loggo

// Do not change rootName: modules.resolve() will misbehave if it isn't "".
const (
	rootString       = "<root>"
	defaultRootLevel = WARNING
	defaultLevel     = UNSPECIFIED
)

type module struct {
	name    string
	level   Level
	parent  *module
	context *Context
}

// Name returns the module's name.
func (module *module) Name() string {
	if module.name == "" {
		return rootString
	}
	return module.name
}

func (m *module) willWrite(level Level) bool {
	if level < TRACE || level > CRITICAL {
		return false
	}
	return level >= m.getEffectiveLogLevel()
}

func (module *module) getEffectiveLogLevel() Level {
	// Note: the root module is guaranteed to have a
	// specified logging level, so acts as a suitable sentinel
	// for this loop.
	for {
		if level := module.level.get(); level != UNSPECIFIED {
			return level
		}
		module = module.parent
	}
	panic("unreachable")
}

// setLevel sets the severity level of the given module.
// The root module cannot be set to UNSPECIFIED level.
func (module *module) setLevel(level Level) {
	// The root module can't be unspecified.
	if module.name == "" && level == UNSPECIFIED {
		level = WARNING
	}
	module.level.set(level)
}

func (m *module) write(entry Entry) {
	entry.Module = m.name
	m.context.write(entry)
}