Browse Source

move yql connector to ydb

move code
uzhas 1 year ago
parent
commit
de6e39881d

+ 21 - 0
library/go/core/log/compat/golog/log.go

@@ -0,0 +1,21 @@
+package golog
+
+import (
+	canal_log "github.com/siddontang/go-log/log"
+	"github.com/ydb-platform/ydb/library/go/core/log"
+)
+
+func SetLevel(level log.Level) {
+	switch level {
+	case log.DebugLevel:
+		canal_log.SetLevel(canal_log.LevelDebug)
+	case log.ErrorLevel:
+		canal_log.SetLevel(canal_log.LevelError)
+	case log.FatalLevel:
+		canal_log.SetLevel(canal_log.LevelFatal)
+	case log.InfoLevel:
+		canal_log.SetLevel(canal_log.LevelInfo)
+	case log.TraceLevel:
+		canal_log.SetLevel(canal_log.LevelTrace)
+	}
+}

+ 5 - 0
library/go/core/log/compat/golog/ya.make

@@ -0,0 +1,5 @@
+GO_LIBRARY()
+
+SRCS(log.go)
+
+END()

+ 202 - 0
library/go/core/log/compat/logrus/log.go

@@ -0,0 +1,202 @@
+package logrus
+
+import (
+	"io"
+	"runtime"
+	"strings"
+	"sync"
+
+	"github.com/sirupsen/logrus"
+	"github.com/ydb-platform/ydb/library/go/core/log"
+)
+
+/* Call frame calculations are copied from logrus package */
+var (
+
+	// qualified package name, cached at first use
+	logrusPackage string
+
+	// Positions in the call stack when tracing to report the calling method
+	minimumCallerDepth int
+
+	// Used for caller information initialisation
+	callerInitOnce sync.Once
+)
+
+const (
+	maximumCallerDepth int = 25
+	knownLogrusFrames  int = 4
+)
+
+func init() {
+	// start at the bottom of the stack before the package-name cache is primed
+	minimumCallerDepth = 1
+}
+
+// getPackageName reduces a fully qualified function name to the package name
+// There really ought to be to be a better way...
+func getPackageName(f string) string {
+	for {
+		lastPeriod := strings.LastIndex(f, ".")
+		lastSlash := strings.LastIndex(f, "/")
+		if lastPeriod > lastSlash {
+			f = f[:lastPeriod]
+		} else {
+			break
+		}
+	}
+
+	return f
+}
+
+func getCallerDepth() int {
+	// cache this package's fully-qualified name
+	callerInitOnce.Do(func() {
+		pcs := make([]uintptr, maximumCallerDepth)
+		_ = runtime.Callers(0, pcs)
+
+		// dynamic get the package name and the minimum caller depth
+		logrusIsNext := false
+		for i := 0; i < maximumCallerDepth; i++ {
+			funcName := runtime.FuncForPC(pcs[i]).Name()
+			if logrusIsNext {
+				logrusPackage = getPackageName(funcName)
+				break
+			}
+			if strings.Contains(funcName, "LogrusAdapter") {
+				logrusIsNext = true
+				continue
+			}
+		}
+
+		minimumCallerDepth = knownLogrusFrames
+	})
+
+	// Restrict the lookback frames to avoid runaway lookups
+	pcs := make([]uintptr, maximumCallerDepth)
+	depth := runtime.Callers(minimumCallerDepth, pcs)
+	frames := runtime.CallersFrames(pcs[:depth])
+	callerDepth := minimumCallerDepth
+
+	for f, again := frames.Next(); again; f, again = frames.Next() {
+		pkg := getPackageName(f.Function)
+
+		// If the caller isn't part of this package, we're done
+		if pkg != logrusPackage {
+			return callerDepth - 2
+		}
+		callerDepth++
+	}
+
+	// if we got here, we failed to find the caller's context
+	return 0
+}
+
+func convertLevel(level log.Level) logrus.Level {
+	switch level {
+	case log.TraceLevel:
+		return logrus.TraceLevel
+	case log.DebugLevel:
+		return logrus.DebugLevel
+	case log.InfoLevel:
+		return logrus.InfoLevel
+	case log.WarnLevel:
+		return logrus.WarnLevel
+	case log.ErrorLevel:
+		return logrus.ErrorLevel
+	case log.FatalLevel:
+		return logrus.FatalLevel
+	}
+
+	return logrus.PanicLevel
+}
+
+func SetLevel(level log.Level) {
+	logrus.SetLevel(convertLevel(level))
+}
+
+type LogrusAdapter struct {
+	logger         log.Logger
+	adaptCallstack bool
+	convertPrefix  bool
+}
+
+func (a *LogrusAdapter) Format(entry *logrus.Entry) ([]byte, error) {
+	var name *string
+	fields := make([]log.Field, 0, len(entry.Data))
+	for key, val := range entry.Data {
+		skip := false
+		if a.convertPrefix && key == "prefix" {
+			if w, ok := val.(string); ok {
+				name = &w
+				skip = true
+			}
+		}
+		if !skip {
+			fields = append(fields, log.Any(key, val))
+		}
+	}
+
+	var logger log.Logger
+	if a.adaptCallstack {
+		logger = log.AddCallerSkip(a.logger, getCallerDepth())
+	} else {
+		logger = a.logger
+	}
+
+	if a.convertPrefix && name != nil {
+		logger = logger.WithName(*name)
+	}
+
+	switch entry.Level {
+	case logrus.TraceLevel:
+		logger.Trace(entry.Message, fields...)
+	case logrus.DebugLevel:
+		logger.Debug(entry.Message, fields...)
+	case logrus.InfoLevel:
+		logger.Info(entry.Message, fields...)
+	case logrus.WarnLevel:
+		logger.Warn(entry.Message, fields...)
+	case logrus.ErrorLevel:
+		logger.Error(entry.Message, fields...)
+	case logrus.FatalLevel:
+		logger.Fatal(entry.Message, fields...)
+	case logrus.PanicLevel:
+		logger.Fatal(entry.Message, fields...)
+	}
+
+	return nil, nil
+}
+
+type Option func(*LogrusAdapter)
+
+func DontAdaptCallstack() Option {
+	return func(adapter *LogrusAdapter) {
+		adapter.adaptCallstack = false
+	}
+}
+
+func ConvertPrefix() Option {
+	return func(adapter *LogrusAdapter) {
+		adapter.convertPrefix = true
+	}
+}
+
+// AdaptLogrus replaces logr formatter by wrapped logger
+func AdaptLogrus(logr *logrus.Logger, logger log.Logger, level log.Level, opts ...Option) {
+	logr.SetLevel(convertLevel(level))
+
+	adapter := &LogrusAdapter{logger, true, false}
+
+	for _, opt := range opts {
+		opt(adapter)
+	}
+
+	logr.SetFormatter(adapter)
+	logr.SetOutput(io.Discard)
+}
+
+// AdaptStandardLogger replaces logrus.StandardLogger() formatter by wrapped logger
+func AdaptStandardLogger(logger log.Logger, level log.Level, opts ...Option) {
+	AdaptLogrus(logrus.StandardLogger(), logger, level, opts...)
+}

