github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/debuglog_db.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package apiserver 5 6 import ( 7 "net/http" 8 9 "github.com/juju/errors" 10 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/state" 13 ) 14 15 func newDebugLogDBHandler(ctxt httpContext) http.Handler { 16 return newDebugLogHandler(ctxt, handleDebugLogDBRequest) 17 } 18 19 func handleDebugLogDBRequest( 20 st state.LogTailerState, 21 reqParams *debugLogParams, 22 socket debugLogSocket, 23 stop <-chan struct{}, 24 ) error { 25 params := makeLogTailerParams(reqParams) 26 tailer, err := newLogTailer(st, params) 27 if err != nil { 28 return errors.Trace(err) 29 } 30 defer tailer.Stop() 31 32 // Indicate that all is well. 33 socket.sendOk() 34 35 var lineCount uint 36 for { 37 select { 38 case <-stop: 39 return nil 40 case rec, ok := <-tailer.Logs(): 41 if !ok { 42 return errors.Annotate(tailer.Err(), "tailer stopped") 43 } 44 45 if err := socket.sendLogRecord(formatLogRecord(rec)); err != nil { 46 return errors.Annotate(err, "sending failed") 47 } 48 49 lineCount++ 50 if reqParams.maxLines > 0 && lineCount == reqParams.maxLines { 51 return nil 52 } 53 } 54 } 55 } 56 57 func makeLogTailerParams(reqParams *debugLogParams) *state.LogTailerParams { 58 params := &state.LogTailerParams{ 59 MinLevel: reqParams.filterLevel, 60 NoTail: reqParams.noTail, 61 InitialLines: int(reqParams.backlog), 62 IncludeEntity: reqParams.includeEntity, 63 ExcludeEntity: reqParams.excludeEntity, 64 IncludeModule: reqParams.includeModule, 65 ExcludeModule: reqParams.excludeModule, 66 } 67 if reqParams.fromTheStart { 68 params.InitialLines = 0 69 } 70 return params 71 } 72 73 func formatLogRecord(r *state.LogRecord) *params.LogMessage { 74 return ¶ms.LogMessage{ 75 Entity: r.Entity.String(), 76 Timestamp: r.Time, 77 Severity: r.Level.String(), 78 Module: r.Module, 79 Location: r.Location, 80 Message: r.Message, 81 } 82 } 83 84 var newLogTailer = _newLogTailer // For replacing in tests 85 86 func _newLogTailer(st state.LogTailerState, params *state.LogTailerParams) (state.LogTailer, error) { 87 return state.NewLogTailer(st, params) 88 }