2019-09-03 20:10:48 +02:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
ring "container/ring"
|
|
|
|
"context"
|
2019-10-08 07:47:56 +02:00
|
|
|
"sync"
|
2019-09-03 20:10:48 +02:00
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
"github.com/minio/minio/cmd/logger/message/log"
|
|
|
|
"github.com/minio/minio/cmd/logger/target/console"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
"github.com/minio/minio/pkg/pubsub"
|
|
|
|
)
|
|
|
|
|
|
|
|
// number of log messages to buffer
|
|
|
|
const defaultLogBufferCount = 10000
|
|
|
|
|
|
|
|
//HTTPConsoleLoggerSys holds global console logger state
|
|
|
|
type HTTPConsoleLoggerSys struct {
|
2019-12-17 05:30:57 +01:00
|
|
|
sync.RWMutex
|
2019-09-03 20:10:48 +02:00
|
|
|
pubsub *pubsub.PubSub
|
|
|
|
console *console.Target
|
|
|
|
nodeName string
|
|
|
|
logBuf *ring.Ring
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:30:57 +01:00
|
|
|
func mustGetNodeName(endpointZones EndpointZones) (nodeName string) {
|
2019-11-20 02:42:27 +01:00
|
|
|
host, err := xnet.ParseHost(GetLocalPeer(endpointZones))
|
2019-09-03 20:10:48 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.FatalIf(err, "Unable to start console logging subsystem")
|
|
|
|
}
|
|
|
|
if globalIsDistXL {
|
|
|
|
nodeName = host.Name
|
|
|
|
}
|
2019-12-17 05:30:57 +01:00
|
|
|
return nodeName
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConsoleLogger - creates new HTTPConsoleLoggerSys with all nodes subscribed to
|
|
|
|
// the console logging pub sub system
|
|
|
|
func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys {
|
2019-09-03 20:10:48 +02:00
|
|
|
ps := pubsub.New()
|
|
|
|
return &HTTPConsoleLoggerSys{
|
2019-12-17 05:30:57 +01:00
|
|
|
pubsub: ps,
|
|
|
|
console: console.New(),
|
|
|
|
logBuf: ring.New(defaultLogBufferCount),
|
2019-09-03 20:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:30:57 +01:00
|
|
|
// SetNodeName - sets the node name if any after distributed setup has initialized
|
|
|
|
func (sys *HTTPConsoleLoggerSys) SetNodeName(endpointZones EndpointZones) {
|
|
|
|
sys.nodeName = mustGetNodeName(endpointZones)
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:10:48 +02:00
|
|
|
// HasLogListeners returns true if console log listeners are registered
|
|
|
|
// for this node or peers
|
|
|
|
func (sys *HTTPConsoleLoggerSys) HasLogListeners() bool {
|
|
|
|
return sys != nil && sys.pubsub.HasSubscribers()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe starts console logging for this node.
|
2020-04-16 19:56:18 +02:00
|
|
|
func (sys *HTTPConsoleLoggerSys) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, node string, last int, logKind string, filter func(entry interface{}) bool) {
|
2019-12-17 05:30:57 +01:00
|
|
|
// Enable console logging for remote client.
|
|
|
|
if !sys.HasLogListeners() {
|
|
|
|
logger.AddTarget(sys)
|
2019-09-03 20:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt := 0
|
|
|
|
// by default send all console logs in the ring buffer unless node or limit query parameters
|
|
|
|
// are set.
|
2020-03-20 23:13:41 +01:00
|
|
|
var lastN []log.Info
|
2019-09-03 20:10:48 +02:00
|
|
|
if last > defaultLogBufferCount || last <= 0 {
|
|
|
|
last = defaultLogBufferCount
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:13:41 +01:00
|
|
|
lastN = make([]log.Info, last)
|
2019-12-17 05:30:57 +01:00
|
|
|
sys.RLock()
|
2019-10-08 07:47:56 +02:00
|
|
|
sys.logBuf.Do(func(p interface{}) {
|
2020-03-20 23:13:41 +01:00
|
|
|
if p != nil {
|
|
|
|
lg, ok := p.(log.Info)
|
|
|
|
if ok && lg.SendLog(node, logKind) {
|
|
|
|
lastN[cnt%last] = lg
|
|
|
|
cnt++
|
|
|
|
}
|
2019-09-03 20:10:48 +02:00
|
|
|
}
|
|
|
|
})
|
2019-12-17 05:30:57 +01:00
|
|
|
sys.RUnlock()
|
2019-09-03 20:10:48 +02: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 23:13:41 +01:00
|
|
|
if (entry == log.Info{}) {
|
2019-09-03 20:10:48 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case subCh <- entry:
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sys.pubsub.Subscribe(subCh, doneCh, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send log message 'e' to console and publish to console
|
|
|
|
// log pubsub system
|
2019-10-12 03:50:54 +02:00
|
|
|
func (sys *HTTPConsoleLoggerSys) Send(e interface{}, logKind string) error {
|
2020-03-20 23:00:44 +01:00
|
|
|
var lg log.Info
|
2019-09-22 10:24:32 +02:00
|
|
|
switch e := e.(type) {
|
|
|
|
case log.Entry:
|
2020-03-20 23:00:44 +01:00
|
|
|
lg = log.Info{Entry: e, NodeName: sys.nodeName}
|
2019-09-22 10:24:32 +02:00
|
|
|
case string:
|
2020-03-20 23:00:44 +01:00
|
|
|
lg = log.Info{ConsoleMsg: e, NodeName: sys.nodeName}
|
2019-09-22 10:24:32 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 20:10:48 +02:00
|
|
|
sys.pubsub.Publish(lg)
|
2019-12-17 05:30:57 +01:00
|
|
|
sys.Lock()
|
2019-09-03 20:10:48 +02:00
|
|
|
// add log to ring buffer
|
|
|
|
sys.logBuf.Value = lg
|
|
|
|
sys.logBuf = sys.logBuf.Next()
|
2019-12-17 05:30:57 +01:00
|
|
|
sys.Unlock()
|
2019-09-03 20:10:48 +02:00
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
return sys.console.Send(e, string(logger.All))
|
2019-09-03 20:10:48 +02:00
|
|
|
}
|