-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalog.go
107 lines (94 loc) · 3.73 KB
/
alog.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package alog enables applications to write to and read from Android's logging facilities.
//
// Writing Log Messages
//
// Writing to the Android logging facilities can be accomplished in multiple ways.
// For applications leveraging Go's log package, alog.NewLogger is the way to go:
//
// logger, err := alog.NewLogger(alog.LogIdMain)
// if err != nil {
// panic(err)
// }
// logger.Print("Successfully connected to Android's logging facilities")
//
// Convenience functions for all log levels are available and applications can output
// their tagged messages to Android's well-known logs as in the following example:
//
// alog.D(alog.Main, "tag", "message")
//
// Finally, applications can leverage the interface Writer and its implementation LoggerWriter
// to write to the Android logging facilities. The respective types and functions are meant to
// be used for integration purposes with other logging frameworks.
//
// Reading Log Entries
//
// Reading from the Android logging facilities is abstracted by the interface Reader and its
// implementation LoggerReader. Applications can access Android's well known logs and read individual
// entries as illustrated in the following snippet:
//
// lr, err := alog.NewLoggerReader(alog.LogIdMain)
// lr.SetDeadline(time.Now().Add(500 * time.Millisecond))
// for entry, err := lr.ReadNext(); err == nil; entry, err = lr.ReadNext() {
// lr.SetDeadline(time.Now().Add(500 * time.Millisecond))
// }
package alog
import "log"
var (
Main, _ = NewLoggerWriter(LogIdMain) // Global Writer for accessing log Main.
Radio, _ = NewLoggerWriter(LogIdRadio) // Global Writer for accessing log Radio.
Events, _ = NewLoggerWriter(LogIdEvents) // Global Writer for accessing log Events.
System, _ = NewLoggerWriter(LogIdSystem) // Global Writer for accessing log System.
)
type ioWriterWrapper struct {
prio Priority
tag Tag
writer Writer
}
func (self ioWriterWrapper) Write(b []byte) (int, error) {
if err := self.writer.Write(self.prio, self.tag, string(b)); err != nil {
return 0, err
}
return len(b), nil
}
// NewLogger returns a log.Logger instance, sending entries to the Android log
// specificed by logId, with priority prio and tag. logFlags are passed on to the
// log.Logger created by this function, but we disable all time-related flags.
//
// Returns an error if accessing the Android logging facilities fails.
func NewLogger(logId LogId, prio Priority, tag Tag, logFlags int) (*log.Logger, error) {
// We disable all time-related flags as entries are timestamped by the
// the Android logging facilities.
logFlags = logFlags & ^log.Ldate
logFlags = logFlags & ^log.Ltime
logFlags = logFlags & ^log.Lmicroseconds
w, err := NewLoggerWriter(logId)
if err != nil {
return nil, err
}
iow := &ioWriterWrapper{prio, tag, w}
return log.New(iow, "", logFlags), nil
}
// V logs message under tag with priority PriorityVerbose to w.
func V(w Writer, tag Tag, message string) error {
return w.Write(PriorityVerbose, tag, message)
}
// D logs message under tag with priority PriorityDebug to w.
func D(w Writer, tag Tag, message string) error {
return w.Write(PriorityDebug, tag, message)
}
// I logs message under tag with priority PriorityInfo to w.
func I(w Writer, tag Tag, message string) error {
return w.Write(PriorityInfo, tag, message)
}
// W logs message under tag with priority PriorityWarning to w.
func W(w Writer, tag Tag, message string) error {
return w.Write(PriorityWarn, tag, message)
}
// E logs message under tag with priority PriorityError to w.
func E(w Writer, tag Tag, message string) error {
return w.Write(PriorityError, tag, message)
}
// F logs message under tag with priority PriorityFatal to w.
func F(w Writer, tag Tag, message string) error {
return w.Write(PriorityFatal, tag, message)
}