Skip to content

Commit

Permalink
Merge pull request #1252 from ersonp/clean/logs
Browse files Browse the repository at this point in the history
Clean logs
  • Loading branch information
jdknives authored Jun 17, 2022
2 parents 3cb49bc + b8f0d66 commit afed86e
Show file tree
Hide file tree
Showing 133 changed files with 608 additions and 596 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- remove transports cache from visor initialization and check them before make route
- `run-source` makefile directive write config to stdout & read config from stdin
- fixed skywire-visor uses skywire-config.json (default config name) without needing to specify
- changed the log levels of most of the logs making info level clutter free

### Added
- added `add-rhv` and `disable-rhv` flags to `skywire-visor` for adding remote hypervisor PK and disable remote hypervisor PK(s) on config file
Expand Down Expand Up @@ -53,6 +54,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `run-vpnsrv-dmsghttp` makefile directive
- `run-vpnsrv -dmsghttp-test` makefile directive
- `install-system-linux` and `install-system-linux-systray` makefile directives
- `trace` log level is added

## 0.6.0

Expand Down
32 changes: 16 additions & 16 deletions cmd/apps/skychat/skychat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"

ipc "github.com/james-barrow/golang-ipc"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
Expand All @@ -35,9 +34,8 @@ const (
port = routing.Port(1)
)

var log = logging.MustGetLogger("chat")
var addr = flag.String("addr", ":8001", "address to bind")
var r = netutil.NewRetrier(log, 50*time.Millisecond, netutil.DefaultMaxBackoff, 5, 2)
var r = netutil.NewRetrier(nil, 50*time.Millisecond, netutil.DefaultMaxBackoff, 5, 2)

