Skip to content

Commit 36077cc

Browse files
committed
Merge pull request #4 from ishbir/log_fix
Factor out use of log package
2 parents d0150aa + c5a44f7 commit 36077cc

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

message.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"time"
1010
)
1111

12+
// LogHandler is called for each log message. If nil, log messages will
13+
// be output using log.Printf instead.
14+
var LogHandler func(message string, args ...interface{})
15+
16+
func logf(message string, args ...interface{}) {
17+
if LogHandler != nil {
18+
LogHandler(message, args...)
19+
} else {
20+
log.Printf(message, args...)
21+
}
22+
}
23+
1224
// MessageID represents the ID of an SMTP message including the hostname part
1325
type MessageID string
1426

@@ -90,7 +102,7 @@ func (m *SMTPMessage) Parse(hostname string) *Message {
90102
}
91103

92104
if msg.Content.IsMIME() {
93-
log.Printf("Parsing MIME body")
105+
logf("Parsing MIME body")
94106
msg.MIME = msg.Content.ParseMIMEBody()
95107
}
96108

@@ -120,16 +132,16 @@ func (content *Content) ParseMIMEBody() *MIMEBody {
120132
var p []string
121133
if len(boundary) > 0 {
122134
p = strings.Split(content.Body, "--"+boundary)
123-
log.Printf("Got boundary: %s", boundary)
135+
logf("Got boundary: %s", boundary)
124136
} else {
125-
log.Printf("Boundary not found: %s", hdr[0])
137+
logf("Boundary not found: %s", hdr[0])
126138
}
127139

128140
for _, s := range p {
129141
if len(s) > 0 {
130142
part := ContentFromString(strings.Trim(s, "\r\n"))
131143
if part.IsMIME() {
132-
log.Printf("Parsing inner MIME body")
144+
logf("Parsing inner MIME body")
133145
part.MIME = part.ParseMIMEBody()
134146
}
135147
parts = append(parts, part)
@@ -171,7 +183,7 @@ func PathFromString(path string) *Path {
171183

172184
// ContentFromString parses SMTP content into separate headers and body
173185
func ContentFromString(data string) *Content {
174-
log.Printf("Parsing Content from string: '%s'", data)
186+
logf("Parsing Content from string: '%s'", data)
175187
x := strings.SplitN(data, "\r\n\r\n", 2)
176188
h := make(map[string][]string, 0)
177189

@@ -189,7 +201,7 @@ func ContentFromString(data string) *Content {
189201
h[key] = []string{value}
190202
lastHdr = key
191203
} else {
192-
log.Printf("Found invalid header: '%s'", hdr)
204+
logf("Found invalid header: '%s'", hdr)
193205
}
194206
}
195207
return &Content{

0 commit comments

Comments
 (0)