-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
log.go
41 lines (34 loc) · 949 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package fileresolver
import (
"io"
"log"
"within.website/olin/abi"
)
// Log returns a file that redirects all write calls to a standard library logger
// with the given prefix and flags.
//
// For more information, please see the spec here: https://github.com/CommonWA/cwa-spec/blob/master/schemes/log.md
func Log(writer io.Writer, prefix string, flag int) abi.File {
lprefix := prefix
if prefix != "" {
lprefix += ": "
}
l := log.New(writer, lprefix, flag)
return logFile{
l: l,
prefix: prefix,
}
}
type logFile struct {
prefix string
l *log.Logger
}
func (logFile) Read(p []byte) (int, error) { return 0, nil }
func (logFile) Flush() error { return nil }
func (logFile) Close() error { return nil }
func (l logFile) Name() string { return "log " + l.prefix }
func (l logFile) Write(p []byte) (int, error) {
res := len(string(p))
l.l.Println(string(p))
return res, nil
}