2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-09-03 14:10:48 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
ring "container/ring"
|
|
|
|
"context"
|
2019-10-08 01:47:56 -04:00
|
|
|
"sync"
|
2019-09-03 14:10:48 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
|
|
|
"github.com/minio/minio/internal/logger/message/log"
|
|
|
|
"github.com/minio/minio/internal/logger/target/console"
|
|
|
|
xnet "github.com/minio/minio/internal/net"
|
|
|
|
"github.com/minio/minio/internal/pubsub"
|
2019-09-03 14:10:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// number of log messages to buffer
|
|
|
|
const defaultLogBufferCount = 10000
|
|
|
|
|
|
|
|
//HTTPConsoleLoggerSys holds global console logger state
|
|
|
|
type HTTPConsoleLoggerSys struct {
|
2019-12-16 23:30:57 -05:00
|
|
|
sync.RWMutex
|
2019-09-03 14:10:48 -04:00
|
|
|
pubsub *pubsub.PubSub
|
|
|
|
console *console.Target
|
|
|
|
nodeName string
|
|
|
|
logBuf *ring.Ring
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:30:57 -05:00
|
|
|
// NewConsoleLogger - creates new HTTPConsoleLoggerSys with all nodes subscribed to
|
|
|
|
// the console logging pub sub system
|
|
|
|
func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys {
|
2019-09-03 14:10:48 -04:00
|
|
|
ps := pubsub.New()
|
|
|
|
return &HTTPConsoleLoggerSys{
|
2019-12-16 23:30:57 -05:00
|
|
|
pubsub: ps,
|
|
|
|
console: console.New(),
|
|
|
|
logBuf: ring.New(defaultLogBufferCount),
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:30:57 -05:00
|
|
|
// SetNodeName - sets the node name if any after distributed setup has initialized
|
2021-03-26 14:37:58 -04:00
|
|
|
func (sys *HTTPConsoleLoggerSys) SetNodeName(nodeName string) {
|
|
|
|
if !globalIsDistErasure {
|
|
|
|
sys.nodeName = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := xnet.ParseHost(globalLocalNodeName)
|
|
|
|
if err != nil {
|
|
|
|
logger.FatalIf(err, "Unable to start console logging subsystem")
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.nodeName = host.Name
|
2019-12-16 23:30:57 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:10:48 -04:00
|
|
|
// HasLogListeners returns true if console log listeners are registered
|
|
|
|
// for this node or peers
|
|
|
|
func (sys *HTTPConsoleLoggerSys) HasLogListeners() bool {
|
2021-01-04 12:40:30 -05:00
|
|
|
return sys != nil && sys.pubsub.NumSubscribers() > 0
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe starts console logging for this node.
|
2020-04-16 13:56:18 -04:00
|
|
|
func (sys *HTTPConsoleLoggerSys) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, node string, last int, logKind string, filter func(entry interface{}) bool) {
|
2019-12-16 23:30:57 -05:00
|
|
|
// Enable console logging for remote client.
|
|
|
|
if !sys.HasLogListeners() {
|
|
|
|
logger.AddTarget(sys)
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt := 0
|
|
|
|
// by default send all console logs in the ring buffer unless node or limit query parameters
|
|
|
|
// are set.
|
2020-03-20 18:13:41 -04:00
|
|
|
var lastN []log.Info
|
2019-09-03 14:10:48 -04:00
|
|
|
if last > defaultLogBufferCount || last <= 0 {
|
|
|
|
last = defaultLogBufferCount
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:13:41 -04:00
|
|
|
lastN = make([]log.Info, last)
|
2019-12-16 23:30:57 -05:00
|
|
|
sys.RLock()
|
2019-10-08 01:47:56 -04:00
|
|
|
sys.logBuf.Do(func(p interface{}) {
|
2020-03-20 18:13:41 -04:00
|
|
|
if p != nil {
|
|
|
|
lg, ok := p.(log.Info)
|
|
|
|
if ok && lg.SendLog(node, logKind) {
|
|
|
|
lastN[cnt%last] = lg
|
|
|
|
cnt++
|
|
|
|
}
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|
|
|
|
})
|
2019-12-16 23:30:57 -05:00
|
|
|
sys.RUnlock()
|
2019-09-03 14:10:48 -04:00
|
|
|
// send last n console log messages in order filtered by node
|
|
|
|
if cnt > 0 {
|
|
|
|
for i := 0; i < last; i++ {
|
|
|
|
entry := lastN[(cnt+i)%last]
|
2020-03-20 18:13:41 -04:00
|
|
|
if (entry == log.Info{}) {
|
2019-09-03 14:10:48 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case subCh <- entry:
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sys.pubsub.Subscribe(subCh, doneCh, filter)
|
|
|
|
}
|
|
|
|
|
2020-08-16 13:25:00 -04:00
|
|
|
// Validate if HTTPConsoleLoggerSys is valid, always returns nil right now
|
|
|
|
func (sys *HTTPConsoleLoggerSys) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:19:44 -04:00
|
|
|
// Endpoint - dummy function for interface compatibility
|
|
|
|
func (sys *HTTPConsoleLoggerSys) Endpoint() string {
|
|
|
|
return sys.console.Endpoint()
|
|
|
|
}
|
|
|
|
|
|
|
|
// String - stringer function for interface compatibility
|
|
|
|
func (sys *HTTPConsoleLoggerSys) String() string {
|
|
|
|
return "console+http"
|
|
|
|
}
|
|
|
|
|
2020-09-15 21:02:54 -04:00
|
|
|
// Content returns the console stdout log
|
|
|
|
func (sys *HTTPConsoleLoggerSys) Content() (logs []log.Entry) {
|
|
|
|
sys.RLock()
|
|
|
|
sys.logBuf.Do(func(p interface{}) {
|
|
|
|
if p != nil {
|
|
|
|
lg, ok := p.(log.Info)
|
|
|
|
if ok {
|
|
|
|
if (lg.Entry != log.Entry{}) {
|
|
|
|
logs = append(logs, lg.Entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
sys.RUnlock()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:10:48 -04:00
|
|
|
// Send log message 'e' to console and publish to console
|
|
|
|
// log pubsub system
|
2019-10-11 21:50:54 -04:00
|
|
|
func (sys *HTTPConsoleLoggerSys) Send(e interface{}, logKind string) error {
|
2020-03-20 18:00:44 -04:00
|
|
|
var lg log.Info
|
2019-09-22 04:24:32 -04:00
|
|
|
switch e := e.(type) {
|
|
|
|
case log.Entry:
|
2020-03-20 18:00:44 -04:00
|
|
|
lg = log.Info{Entry: e, NodeName: sys.nodeName}
|
2019-09-22 04:24:32 -04:00
|
|
|
case string:
|
2020-03-20 18:00:44 -04:00
|
|
|
lg = log.Info{ConsoleMsg: e, NodeName: sys.nodeName}
|
2019-09-22 04:24:32 -04:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:10:48 -04:00
|
|
|
sys.pubsub.Publish(lg)
|
2019-12-16 23:30:57 -05:00
|
|
|
sys.Lock()
|
2019-09-03 14:10:48 -04:00
|
|
|
// add log to ring buffer
|
|
|
|
sys.logBuf.Value = lg
|
|
|
|
sys.logBuf = sys.logBuf.Next()
|
2019-12-16 23:30:57 -05:00
|
|
|
sys.Unlock()
|
2019-09-03 14:10:48 -04:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
return sys.console.Send(e, string(logger.All))
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|