~jameinel/loggo/benchmarks

« back to all changes in this revision

Viewing changes to writer.go

  • Committer: Tim Penhey
  • Date: 2013-05-03 02:42:49 UTC
  • Revision ID: tim.penhey@canonical.com-20130503024249-3rn7y6yq52r4uiwn
Add mutex around writer map.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
        "fmt"
5
5
        "io"
6
6
        "os"
 
7
        "sync"
7
8
        "time"
8
9
)
9
10
 
23
24
 
24
25
// The registered writers is an application level singleton.
25
26
var (
26
 
        writers = map[string]registeredWriter{
 
27
        writerMutex sync.Mutex
 
28
        writers     = map[string]registeredWriter{
27
29
                "default": {
28
30
                        writer: &SimpleWriter{
29
31
                                os.Stderr,
37
39
// level that will be written, and a name for the writer.  If there is already
38
40
// a registered writer with that name, and error is returned.
39
41
func RegisterWriter(name string, writer Writer, minLevel Level) error {
 
42
        writerMutex.Lock()
 
43
        defer writerMutex.Unlock()
40
44
        if _, found := writers[name]; found {
41
45
                return fmt.Errorf("There is already a Writer registerd with the name %q", name)
42
46
        }
47
51
// RemoveWriter removes the Writer identified by 'name' and returns it.
48
52
// If the Writer is not found, an error is returned.
49
53
func RemoveWriter(name string) (Writer, Level, error) {
 
54
        writerMutex.Lock()
 
55
        defer writerMutex.Unlock()
50
56
        registered, found := writers[name]
51
57
        if !found {
52
58
                return nil, NOT_SPECIFIED, fmt.Errorf("No registered Writer called %q", name)
56
62
}
57
63
 
58
64
func writeToWriters(level Level, module, filename string, line int, timestamp time.Time, message string) {
 
65
        writerMutex.Lock()
 
66
        defer writerMutex.Unlock()
59
67
        for _, registered := range writers {
60
68
                if level >= registered.level {
61
69
                        registered.writer.Write(level, module, filename, line, timestamp, message)