[tor-commits] [websocket/master] Use the standard log package.
dcf at torproject.org
dcf at torproject.org
Sat Dec 19 08:34:31 UTC 2015
commit 086d3bcf6ffe5faa1889b89a4ceea9ac58b948e1
Author: David Fifield <david at bamsoftware.com>
Date: Fri Dec 18 22:59:56 2015 -0800
Use the standard log package.
This changes "2015-12-19" to "2015/12/19" and uses UTC instead of local
time.
---
websocket-client/websocket-client.go | 35 ++++++++++++---------------------
websocket-server/websocket-server.go | 36 +++++++++++++---------------------
2 files changed, 26 insertions(+), 45 deletions(-)
diff --git a/websocket-client/websocket-client.go b/websocket-client/websocket-client.go
index f65a9c9..859d98e 100644
--- a/websocket-client/websocket-client.go
+++ b/websocket-client/websocket-client.go
@@ -11,13 +11,13 @@ import (
"flag"
"fmt"
"io"
+ "log"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
- "time"
)
import "git.torproject.org/pluggable-transports/goptlib.git"
@@ -27,14 +27,10 @@ var ptInfo pt.ClientInfo
const ptMethodName = "websocket"
const bufSiz = 1500
-var logFile = os.Stderr
-
// When a connection handler starts, +1 is written to this channel; when it
// ends, -1 is written.
var handlerChan = make(chan int)
-var logMutex sync.Mutex
-
func usage() {
fmt.Printf("Usage: %s [OPTIONS]\n", os.Args[0])
fmt.Printf("WebSocket client pluggable transport for Tor.\n")
@@ -45,14 +41,6 @@ func usage() {
fmt.Printf(" --socks ADDR listen for SOCKS on ADDR.\n")
}
-func log(format string, v ...interface{}) {
- dateStr := time.Now().Format("2006-01-02 15:04:05")
- logMutex.Lock()
- defer logMutex.Unlock()
- msg := fmt.Sprintf(format, v...)
- fmt.Fprintf(logFile, "%s %s\n", dateStr, msg)
-}
-
func proxy(local *net.TCPConn, ws *websocket.Conn) {
var wg sync.WaitGroup
@@ -77,7 +65,7 @@ func proxy(local *net.TCPConn, ws *websocket.Conn) {
}
}
if err != nil && err != io.EOF {
- log("%s", err)
+ log.Printf("%s", err)
}
local.CloseRead()
ws.Close()
@@ -106,7 +94,7 @@ func proxy(local *net.TCPConn, ws *websocket.Conn) {
}
}
if err != nil && err != io.EOF {
- log("%s", err)
+ log.Printf("%s", err)
}
local.CloseWrite()
ws.Close()
@@ -127,7 +115,7 @@ func handleConnection(conn *pt.SocksConn) error {
var ws *websocket.Conn
- log("SOCKS request for %s", conn.Req.Target)
+ log.Printf("SOCKS request for %s", conn.Req.Target)
destAddr, err := net.ResolveTCPAddr("tcp", conn.Req.Target)
if err != nil {
conn.Reject()
@@ -139,7 +127,7 @@ func handleConnection(conn *pt.SocksConn) error {
err = conn.Reject()
return err
}
- log("WebSocket connection to %s", ws.Config().Location.String())
+ log.Printf("WebSocket connection to %s", ws.Config().Location.String())
defer ws.Close()
err = conn.Grant(destAddr)
if err != nil {
@@ -164,7 +152,7 @@ func socksAcceptLoop(ln *pt.SocksListener) error {
go func() {
err := handleConnection(socks)
if err != nil {
- log("SOCKS from %s: %s", socks.RemoteAddr(), err)
+ log.Printf("SOCKS from %s: %s", socks.RemoteAddr(), err)
}
}()
}
@@ -179,7 +167,7 @@ func startListener(addrStr string) (*pt.SocksListener, error) {
go func() {
err := socksAcceptLoop(ln)
if err != nil {
- log("accept: %s", err)
+ log.Printf("accept: %s", err)
}
}()
return ln, nil
@@ -201,13 +189,14 @@ func main() {
fmt.Fprintf(os.Stderr, "Can't open log file %q: %s.\n", logFilename, err.Error())
os.Exit(1)
}
- logFile = f
+ log.SetOutput(f)
}
- log("starting")
+ log.SetFlags(log.LstdFlags | log.LUTC)
+ log.Printf("starting")
ptInfo, err = pt.ClientSetup(nil)
if err != nil {
- log("error in setup: %s", err)
+ log.Printf("error in setup: %s", err)
os.Exit(1)
}
@@ -221,7 +210,7 @@ func main() {
break
}
pt.Cmethod(ptMethodName, ln.Version(), ln.Addr())
- log("listening on %s", ln.Addr().String())
+ log.Printf("listening on %s", ln.Addr().String())
listeners = append(listeners, ln)
default:
pt.CmethodError(methodName, "no such method")
diff --git a/websocket-server/websocket-server.go b/websocket-server/websocket-server.go
index fbfffd8..0d065cc 100644
--- a/websocket-server/websocket-server.go
+++ b/websocket-server/websocket-server.go
@@ -11,6 +11,7 @@ import (
"flag"
"fmt"
"io"
+ "log"
"net"
"net/http"
"os"
@@ -48,16 +49,6 @@ func usage() {
fmt.Printf(" --port PORT listen on PORT (overrides Tor's requested port).\n")
}
-var logMutex sync.Mutex
-
-func log(format string, v ...interface{}) {
- dateStr := time.Now().Format("2006-01-02 15:04:05")
- logMutex.Lock()
- defer logMutex.Unlock()
- msg := fmt.Sprintf(format, v...)
- fmt.Fprintf(logFile, "%s %s\n", dateStr, msg)
-}
-
// An abstraction that makes an underlying WebSocket connection look like an
// io.ReadWriteCloser. It internally takes care of things like base64 encoding
// and decoding.
@@ -146,7 +137,7 @@ func proxy(local *net.TCPConn, conn *webSocketConn) {
go func() {
_, err := io.Copy(conn, local)
if err != nil {
- log("error copying ORPort to WebSocket")
+ log.Printf("error copying ORPort to WebSocket")
}
local.CloseRead()
conn.Close()
@@ -155,7 +146,7 @@ func proxy(local *net.TCPConn, conn *webSocketConn) {
go func() {
_, err := io.Copy(local, conn)
if err != nil {
- log("error copying WebSocket to ORPort")
+ log.Printf("error copying WebSocket to ORPort")
}
local.CloseWrite()
conn.Close()
@@ -178,7 +169,7 @@ func webSocketHandler(ws *websocket.WebSocket) {
or, err := pt.DialOr(&ptInfo, ws.Conn.RemoteAddr().String(), ptMethodName)
if err != nil {
- log("Failed to connect to ORPort: " + err.Error())
+ log.Printf("Failed to connect to ORPort: " + err.Error())
return
}
defer or.Close()
@@ -202,7 +193,7 @@ func startListener(addr *net.TCPAddr) (*net.TCPListener, error) {
}
err = s.Serve(ln)
if err != nil {
- log("http.Serve: " + err.Error())
+ log.Printf("http.Serve: " + err.Error())
}
}()
return ln, nil
@@ -223,14 +214,15 @@ func main() {
fmt.Fprintf(os.Stderr, "Can't open log file %q: %s.\n", logFilename, err.Error())
os.Exit(1)
}
- logFile = f
+ log.SetOutput(f)
}
- log("starting")
+ log.SetFlags(log.LstdFlags | log.LUTC)
+ log.Printf("starting")
var err error
ptInfo, err = pt.ServerSetup(nil)
if err != nil {
- log("error in setup: %s", err)
+ log.Printf("error in setup: %s", err)
os.Exit(1)
}
@@ -251,7 +243,7 @@ func main() {
break
}
pt.Smethod(bindaddr.MethodName, ln.Addr())
- log("listening on %s", ln.Addr().String())
+ log.Printf("listening on %s", ln.Addr().String())
listeners = append(listeners, ln)
default:
pt.SmethodError(bindaddr.MethodName, "no such method")
@@ -273,13 +265,13 @@ func main() {
case sig = <-sigChan:
}
}
- log("Got first signal %q with %d running handlers.", sig, numHandlers)
+ log.Printf("Got first signal %q with %d running handlers.", sig, numHandlers)
for _, ln := range listeners {
ln.Close()
}
if sig == syscall.SIGTERM {
- log("Caught signal %q, exiting.", sig)
+ log.Printf("Caught signal %q, exiting.", sig)
return
}
@@ -289,11 +281,11 @@ func main() {
select {
case n := <-handlerChan:
numHandlers += n
- log("%d remaining handlers.", numHandlers)
+ log.Printf("%d remaining handlers.", numHandlers)
case sig = <-sigChan:
}
}
if sig != nil {
- log("Got second signal %q with %d running handlers.", sig, numHandlers)
+ log.Printf("Got second signal %q with %d running handlers.", sig, numHandlers)
}
}
More information about the tor-commits
mailing list