2
2
// debug messages to stdout. Trace logging is also supported.
3
3
// Trace logs go to stdout as well, but they are only written if the program
4
4
// is run with environment variable "TRACE=true".
5
- // A stack dump will be printed after the message if "PRINT_STACK=true".
6
5
package golog
7
6
8
7
import (
@@ -67,16 +66,6 @@ type outputs struct {
67
66
DebugOut io.Writer
68
67
}
69
68
70
- // MultiLine is an interface for arguments that support multi-line output.
71
- type MultiLine interface {
72
- // MultiLinePrinter returns a function that can be used to print the
73
- // multi-line output. The returned function writes one line to the buffer and
74
- // returns true if there are more lines to write. This function does not need
75
- // to take care of trailing carriage returns, golog handles that
76
- // automatically.
77
- MultiLinePrinter () func (buf * bytes.Buffer ) bool
78
- }
79
-
80
69
// ErrorReporter is a function to which the logger will report errors.
81
70
// It the given error and corresponding message along with associated ops
82
71
// context. This should return quickly as it executes on the critical code
@@ -145,20 +134,15 @@ func LoggerFor(prefix string) Logger {
145
134
l .traceOut = ioutil .Discard
146
135
}
147
136
148
- printStack := os .Getenv ("PRINT_STACK" )
149
- l .printStack , _ = strconv .ParseBool (printStack )
150
-
151
137
return l
152
138
}
153
139
154
140
type logger struct {
155
- prefix string
156
- traceOn bool
157
- traceOut io.Writer
158
- printStack bool
159
- outs atomic.Value
160
- pc []uintptr
161
- funcForPc * runtime.Func
141
+ prefix string
142
+ traceOn bool
143
+ traceOut io.Writer
144
+ outs atomic.Value
145
+ pc []uintptr
162
146
}
163
147
164
148
// attaches the file and line number corresponding to
@@ -175,44 +159,27 @@ func (l *logger) print(out io.Writer, skipFrames int, severity string, arg inter
175
159
defer bufferPool .Put (buf )
176
160
177
161
linePrefix := l .linePrefix (skipFrames )
178
- writeHeader := func () {
179
- buf .WriteString (severity )
180
- buf .WriteString (" " )
181
- buf .WriteString (linePrefix )
182
- }
162
+ sp := severity + " " + linePrefix
163
+ buf .WriteString (sp )
183
164
if arg != nil {
184
- ml , isMultiline := arg .(MultiLine )
185
- if ! isMultiline {
186
- writeHeader ()
187
- fmt .Fprintf (buf , "%v" , arg )
165
+ if err , isError := arg .(errors.Error ); isError && err != nil {
166
+ buf .WriteString (err .Error ())
188
167
printContext (buf , arg )
189
168
buf .WriteByte ('\n' )
190
- } else {
191
- mlp := ml .MultiLinePrinter ()
192
- first := true
193
- for {
194
- writeHeader ()
195
- more := mlp (buf )
196
- if first {
197
- printContext (buf , arg )
198
- first = false
199
- }
200
- buf .WriteByte ('\n' )
201
- if ! more {
202
- break
203
- }
169
+ if severity == "FATAL" || severity == "ERROR" {
170
+ err .PrintStack (buf , sp )
204
171
}
172
+ } else {
173
+ fmt .Fprintf (buf , "%v" , arg )
174
+ printContext (buf , arg )
175
+ buf .WriteByte ('\n' )
205
176
}
206
177
}
207
178
b := []byte (hidden .Clean (buf .String ()))
208
179
_ , err := out .Write (b )
209
180
if err != nil {
210
181
errorOnLogging (err )
211
182
}
212
- if l .printStack {
213
- l .doPrintStack ()
214
- }
215
-
216
183
return linePrefix
217
184
}
218
185
@@ -232,9 +199,6 @@ func (l *logger) printf(out io.Writer, skipFrames int, severity string, err erro
232
199
if err2 != nil {
233
200
errorOnLogging (err )
234
201
}
235
- if l .printStack {
236
- l .doPrintStack ()
237
- }
238
202
return linePrefix
239
203
}
240
204
@@ -349,26 +313,6 @@ func (l *logger) AsStdLogger() *log.Logger {
349
313
return log .New (& errorWriter {l }, "" , 0 )
350
314
}
351
315
352
- func (l * logger ) doPrintStack () {
353
- var b []byte
354
- buf := bytes .NewBuffer (b )
355
- for _ , pc := range l .pc {
356
- funcForPc := runtime .FuncForPC (pc )
357
- if funcForPc == nil {
358
- break
359
- }
360
- name := funcForPc .Name ()
361
- if strings .HasPrefix (name , "runtime." ) {
362
- break
363
- }
364
- file , line := funcForPc .FileLine (pc )
365
- fmt .Fprintf (buf , "\t %s\t %s: %d\n " , name , file , line )
366
- }
367
- if _ , err := buf .WriteTo (os .Stderr ); err != nil {
368
- errorOnLogging (err )
369
- }
370
- }
371
-
372
316
func errorOnLogging (err error ) {
373
317
fmt .Fprintf (os .Stderr , "Unable to log: %v\n " , err )
374
318
}
0 commit comments