David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 1 | // Copyright 2014 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package loggo |
| 5 | |
| 6 | import ( |
| 7 | "path" |
| 8 | "sync" |
| 9 | ) |
| 10 | |
| 11 | // TestWriter is a useful Writer for testing purposes. Each component of the |
| 12 | // logging message is stored in the Log array. |
| 13 | type TestWriter struct { |
| 14 | mu sync.Mutex |
| 15 | log []Entry |
| 16 | } |
| 17 | |
| 18 | // Write saves the params as members in the TestLogValues struct appended to the Log array. |
| 19 | func (writer *TestWriter) Write(entry Entry) { |
| 20 | writer.mu.Lock() |
| 21 | defer writer.mu.Unlock() |
| 22 | entry.Filename = path.Base(entry.Filename) |
| 23 | writer.log = append(writer.log, entry) |
| 24 | } |
| 25 | |
| 26 | // Clear removes any saved log messages. |
| 27 | func (writer *TestWriter) Clear() { |
| 28 | writer.mu.Lock() |
| 29 | defer writer.mu.Unlock() |
| 30 | writer.log = nil |
| 31 | } |
| 32 | |
| 33 | // Log returns a copy of the current logged values. |
| 34 | func (writer *TestWriter) Log() []Entry { |
| 35 | writer.mu.Lock() |
| 36 | defer writer.mu.Unlock() |
| 37 | v := make([]Entry, len(writer.log)) |
| 38 | copy(v, writer.log) |
| 39 | return v |
| 40 | } |