From 9e2b2d61d46b753e84976c3850580ed8159215a4 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Thu, 22 Oct 2020 10:17:05 +0200
Subject: [PATCH] logging in tview working

---
 cmd/gosdn-tview/grpc/commands.go |  2 +-
 nucleus/cli-handling.go          | 31 ++++++++++++++++++++-----------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/cmd/gosdn-tview/grpc/commands.go b/cmd/gosdn-tview/grpc/commands.go
index c243d6517..74d2b0955 100644
--- a/cmd/gosdn-tview/grpc/commands.go
+++ b/cmd/gosdn-tview/grpc/commands.go
@@ -32,7 +32,7 @@ var CommandList = []command{
 
 //Connect creates a new connection to the gRPC server
 func Connect() (*grpc.ClientConn, error) {
-	address := "localhost:55055"
+	address := "141.100.70.170:55066"
 	return grpc.Dial(address, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second), grpc.WithBlock())
 }
 
diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go
index 1f4d69813..2afb6f6f3 100644
--- a/nucleus/cli-handling.go
+++ b/nucleus/cli-handling.go
@@ -8,9 +8,10 @@ package nucleus
 
 import (
 	"context"
+	"io"
 	"net"
+	"os"
 	"sync"
-	"time"
 
 	pb "code.fbi.h-da.de/cocsn/gosdn/api/proto"
 	"code.fbi.h-da.de/cocsn/gosdn/log"
@@ -33,6 +34,17 @@ type server struct {
 	logConnections []*LogConnection
 }
 
+var srv *server
+
+type buf []byte
+
+func (b *buf) Write(p []byte) (n int, err error) {
+
+	reply := pb.LogReply{Log: string(p)}
+	srv.BroadcastLog(&reply)
+	return len(p), nil
+}
+
 func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
 	log.Info("Received: ", in.GetName())
 	return &pb.HelloReply{Message: "Hello " + in.GetName(), GoSDNInfo: "goSDN in version: DEVELOP"}, nil
@@ -88,6 +100,8 @@ func (s *server) Shutdown(ctx context.Context, in *pb.ShutdownRequest) (*pb.Shut
 func getCLIGoing(core *Core) {
 
 	var logConnections []*LogConnection
+	var logBuffer buf
+
 	log.Info("Starting: GetCLIGoing")
 	// Boot-up the control interface for the cli
 	cliControlListener, err := net.Listen("tcp", core.config.CliSocket)
@@ -96,18 +110,13 @@ func getCLIGoing(core *Core) {
 	}
 
 	cliControlServer := grpc.NewServer()
-	server := &server{core: core, logConnections: logConnections}
+	srv = &server{core: core, logConnections: logConnections}
 
-	pb.RegisterGrpcCliServer(cliControlServer, server)
+	//TODO: move?
+	wrt := io.MultiWriter(os.Stdout, &logBuffer)
+	log.Output(wrt)
 
-	//TODO: remove!
-	go func() {
-		ticker := time.NewTicker(5 * time.Second)
-		for t := range ticker.C {
-			reply := pb.LogReply{Log: t.String()}
-			server.BroadcastLog(&reply)
-		}
-	}()
+	pb.RegisterGrpcCliServer(cliControlServer, srv)
 
 	if err := cliControlServer.Serve(cliControlListener); err != nil {
 		log.Fatal(err)
-- 
GitLab