| 
									
										
										
										
											2021-04-19 03:41:13 +08: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-04 02:10:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	ring "container/ring" | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/logger/message/log" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/logger/target/console" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/pubsub" | 
					
						
							| 
									
										
										
										
											2021-06-15 05:54:37 +08:00
										 |  |  | 	xnet "github.com/minio/pkg/net" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // number of log messages to buffer
 | 
					
						
							|  |  |  | const defaultLogBufferCount = 10000 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | // HTTPConsoleLoggerSys holds global console logger state
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | type HTTPConsoleLoggerSys struct { | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sync.RWMutex | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	pubsub   *pubsub.PubSub | 
					
						
							|  |  |  | 	console  *console.Target | 
					
						
							|  |  |  | 	nodeName string | 
					
						
							|  |  |  | 	logBuf   *ring.Ring | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | // NewConsoleLogger - creates new HTTPConsoleLoggerSys with all nodes subscribed to
 | 
					
						
							|  |  |  | // the console logging pub sub system
 | 
					
						
							|  |  |  | func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys { | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	ps := pubsub.New() | 
					
						
							|  |  |  | 	return &HTTPConsoleLoggerSys{ | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 		pubsub:  ps, | 
					
						
							|  |  |  | 		console: console.New(), | 
					
						
							|  |  |  | 		logBuf:  ring.New(defaultLogBufferCount), | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | // SetNodeName - sets the node name if any after distributed setup has initialized
 | 
					
						
							| 
									
										
										
										
											2021-03-27 02:37:58 +08: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-17 12:30:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | // HasLogListeners returns true if console log listeners are registered
 | 
					
						
							|  |  |  | // for this node or peers
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) HasLogListeners() bool { | 
					
						
							| 
									
										
										
										
											2021-01-05 01:40:30 +08:00
										 |  |  | 	return sys != nil && sys.pubsub.NumSubscribers() > 0 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Subscribe starts console logging for this node.
 | 
					
						
							| 
									
										
										
										
											2020-04-17 01:56:18 +08: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 12:30:57 +08:00
										 |  |  | 	// Enable console logging for remote client.
 | 
					
						
							|  |  |  | 	if !sys.HasLogListeners() { | 
					
						
							|  |  |  | 		logger.AddTarget(sys) | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cnt := 0 | 
					
						
							|  |  |  | 	// by default send all console logs in the ring buffer unless node or limit query parameters
 | 
					
						
							|  |  |  | 	// are set.
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 	var lastN []log.Info | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	if last > defaultLogBufferCount || last <= 0 { | 
					
						
							|  |  |  | 		last = defaultLogBufferCount | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 	lastN = make([]log.Info, last) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.RLock() | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | 	sys.logBuf.Do(func(p interface{}) { | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 		if p != nil { | 
					
						
							|  |  |  | 			lg, ok := p.(log.Info) | 
					
						
							|  |  |  | 			if ok && lg.SendLog(node, logKind) { | 
					
						
							|  |  |  | 				lastN[cnt%last] = lg | 
					
						
							|  |  |  | 				cnt++ | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.RUnlock() | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08: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-21 06:13:41 +08:00
										 |  |  | 			if (entry == log.Info{}) { | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case subCh <- entry: | 
					
						
							|  |  |  | 			case <-doneCh: | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sys.pubsub.Subscribe(subCh, doneCh, filter) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-14 00:39:13 +08:00
										 |  |  | // Init if HTTPConsoleLoggerSys is valid, always returns nil right now
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Init() error { | 
					
						
							| 
									
										
										
										
											2020-08-17 01:25:00 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-03 07:19:44 +08: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-16 09:02:54 +08: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-04 02:10:48 +08:00
										 |  |  | // Send log message 'e' to console and publish to console
 | 
					
						
							|  |  |  | // log pubsub system
 | 
					
						
							| 
									
										
										
										
											2019-10-12 09:50:54 +08:00
										 |  |  | func (sys *HTTPConsoleLoggerSys) Send(e interface{}, logKind string) error { | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 	var lg log.Info | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	switch e := e.(type) { | 
					
						
							|  |  |  | 	case log.Entry: | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 		lg = log.Info{Entry: e, NodeName: sys.nodeName} | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	case string: | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 		lg = log.Info{ConsoleMsg: e, NodeName: sys.nodeName} | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	sys.pubsub.Publish(lg) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.Lock() | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	// add log to ring buffer
 | 
					
						
							|  |  |  | 	sys.logBuf.Value = lg | 
					
						
							|  |  |  | 	sys.logBuf = sys.logBuf.Next() | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.Unlock() | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	return sys.console.Send(e, string(logger.All)) | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | } |