| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package memlogger | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 | |
| 10 "golang.org/x/net/context" | |
| 11 | |
| 12 "infra/libs/logging" | |
| 13 ) | |
| 14 | |
| 15 // LogLevel indicates the severity of a LogEntry. | |
| 16 type LogLevel uint | |
| 17 | |
| 18 // 4 different log levels. These are automatically recorded in LogEntry by the | |
| 19 // various MemLogger.* methods. | |
| 20 const ( | |
| 21 LogError LogLevel = iota | |
| 22 LogWarn | |
| 23 LogInfo | |
| 24 ) | |
| 25 | |
| 26 func (l LogLevel) String() string { | |
| 27 switch l { | |
| 28 case LogError: | |
| 29 return "ERR" | |
| 30 case LogWarn: | |
| 31 return "WRN" | |
| 32 case LogInfo: | |
| 33 return "IFO" | |
| 34 default: | |
| 35 return "???" | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 // LogEntry is a single entry in a MemLogger, containing a message and a | |
| 40 // severity. | |
| 41 type LogEntry struct { | |
| 42 Level LogLevel | |
| 43 Msg string | |
| 44 } | |
| 45 | |
| 46 // MemLogger is an implementation of Logger. | |
| 47 type MemLogger []LogEntry | |
| 48 | |
| 49 // Infof adds a new LogEntry at the LogInfo level | |
| 50 func (m *MemLogger) Infof(format string, args ...interface{}) { | |
| 51 *m = append(*m, LogEntry{LogInfo, fmt.Sprintf(format, args...)}) | |
| 52 } | |
| 53 | |
| 54 // Warningf adds a new LogEntry at the LogWarn level | |
| 55 func (m *MemLogger) Warningf(format string, args ...interface{}) { | |
| 56 *m = append(*m, LogEntry{LogWarn, fmt.Sprintf(format, args...)}) | |
| 57 } | |
| 58 | |
| 59 // Errorf adds a new LogEntry at the LogError level | |
| 60 func (m *MemLogger) Errorf(format string, args ...interface{}) { | |
| 61 *m = append(*m, LogEntry{LogError, fmt.Sprintf(format, args...)}) | |
| 62 } | |
| 63 | |
| 64 // Use adds a memory backed Logger to Context, with concrete type | |
| 65 // *MemLogger. Casting to the concrete type can be used to inspect the | |
| 66 // log output after running a test case, for example. | |
| 67 func Use(c context.Context) context.Context { | |
| 68 ml := &MemLogger{} | |
| 69 return logging.Set(c, func(ic context.Context) logging.Logger { | |
| 70 return ml | |
| 71 }) | |
| 72 } | |
| OLD | NEW |