@@ -404,13 +404,6 @@ func (t *traceLocation) Set(value string) error {
404
404
return nil
405
405
}
406
406
407
- // flushSyncWriter is the interface satisfied by logging destinations.
408
- type flushSyncWriter interface {
409
- Flush () error
410
- Sync () error
411
- io.Writer
412
- }
413
-
414
407
var logging loggingT
415
408
var commandLine flag.FlagSet
416
409
@@ -486,7 +479,7 @@ type settings struct {
486
479
// Access to all of the following fields must be protected via a mutex.
487
480
488
481
// file holds writer for each of the log types.
489
- file [severity .NumSeverity ]flushSyncWriter
482
+ file [severity .NumSeverity ]io. Writer
490
483
// flushInterval is the interval for periodic flushing. If zero,
491
484
// the global default will be used.
492
485
flushInterval time.Duration
@@ -831,32 +824,12 @@ func (l *loggingT) printS(err error, s severity.Severity, depth int, msg string,
831
824
buffer .PutBuffer (b )
832
825
}
833
826
834
- // redirectBuffer is used to set an alternate destination for the logs
835
- type redirectBuffer struct {
836
- w io.Writer
837
- }
838
-
839
- func (rb * redirectBuffer ) Sync () error {
840
- return nil
841
- }
842
-
843
- func (rb * redirectBuffer ) Flush () error {
844
- return nil
845
- }
846
-
847
- func (rb * redirectBuffer ) Write (bytes []byte ) (n int , err error ) {
848
- return rb .w .Write (bytes )
849
- }
850
-
851
827
// SetOutput sets the output destination for all severities
852
828
func SetOutput (w io.Writer ) {
853
829
logging .mu .Lock ()
854
830
defer logging .mu .Unlock ()
855
831
for s := severity .FatalLog ; s >= severity .InfoLog ; s -- {
856
- rb := & redirectBuffer {
857
- w : w ,
858
- }
859
- logging .file [s ] = rb
832
+ logging .file [s ] = w
860
833
}
861
834
}
862
835
@@ -868,10 +841,7 @@ func SetOutputBySeverity(name string, w io.Writer) {
868
841
if ! ok {
869
842
panic (fmt .Sprintf ("SetOutputBySeverity(%q): unrecognized severity name" , name ))
870
843
}
871
- rb := & redirectBuffer {
872
- w : w ,
873
- }
874
- logging .file [sev ] = rb
844
+ logging .file [sev ] = w
875
845
}
876
846
877
847
// LogToStderr sets whether to log exclusively to stderr, bypassing outputs
@@ -1011,8 +981,8 @@ func (l *loggingT) exit(err error) {
1011
981
logExitFunc (err )
1012
982
return
1013
983
}
1014
- files := l .flushAll ()
1015
- l .syncAll (files )
984
+ needToSync := l .flushAll ()
985
+ l .syncAll (needToSync )
1016
986
OsExit (2 )
1017
987
}
1018
988
@@ -1029,10 +999,6 @@ type syncBuffer struct {
1029
999
maxbytes uint64 // The max number of bytes this syncBuffer.file can hold before cleaning up.
1030
1000
}
1031
1001
1032
- func (sb * syncBuffer ) Sync () error {
1033
- return sb .file .Sync ()
1034
- }
1035
-
1036
1002
// CalculateMaxSize returns the real max size in bytes after considering the default max size and the flag options.
1037
1003
func CalculateMaxSize () uint64 {
1038
1004
if logging .logFile != "" {
@@ -1224,37 +1190,44 @@ func StartFlushDaemon(interval time.Duration) {
1224
1190
// lockAndFlushAll is like flushAll but locks l.mu first.
1225
1191
func (l * loggingT ) lockAndFlushAll () {
1226
1192
l .mu .Lock ()
1227
- files := l .flushAll ()
1193
+ needToSync := l .flushAll ()
1228
1194
l .mu .Unlock ()
1229
1195
// Some environments are slow when syncing and holding the lock might cause contention.
1230
- l .syncAll (files )
1196
+ l .syncAll (needToSync )
1231
1197
}
1232
1198
1233
1199
// flushAll flushes all the logs
1234
1200
// l.mu is held.
1235
- func (l * loggingT ) flushAll () []flushSyncWriter {
1236
- files := make ([]flushSyncWriter , 0 , severity .NumSeverity )
1201
+ //
1202
+ // The result is the number of files which need to be synced and the pointers to them.
1203
+ func (l * loggingT ) flushAll () fileArray {
1204
+ var needToSync fileArray
1205
+
1237
1206
// Flush from fatal down, in case there's trouble flushing.
1238
1207
for s := severity .FatalLog ; s >= severity .InfoLog ; s -- {
1239
1208
file := l .file [s ]
1240
- if file != nil {
1241
- _ = file .Flush () // ignore error
1209
+ if sb , ok := file .(* syncBuffer ); ok && sb .file != nil {
1210
+ _ = sb .Flush () // ignore error
1211
+ needToSync .files [needToSync .num ] = sb .file
1212
+ needToSync .num ++
1242
1213
}
1243
- files = append (files , file )
1244
1214
}
1245
1215
if logging .loggerOptions .flush != nil {
1246
1216
logging .loggerOptions .flush ()
1247
1217
}
1248
- return files
1218
+ return needToSync
1219
+ }
1220
+
1221
+ type fileArray struct {
1222
+ num int
1223
+ files [severity .NumSeverity ]* os.File
1249
1224
}
1250
1225
1251
1226
// syncAll attempts to "sync" their data to disk.
1252
- func (l * loggingT ) syncAll (files [] flushSyncWriter ) {
1227
+ func (l * loggingT ) syncAll (needToSync fileArray ) {
1253
1228
// Flush from fatal down, in case there's trouble flushing.
1254
- for _ , file := range files {
1255
- if file != nil {
1256
- _ = file .Sync () // ignore error
1257
- }
1229
+ for i := 0 ; i < needToSync .num ; i ++ {
1230
+ _ = needToSync .files [i ].Sync () // ignore error
1258
1231
}
1259
1232
}
1260
1233
0 commit comments