Skip to content

Commit

Permalink
Use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Sep 20, 2024
1 parent acdc622 commit d8b3e50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
52 changes: 30 additions & 22 deletions x/examples/test-connectivity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"log/slog"
"net"
"net/http"
"net/http/httptrace"
Expand All @@ -37,12 +36,10 @@ import (
"github.com/Jigsaw-Code/outline-sdk/x/config"
"github.com/Jigsaw-Code/outline-sdk/x/connectivity"
"github.com/Jigsaw-Code/outline-sdk/x/report"
"github.com/lmittmann/tint"
"golang.org/x/term"
)

var debugLog log.Logger = *log.New(io.Discard, "", 0)

// var errorLog log.Logger = *log.New(os.Stderr, "[ERROR] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)

type connectivityReport struct {
Test testReport `json:"test"`
DNSQueries []dnsReport `json:"dns_queries,omitempty"`
Expand Down Expand Up @@ -157,28 +154,34 @@ func main() {

flag.Parse()

logLevel := slog.LevelInfo
if *verboseFlag {
logLevel = slog.LevelDebug
}
slog.SetDefault(slog.New(tint.NewHandler(
os.Stderr,
&tint.Options{NoColor: !term.IsTerminal(int(os.Stderr.Fd())), Level: logLevel},
)))

// Perform custom range validation for sampling rate
if *reportSuccessFlag < 0.0 || *reportSuccessFlag > 1.0 {
fmt.Println("Error: report-success-rate must be between 0 and 1.")
slog.Error("Error: report-success-rate must be between 0 and 1.", "report-success-rate", *reportSuccessFlag)
flag.Usage()
return
os.Exit(1)
}

if *reportFailureFlag < 0.0 || *reportFailureFlag > 1.0 {
fmt.Println("Error: report-failure-rate must be between 0 and 1.")
slog.Error("Error: report-failure-rate must be between 0 and 1.", "report-failure-rate", *reportFailureFlag)
flag.Usage()
return
}

if *verboseFlag {
debugLog = *log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
os.Exit(1)
}

var reportCollector report.Collector
if *reportToFlag != "" {
collectorURL, err := url.Parse(*reportToFlag)
if err != nil {
debugLog.Printf("Failed to parse collector URL: %v", err)
slog.Error("Failed to parse collector URL", "url", err)
os.Exit(1)
}
remoteCollector := &report.RemoteCollector{
CollectorURL: collectorURL,
Expand Down Expand Up @@ -294,32 +297,37 @@ func main() {
case "tcp":
streamDialer, err := configToDialer.NewStreamDialer(*transportFlag)
if err != nil {
log.Fatalf("Failed to create StreamDialer: %v", err)
slog.Error("Failed to create StreamDialer", "error", err)
os.Exit(1)
}
resolver = dns.NewTCPResolver(streamDialer, resolverAddress)

case "udp":
packetDialer, err := configToDialer.NewPacketDialer(*transportFlag)
if err != nil {
log.Fatalf("Failed to create PacketDialer: %v", err)
slog.Error("Failed to create PacketDialer", "error", err)
os.Exit(1)
}
resolver = dns.NewUDPResolver(packetDialer, resolverAddress)
default:
log.Fatalf(`Invalid proto %v. Must be "tcp" or "udp"`, proto)
slog.Error(`Invalid proto. Must be "tcp" or "udp"`, "proto", proto)
os.Exit(1)
}
startTime := time.Now()
result, err := connectivity.TestConnectivityWithResolver(context.Background(), resolver, *domainFlag)
if err != nil {
log.Fatalf("Connectivity test failed to run: %v", err)
slog.Error("Connectivity test failed to run", "error", err)
os.Exit(1)
}
testDuration := time.Since(startTime)
if result == nil {
success = true
}
debugLog.Printf("Test %v %v result: %v", proto, resolverAddress, result)
slog.Debug("Test done", "proto", proto, "resolver", resolverAddress, "result", result)
sanitizedConfig, err := config.SanitizeConfig(*transportFlag)
if err != nil {
log.Fatalf("Failed to sanitize config: %v", err)
slog.Error("Failed to sanitize config", "error", err)
os.Exit(1)
}
var r report.Report = connectivityReport{
Test: testReport{
Expand All @@ -337,7 +345,7 @@ func main() {
if reportCollector != nil {
err = reportCollector.Collect(context.Background(), r)
if err != nil {
debugLog.Printf("Failed to collect report: %v\n", err)
slog.Warn("Failed to collect report", "error", err)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions x/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ require (
// Use github.com/Psiphon-Labs/psiphon-tunnel-core@staging-client as per
// https://github.com/Psiphon-Labs/psiphon-tunnel-core/?tab=readme-ov-file#using-psiphon-with-go-modules
github.com/Psiphon-Labs/psiphon-tunnel-core v1.0.11-0.20240619172145-03cade11f647
github.com/lmittmann/tint v1.0.5
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/stretchr/testify v1.9.0
github.com/vishvananda/netlink v1.1.0
golang.org/x/mobile v0.0.0-20240520174638-fa72addaaa1b
golang.org/x/net v0.25.0
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions x/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/marusama/semaphore v0.0.0-20171214154724-565ffd8e868a h1:6SRny9FLB1eWasPyDUqBQnMi9NhXU01XIlB0ao89YoI=
github.com/marusama/semaphore v0.0.0-20171214154724-565ffd8e868a/go.mod h1:TmeOqAKoDinfPfSohs14CO3VcEf7o+Bem6JiNe05yrQ=
github.com/mdlayher/netlink v1.4.2-0.20210930205308-a81a8c23d40a h1:yk5OmRew64lWdeNanQ3l0hDgUt1E8MfipPhh/GO9Tuw=
Expand Down

0 comments on commit d8b3e50

Please sign in to comment.