| 
									
										
										
										
											2019-09-04 02:10:48 +08: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 13:47:56 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08: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 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
										 |  |  | func mustGetNodeName(endpointZones EndpointZones) (nodeName string) { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 	host, err := xnet.ParseHost(GetLocalPeer(endpointZones)) | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.FatalIf(err, "Unable to start console logging subsystem") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	if globalIsDistErasure { | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 		nodeName = host.Name | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08: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-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
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) SetNodeName(endpointZones EndpointZones) { | 
					
						
							|  |  |  | 	sys.nodeName = mustGetNodeName(endpointZones) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 	return sys != nil && sys.pubsub.HasSubscribers() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 01:25:00 +08:00
										 |  |  | // Validate if HTTPConsoleLoggerSys is valid, always returns nil right now
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Validate() error { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |