github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/daemon/logger/plugin.go (about)

     1  package logger // import "github.com/docker/docker/daemon/logger"
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/docker/docker/api/types/plugins/logdriver"
    10  	getter "github.com/docker/docker/pkg/plugingetter"
    11  	"github.com/docker/docker/pkg/stringid"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  var pluginGetter getter.PluginGetter
    16  
    17  const extName = "LogDriver"
    18  
    19  // logPlugin defines the available functions that logging plugins must implement.
    20  type logPlugin interface {
    21  	StartLogging(streamPath string, info Info) (err error)
    22  	StopLogging(streamPath string) (err error)
    23  	Capabilities() (cap Capability, err error)
    24  	ReadLogs(info Info, config ReadConfig) (stream io.ReadCloser, err error)
    25  }
    26  
    27  // RegisterPluginGetter sets the plugingetter
    28  func RegisterPluginGetter(plugingetter getter.PluginGetter) {
    29  	pluginGetter = plugingetter
    30  }
    31  
    32  // GetDriver returns a logging driver by its name.
    33  // If the driver is empty, it looks for the local driver.
    34  func getPlugin(name string, mode int) (Creator, error) {
    35  	p, err := pluginGetter.Get(name, extName, mode)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("error looking up logging plugin %s: %v", name, err)
    38  	}
    39  
    40  	d := &logPluginProxy{p.Client()}
    41  	return makePluginCreator(name, d, p.ScopedPath), nil
    42  }
    43  
    44  func makePluginCreator(name string, l *logPluginProxy, scopePath func(s string) string) Creator {
    45  	return func(logCtx Info) (logger Logger, err error) {
    46  		defer func() {
    47  			if err != nil {
    48  				pluginGetter.Get(name, extName, getter.Release)
    49  			}
    50  		}()
    51  
    52  		unscopedPath := filepath.Join("/", "run", "docker", "logging")
    53  		logRoot := scopePath(unscopedPath)
    54  		if err := os.MkdirAll(logRoot, 0700); err != nil {
    55  			return nil, err
    56  		}
    57  
    58  		id := stringid.GenerateNonCryptoID()
    59  		a := &pluginAdapter{
    60  			driverName: name,
    61  			id:         id,
    62  			plugin:     l,
    63  			fifoPath:   filepath.Join(logRoot, id),
    64  			logInfo:    logCtx,
    65  		}
    66  
    67  		cap, err := a.plugin.Capabilities()
    68  		if err == nil {
    69  			a.capabilities = cap
    70  		}
    71  
    72  		stream, err := openPluginStream(a)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  
    77  		a.stream = stream
    78  		a.enc = logdriver.NewLogEntryEncoder(a.stream)
    79  
    80  		if err := l.StartLogging(filepath.Join(unscopedPath, id), logCtx); err != nil {
    81  			return nil, errors.Wrapf(err, "error creating logger")
    82  		}
    83  
    84  		if cap.ReadLogs {
    85  			return &pluginAdapterWithRead{a}, nil
    86  		}
    87  
    88  		return a, nil
    89  	}
    90  }