+ 5 - 0
library/go/core/log/compat/logrus/ya.make

@@ -0,0 +1,5 @@
+GO_LIBRARY()
+
+SRCS(log.go)
+
+END()

+ 76 - 0
library/go/core/log/compat/pion/log.go

@@ -0,0 +1,76 @@
+package pion
+
+import (
+	"github.com/pion/logging"
+	"github.com/ydb-platform/ydb/library/go/core/log"
+)
+
+type LoggerFactory struct {
+	StandardLogger log.Logger
+}
+
+func (l LoggerFactory) NewLogger(scope string) logging.LeveledLogger {
+	return LoggerAdapter{
+		standardLogger: l.StandardLogger,
+		scope:          scope,
+	}
+}
+
+type LoggerAdapter struct {
+	standardLogger log.Logger
+	scope          string
+}
+
+func (a LoggerAdapter) Trace(msg string) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Trace(a.addScope(msg))
+}
+
+func (a LoggerAdapter) Tracef(format string, args ...interface{}) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Tracef(a.addScope(format), args...)
+}
+
+func (a LoggerAdapter) Debug(msg string) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Debug(a.addScope(msg))
+}
+
+func (a LoggerAdapter) Debugf(format string, args ...interface{}) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Debugf(a.addScope(format), args...)
+}
+
+func (a LoggerAdapter) Info(msg string) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Info(a.addScope(msg))
+}
+
+func (a LoggerAdapter) Infof(format string, args ...interface{}) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Infof(a.addScope(format), args...)
+}
+
+func (a LoggerAdapter) Warn(msg string) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Warn(a.addScope(msg))
+}
+
+func (a LoggerAdapter) Warnf(format string, args ...interface{}) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Warnf(a.addScope(format), args...)
+}
+
+func (a LoggerAdapter) Error(msg string) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Error(a.addScope(msg))
+}
+
+func (a LoggerAdapter) Errorf(format string, args ...interface{}) {
+	log.AddCallerSkip(a.standardLogger, 1)
+	a.standardLogger.Errorf(a.addScope(format), args...)
+}
+
+func (a LoggerAdapter) addScope(s string) string {
+	return a.scope + ": " + s
+}

+ 5 - 0
library/go/core/log/compat/pion/ya.make

@@ -0,0 +1,5 @@
+GO_LIBRARY()
+
+SRCS(log.go)
+
+END()

+ 54 - 0
library/go/core/log/compat/stdlog/stdlog.go

