Skip to content
Snippets Groups Projects
Commit a7f93cad authored by Malte Bauch's avatar Malte Bauch
Browse files

Merge branch 'v.0.1.0-codename-threadbare' into '61-unmarshal-json-with-ygot'

# Conflicts:
#   go.mod
#   sbi/restconf/client/ciena/client.go
parents ff18ed79 3ec15729
Branches
Tags
3 merge requests!90Develop,!74Resolve "Unmarshal JSON with ygot",!53V.0.1.0 Codename Threadbare
Pipeline #55628 passed with warnings
......@@ -18,23 +18,20 @@ documentation:pdf:
- documentation/design/documentation.pdf
.mdbook_common: &rust
before_script:
- cargo install mdbook
image:
name: rust:latest
image:
name: $CI_REGISTRY/danet/internetworking/mdbook:latest
entrypoint:
- ''
stage: deploy
script:
- mdbook build documentation --dest-dir public
cache:
paths:
- /root/.cargo/
documentation:static:
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
artifacts:
paths:
- documentation/public
- public
expire_in: 1 week
<<: *rust
......
......@@ -2,10 +2,10 @@ package main
import (
pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
"code.fbi.h-da.de/cocsn/gosdn/log"
"context"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"os"
"time"
......
......@@ -8,7 +8,7 @@ import (
"code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/app"
grpc "code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/grpc"
"code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/views"
"code.fbi.h-da.de/cocsn/gosdn/log"
log "github.com/sirupsen/logrus"
)
func main() {
......
package main
import (
"code.fbi.h-da.de/cocsn/gosdn/log"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"flag"
log "github.com/sirupsen/logrus"
)
func main() {
......@@ -16,7 +16,7 @@ func main() {
flag.Parse()
cliSocket := *cliListenAddr + ":" + *cliListenPort
log.Loglevel(log.DEBUG)
log.SetLevel(log.DebugLevel)
// Setup a channel to communicate if goSDN should shutdown.
IsRunningChannel := make(chan bool)
......
package database
import (
"code.fbi.h-da.de/cocsn/gosdn/log"
"errors"
"github.com/neo4j/neo4j-go-driver/neo4j"
log "github.com/sirupsen/logrus"
)
//Database is a database
......
......@@ -15,6 +15,7 @@ require (
github.com/openconfig/ygot v0.8.7
github.com/rivo/tview v0.0.0-20201018122409-d551c850a743
github.com/tidwall/gjson v1.6.3
github.com/sirupsen/logrus v1.4.2
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.23.0
......
package log
import (
"fmt"
"io"
"log/syslog"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
var logger *Logger
var once sync.Once
// Logger is a wrapper for log.Logger and provides
// methods to enable and disable logging.
type Logger struct {
DefaultWriter io.Writer
LoglevelWriter map[Level]io.Writer
toSyslog map[Level]bool
Loglevel Level
lock sync.Mutex
builder strings.Builder
}
func (l *Logger) buildMessage(level Level, syslog bool, args ...interface{}) {
if !syslog {
l.builder.WriteString(time.Now().Format(time.RFC3339))
}
l.builder.WriteRune('\t')
l.builder.WriteString(prefix(level))
l.builder.WriteRune('\t')
function, line := callers()
functionSplitted := strings.SplitAfter(function, "/")
function = functionSplitted[len(functionSplitted)-1]
l.builder.WriteString(function)
l.builder.WriteRune(':')
l.builder.WriteString(strconv.Itoa(line))
l.builder.WriteRune('\t')
l.builder.WriteString(fmt.Sprint(args...))
l.builder.WriteRune('\n')
}
func get() *Logger {
once.Do(func() {
logger = &Logger{
DefaultWriter: os.Stderr,
LoglevelWriter: make(map[Level]io.Writer),
toSyslog: make(map[Level]bool),
Loglevel: INFO,
lock: sync.Mutex{},
}
})
return logger
}
// Loglevel sets the verbosity of the logger
// Defaults to INFO
func Loglevel(level Level) {
l := get()
l.lock.Lock()
defer l.lock.Unlock()
l.Loglevel = level
}
// Output defines the output of the logger
// Defaults to os.Stderr
func Output(out io.Writer) {
l := get()
l.lock.Lock()
defer l.lock.Unlock()
l.DefaultWriter = out
}
// LoglevelOutput defines a special output
// for a certain log level
func LoglevelOutput(level Level, out io.Writer) {
l := get()
l.lock.Lock()
defer l.lock.Unlock()
l.LoglevelWriter[level] = out
if reflect.TypeOf(out) == reflect.TypeOf(&syslog.Writer{}) {
l.toSyslog[level] = true
}
}
// Debug passes the DEBUG flag and a
// message to the logger
func Debug(args ...interface{}) {
log(DEBUG, args...)
}
// Info passes the INFO flag and a
// message to the logger
func Info(args ...interface{}) {
log(INFO, args...)
}
// Warn passes the WARNING flag and a
// message to the logger
func Warn(args ...interface{}) {
log(WARNING, args...)
}
// Error passes the ERROR flag and a
// message to the logger
func Error(args ...interface{}) {
log(ERROR, args...)
}
// Fatal passes the FATAL flag and a
// message to the logger and calls
// os.Exit(1)
func Fatal(args ...interface{}) {
log(FATAL, args...)
os.Exit(1)
}
// Debugf passes the DEBUG flag,
// a formatted string and a
// message to the logger
func Debugf(format string, args ...interface{}) {
logf(DEBUG, format, args...)
}
// Infof passes the INFO flag,
// a formatted string and and a
// message to the logger
func Infof(format string, args ...interface{}) {
logf(INFO, format, args...)
}
// Warnf passes the WARNING flag,
// a formatted string and and a
// message to the logger
func Warnf(format string, args ...interface{}) {
logf(WARNING, format, args...)
}
// Errorf passes the ERROR flag,
// a formatted string and and a
// message to the logger
func Errorf(format string, args ...interface{}) {
logf(ERROR, format, args...)
}
// Fatalf passes the FATAL flag,
// a formatted string and and a
// message to the logger and calls
// os.Exit(1)
func Fatalf(format string, args ...interface{}) {
logf(FATAL, format, args...)
os.Exit(1)
}
func logf(level Level, format string, args ...interface{}) {
log(level, fmt.Sprintf(format, args...))
}
func log(level Level, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
l := get()
l.lock.Lock()
defer l.lock.Unlock()
defer l.builder.Reset()
if level <= l.Loglevel {
l.buildMessage(level, l.toSyslog[level], args...)
msg := []byte(l.builder.String())
writer, ok := l.LoglevelWriter[level]
var err error
if !ok {
_, err = l.DefaultWriter.Write(msg)
} else {
_, err = writer.Write(msg)
}
if err != nil {
panic(err)
}
}
}
func callers() (string, int) {
pc := make([]uintptr, 15)
n := runtime.Callers(5, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return frame.Function, frame.Line
}
func prefix(level Level) string {
switch level {
case FATAL:
return "FATAL"
case ERROR:
return "ERROR"
case WARNING:
return "WARNING"
case INFO:
return "INFO"
case DEBUG:
return "DEBUG"
}
return ""
}
package log
// Level is an 8 bit integer representing a
// log level for the logger
type Level uint8
// Constants for more verbose integer
// values
const (
FATAL Level = iota
ERROR
WARNING
INFO
DEBUG
)
......@@ -14,8 +14,8 @@ import (
"sync"
pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
"code.fbi.h-da.de/cocsn/gosdn/log"
"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/client/ciena"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
......@@ -120,7 +120,7 @@ func getCLIGoing(core *Core) {
//TODO: move?
wrt := io.MultiWriter(os.Stdout, &logBuffer)
log.Output(wrt)
log.SetOutput(wrt)
healthpb.RegisterHealthServer(cliControlServer, healthCheck)
pb.RegisterGrpcCliServer(cliControlServer, srv)
......
......@@ -2,10 +2,10 @@ package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/log"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/client/ciena"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"os"
)
......
......@@ -2,8 +2,8 @@ package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/log"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
log "github.com/sirupsen/logrus"
"time"
)
......
......@@ -3,7 +3,6 @@ package ciena
import (
"bytes"
"code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/log"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/util"
apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client"
......@@ -15,6 +14,7 @@ import (
"github.com/go-openapi/strfmt"
"github.com/openconfig/ygot/ygot"
"github.com/tidwall/gjson"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment