1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 02:21:48 +08:00
Files
kubo/core/corehttp/logs.go
Jeromy 94bdce63a7 vendor logging lib update
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
2015-11-05 15:57:21 -08:00

51 lines
1018 B
Go

package corehttp
import (
"io"
"net"
"net/http"
core "github.com/ipfs/go-ipfs/core"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
)
type writeErrNotifier struct {
w io.Writer
errs chan error
}
func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) {
ch := make(chan error, 1)
return &writeErrNotifier{
w: w,
errs: ch,
}, ch
}
func (w *writeErrNotifier) Write(b []byte) (int, error) {
n, err := w.w.Write(b)
if err != nil {
select {
case w.errs <- err:
default:
}
}
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
return n, err
}
func LogOption() ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
wnf, errs := newWriteErrNotifier(w)
logging.WriterGroup.AddWriter(wnf)
log.Event(n.Context(), "log API client connected")
<-errs
})
return mux, nil
}
}