blob: b20e47024b6fa4fee85fa52a8c54861cac366d7d [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2014 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package loggo
5
6import (
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.
13type 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.
19func (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.
27func (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.
34func (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}