David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package loggo |
| 5 | |
| 6 | // Do not change rootName: modules.resolve() will misbehave if it isn't "". |
| 7 | const ( |
| 8 | rootString = "<root>" |
| 9 | defaultRootLevel = WARNING |
| 10 | defaultLevel = UNSPECIFIED |
| 11 | ) |
| 12 | |
| 13 | type module struct { |
| 14 | name string |
| 15 | level Level |
| 16 | parent *module |
| 17 | context *Context |
| 18 | } |
| 19 | |
| 20 | // Name returns the module's name. |
| 21 | func (module *module) Name() string { |
| 22 | if module.name == "" { |
| 23 | return rootString |
| 24 | } |
| 25 | return module.name |
| 26 | } |
| 27 | |
| 28 | func (m *module) willWrite(level Level) bool { |
| 29 | if level < TRACE || level > CRITICAL { |
| 30 | return false |
| 31 | } |
| 32 | return level >= m.getEffectiveLogLevel() |
| 33 | } |
| 34 | |
| 35 | func (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. |
| 50 | func (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 | |
| 58 | func (m *module) write(entry Entry) { |
| 59 | entry.Module = m.name |
| 60 | m.context.write(entry) |
| 61 | } |