@@ -0,0 +1,54 @@
+package stdlog
+
+import (
+	"bytes"
+	"fmt"
+	stdlog "log"
+
+	"github.com/ydb-platform/ydb/library/go/core/log"
+)
+
+func levelToFunc(logger log.Logger, lvl log.Level) (func(msg string, fields ...log.Field), error) {
+	switch lvl {
+	case log.DebugLevel:
+		return logger.Debug, nil
+	case log.TraceLevel:
+		return logger.Trace, nil
+	case log.InfoLevel:
+		return logger.Info, nil
+	case log.WarnLevel:
+		return logger.Warn, nil
+	case log.ErrorLevel:
+		return logger.Error, nil
+	case log.FatalLevel:
+		return logger.Fatal, nil
+	}
+
+	return nil, fmt.Errorf("unknown log level: %v", lvl)
+}
+
+type loggerWriter struct {
+	logFunc func(msg string, fields ...log.Field)
+}
+
+func (w *loggerWriter) Write(p []byte) (int, error) {
+	p = bytes.TrimSpace(p)
+	w.logFunc(string(p))
+	return len(p), nil
+}
+
+// New creates stdlib log.Logger that writes to provided logger on Error level
+func New(logger log.Logger) *stdlog.Logger {
+	l := log.AddCallerSkip(logger, 3)
+	return stdlog.New(&loggerWriter{logFunc: l.Error}, "", 0)
+}
+
+// NewAt creates stdlib log.Logger that writes to provided logger on specified level
+func NewAt(logger log.Logger, lvl log.Level) (*stdlog.Logger, error) {
+	l := log.AddCallerSkip(logger, 3)
+	logFunc, err := levelToFunc(l, lvl)
+	if err != nil {
+		return nil, err
+	}
+	return stdlog.New(&loggerWriter{logFunc: logFunc}, "", 0), nil
+}

+ 5 - 0
library/go/core/log/compat/stdlog/ya.make

@@ -0,0 +1,5 @@
+GO_LIBRARY()
+
+SRCS(stdlog.go)
+
+END()

+ 6 - 0
library/go/core/log/compat/ya.make

@@ -0,0 +1,6 @@
+RECURSE(
+    golog
+    logrus
+    pion
+    stdlog
+)

+ 124 - 0
library/go/core/log/ctxlog/ctxlog.go

@@ -0,0 +1,124 @@
+package ctxlog
+
+import (
+	"context"
+	"fmt"
+
+	"github.com/ydb-platform/ydb/library/go/core/log"
+)
+
+type ctxKey struct{}
+
+// ContextFields returns log.Fields bound with ctx.
+// If no fields are bound, it returns nil.
+func ContextFields(ctx context.Context) []log.Field {
+	fs, _ := ctx.Value(ctxKey{}).([]log.Field)
+	return fs
+}
+
+// WithFields returns a new context that is bound with given fields and based
+// on parent ctx.
+func WithFields(ctx context.Context, fields ...log.Field) context.Context {
+	if len(fields) == 0 {
+		return ctx
+	}
+
+	return context.WithValue(ctx, ctxKey{}, mergeFields(ContextFields(ctx), fields))
+}
+
+// Trace logs at Trace log level using fields both from arguments and ones that
+// are bound to ctx.
+func Trace(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Trace(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Debug logs at Debug log level using fields both from arguments and ones that
+// are bound to ctx.
+func Debug(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Debug(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Info logs at Info log level using fields both from arguments and ones that
+// are bound to ctx.
+func Info(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Info(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Warn logs at Warn log level using fields both from arguments and ones that
+// are bound to ctx.
+func Warn(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Warn(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Error logs at Error log level using fields both from arguments and ones that
+// are bound to ctx.
+func Error(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Error(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Fatal logs at Fatal log level using fields both from arguments and ones that
+// are bound to ctx.
+func Fatal(ctx context.Context, l log.Logger, msg string, fields ...log.Field) {
+	log.AddCallerSkip(l, 1).Fatal(msg, mergeFields(ContextFields(ctx), fields)...)
+}
+
+// Tracef logs at Trace log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Tracef(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Trace(msg, ContextFields(ctx)...)
+}
+
+// Debugf logs at Debug log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Debugf(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Debug(msg, ContextFields(ctx)...)
+}
+
+// Infof logs at Info log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Infof(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Info(msg, ContextFields(ctx)...)
+}
+
+// Warnf logs at Warn log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Warnf(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Warn(msg, ContextFields(ctx)...)
+}
+
+// Errorf logs at Error log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Errorf(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Error(msg, ContextFields(ctx)...)
+}
+
+// Fatalf logs at Fatal log level using fields that are bound to ctx.
+// The message is formatted using provided arguments.
+func Fatalf(ctx context.Context, l log.Logger, format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	log.AddCallerSkip(l, 1).Fatal(msg, ContextFields(ctx)...)
+}
+
+func mergeFields(a, b []log.Field) []log.Field {
+	if a == nil {
+		return b
+	}
+	if b == nil {
+		return a
+	}
+
+	// NOTE: just append() here is unsafe. If a caller passed slice of fields
+	// followed by ... with capacity greater than length, then simultaneous
+	// logging will lead to a data race condition.
+	//
+	// See https://golang.org/ref/spec#Passing_arguments_to_..._parameters
+	c := make([]log.Field, len(a)+len(b))
+	n := copy(c, a)
+	copy(c[n:], b)
+	return c
+}

Some files were not shown because too many files changed in this diff