var (
appCl *app.Client
Expand All @@ -56,11 +54,11 @@ func main() {
defer appCl.Close()

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
fmt.Printf("Failed to output build info: %v", err)
print(fmt.Sprintf("Failed to output build info: %v\n", err))
}

flag.Parse()
fmt.Print("Successfully started skychat.")
fmt.Println("Successfully started skychat.")

clientCh = make(chan string)
defer close(clientCh)
Expand All @@ -71,7 +69,7 @@ func main() {
if runtime.GOOS == "windows" {
ipcClient, err := ipc.StartClient(skyenv.SkychatName, nil)
if err != nil {
fmt.Printf("Error creating ipc server for skychat client: %v\n", err)
print(fmt.Sprintf("Error creating ipc server for skychat client: %v\n", err))
setAppError(appCl, err)
os.Exit(1)
}
Expand All @@ -84,32 +82,34 @@ func main() {
http.HandleFunc("/message", messageHandler(ctx))
http.HandleFunc("/sse", sseHandler)

fmt.Print("Serving HTTP on", *addr)
fmt.Println("Serving HTTP on", *addr)

setAppStatus(appCl, launcher.AppDetailedStatusRunning)

err := http.ListenAndServe(*addr, nil)
if err != nil {
fmt.Println(err)
print(err)
setAppError(appCl, err)
os.Exit(1)
}
}

func listenLoop() {
l, err := appCl.Listen(netType, port)
if err != nil {
fmt.Printf("Error listening network %v on port %d: %v\n", netType, port, err)
print(fmt.Sprintf("Error listening network %v on port %d: %v\n", netType, port, err))
setAppError(appCl, err)
return
}

for {
fmt.Print("Accepting skychat conn...")
fmt.Println("Accepting skychat conn...")
conn, err := l.Accept()
if err != nil {
fmt.Print("Failed to accept conn:", err)
print(fmt.Sprintf("Failed to accept conn: %v", err))
return
}
fmt.Print("Accepted skychat conn")
fmt.Println("Accepted skychat conn")

raddr := conn.RemoteAddr().(appnet.Addr)
connsMu.Lock()
Expand All @@ -127,7 +127,7 @@ func handleConn(conn net.Conn) {
buf := make([]byte, 32*1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Print("Failed to read packet:", err)
fmt.Println("Failed to read packet:", err)
raddr := conn.RemoteAddr().(appnet.Addr)
connsMu.Lock()
delete(conns, raddr.PubKey)
Expand All @@ -137,7 +137,7 @@ func handleConn(conn net.Conn) {

clientMsg, err := json.Marshal(map[string]string{"sender": raddr.PubKey.Hex(), "message": string(buf[:n])})
if err != nil {
fmt.Printf("Failed to marshal json: %v", err)
print(fmt.Sprintf("Failed to marshal json: %v\n", err))
}
select {
case clientCh <- string(clientMsg):
Expand Down Expand Up @@ -255,12 +255,12 @@ func handleIPCSignal(client *ipc.Client) {

func setAppStatus(appCl *app.Client, status launcher.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
fmt.Printf("Failed to set status %v: %v\n", status, err)
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func setAppError(appCl *app.Client, appErr error) {
if err := appCl.SetError(appErr.Error()); err != nil {
fmt.Printf("Failed to set error %v: %v\n", appErr, err)
print(fmt.Sprintf("Failed to set error %v: %v\n", appErr, err))
}
}
52 changes: 35 additions & 17 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"io"
"net"
"os"
"time"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/launcher"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand All @@ -28,9 +29,7 @@ const (
socksPort = routing.Port(3)
)

var log = logging.MustGetLogger("skysocks-client")

var r = netutil.NewRetrier(log, time.Second, netutil.DefaultMaxBackoff, 0, 1)
var r = netutil.NewRetrier(nil, time.Second, netutil.DefaultMaxBackoff, 0, 1)

func dialServer(ctx context.Context, appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
var conn net.Conn
Expand All @@ -54,53 +53,72 @@ func main() {
appCl := app.NewClient(nil)
defer appCl.Close()

skysocks.Log = log

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
print(fmt.Sprintf("Failed to output build info: %v\n", err))
}

var addr = flag.String("addr", skyenv.SkysocksClientAddr, "Client address to listen on")
var serverPK = flag.String("srv", "", "PubKey of the server to connect to")
flag.Parse()

if *serverPK == "" {
log.Warn("Empty server PubKey. Exiting")
err := errors.New("Empty server PubKey. Exiting")
print(err)
setAppErr(appCl, err)
return
}

pk := cipher.PubKey{}
if err := pk.UnmarshalText([]byte(*serverPK)); err != nil {
log.Fatal("Invalid server PubKey: ", err)
print(fmt.Sprintf("Invalid server PubKey: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

for {
conn, err := dialServer(ctx, appCl, pk)
if err != nil {
log.Fatalf("Failed to dial to a server: %v", err)
print(fmt.Sprintf("Failed to dial to a server: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

log.Printf("Connected to %v\n", pk)
fmt.Printf("Connected to %v\n", pk)

client, err := skysocks.NewClient(conn, appCl)
if err != nil {
log.Fatal("Failed to create a new client: ", err)
print(fmt.Sprintf("Failed to create a new client: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

log.Printf("Serving proxy client %v\n", *addr)
fmt.Printf("Serving proxy client %v\n", *addr)

if err := client.ListenAndServe(*addr); err != nil {
log.Errorf("Error serving proxy client: %v\n", err)
print(fmt.Sprintf("Error serving proxy client: %v\n", err))
}

// need to filter this out, cause usually client failure means app conn is already closed
if err := conn.Close(); err != nil && err != io.ErrClosedPipe {
log.Errorf("Error closing app conn: %v\n", err)
print(fmt.Sprintf("Error closing app conn: %v\n", err))
}

log.Println("Reconnecting to skysocks server")
fmt.Println("Reconnecting to skysocks server")
setAppStatus(appCl, launcher.AppDetailedStatusReconnecting)
}
}

func setAppErr(appCl *app.Client, err error) {
if appErr := appCl.SetError(err.Error()); appErr != nil {
fmt.Printf("Failed to set error %v: %v\n", err, appErr)
}
}

func setAppStatus(appCl *app.Client, status launcher.AppDetailedStatus) {
if err := appCl.SetDetailedStatus(string(status)); err != nil {
fmt.Printf("Failed to set status %v: %v\n", status, err)
}
}
8 changes: 2 additions & 6 deletions cmd/apps/skysocks/skysocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"

ipc "github.com/james-barrow/golang-ipc"
"github.com/sirupsen/logrus"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire/internal/skysocks"
Expand All @@ -26,22 +26,18 @@ const (
port routing.Port = 3
)

var log = logrus.New()

func main() {
appCl := app.NewClient(nil)
defer appCl.Close()

skysocks.Log = log

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
fmt.Printf("Failed to output build info: %v", err)
}

var passcode = flag.String("passcode", "", "Authorize user against this passcode")
flag.Parse()

srv, err := skysocks.NewServer(*passcode, appCl, log)
srv, err := skysocks.NewServer(*passcode, appCl)
if err != nil {
log.Fatal("Failed to create a new server: ", err)
}
Expand Down
Loading

0 comments on commit afed86e

Please sign in to comment.