调用log4go包

Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
dmy@lab 2015-10-16 23:21:38 +08:00
parent 70b9ed78ea
commit 74de3d715f
5 changed files with 37 additions and 26 deletions

Binary file not shown.

View File

@ -1,8 +1,8 @@
package log package log
import ( import (
log "github.com/alecthomas/log4go"
"fmt" "fmt"
log "github.com/alecthomas/log4go"
) )
var root log.Logger = make(log.Logger) var root log.Logger = make(log.Logger)
@ -54,6 +54,7 @@ type Logger interface {
Info(string, ...interface{}) Info(string, ...interface{})
Warn(string, ...interface{}) error Warn(string, ...interface{}) error
Error(string, ...interface{}) error Error(string, ...interface{}) error
Panic(string, ...interface{})
} }
type PrefixLogger struct { type PrefixLogger struct {
@ -91,6 +92,10 @@ func (pl *PrefixLogger) Error(arg0 string, args ...interface{}) error {
return pl.Logger.Error(pl.pfx(arg0), args...) return pl.Logger.Error(pl.pfx(arg0), args...)
} }
func (pl *PrefixLogger) Panic(arg0 string, args ...interface{}) {
panic(pl.Error(arg0, args))
}
func (pl *PrefixLogger) AddLogPrefix(prefix string) { func (pl *PrefixLogger) AddLogPrefix(prefix string) {
if len(pl.prefix) > 0 { if len(pl.prefix) > 0 {
pl.prefix += " " pl.prefix += " "
@ -118,4 +123,8 @@ func Warn(arg0 string, args ...interface{}) error {
func Error(arg0 string, args ...interface{}) error { func Error(arg0 string, args ...interface{}) error {
return root.Error(arg0, args...) return root.Error(arg0, args...)
} }
func Panic(arg0 string, args ...interface{}) error {
panic(root.Error(arg0, args...))
}

View File

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
// "github.com/TransX/log" "github.com/TransX/log"
) )
func Tunnel() { func Tunnel() {
@ -13,5 +13,6 @@ func Tunnel() {
func main() { func main() {
fmt.Println("Hello World!") fmt.Println("Hello World!")
log.LogTo("stdout", "FINEST")
Tunnel() Tunnel()
} }

View File

@ -1,10 +1,9 @@
package main package main
import ( import (
"github.com/TransX/log"
"github.com/TransX/tscipher" "github.com/TransX/tscipher"
"log"
"net" "net"
"os"
"testing" "testing"
"time" "time"
) )
@ -16,7 +15,7 @@ func server(t *testing.T) {
} }
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
log.Println("Test Server Incoming", conn.RemoteAddr().String()) log.Info("Test Server Incoming", conn.RemoteAddr().String())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -25,15 +24,15 @@ func server(t *testing.T) {
n, err := conn.Read(bytes) n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n]) decryped, _ := XOR.Decrypt(bytes[:n])
copy(bytes, decryped[:n]) copy(bytes, decryped[:n])
log.Println("Test Server Receive ", string(bytes[:n])) log.Info("Test Server Receive ", string(bytes[:n]))
encrypted, _ := XOR.Encrypt([]byte("OK")) encrypted, _ := XOR.Encrypt([]byte("OK"))
_, err = conn.Write(encrypted) _, err = conn.Write(encrypted)
log.Println("Test Server write") log.Info("Test Server write")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
conn.Close() conn.Close()
log.Println("Test Server closed") log.Info("Test Server closed")
} }
} }
@ -46,28 +45,27 @@ func client(t *testing.T) {
XOR := tscipher.NewXOR([]byte("fasdfasdf")) XOR := tscipher.NewXOR([]byte("fasdfasdf"))
encrypted, _ := XOR.Encrypt([]byte("Client")) encrypted, _ := XOR.Encrypt([]byte("Client"))
conn.Write(encrypted) conn.Write(encrypted)
log.Println("Test Client write") log.Info("Test Client write")
bytes := make([]byte, 32) bytes := make([]byte, 32)
n, err := conn.Read(bytes) n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n]) decryped, _ := XOR.Decrypt(bytes[:n])
copy(bytes, decryped[:n]) copy(bytes, decryped[:n])
log.Println("Test Client read") log.Info("Test Client read")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
log.Println("Test Client Receive ", string(bytes[:n])) log.Info("Test Client Receive ", string(bytes[:n]))
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
conn.Close() conn.Close()
log.Println("Test Client closed") log.Info("Test Client closed")
} }
} }
func TestTunnel(t *testing.T) { func TestTunnel(t *testing.T) {
file, _ := os.Create("log.txt") log.LogTo("stdout", "FINEST")
log.SetOutput(file)
// t.Log("Start testing.") // t.Log("Start testing.")
log.Println("Test Start testing.") log.Info("Test Start testing.")
go server(t) go server(t)
go client(t) go client(t)
trans := NewTransTCP() trans := NewTransTCP()

23
tcp.go
View File

@ -4,9 +4,10 @@ import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"github.com/TransX/log"
"github.com/TransX/tscipher" "github.com/TransX/tscipher"
"log"
"net" "net"
"os"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
"time" "time"
@ -68,18 +69,18 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) {
} }
nByte, err := tscipher.ReceiveData(revCarrier) nByte, err := tscipher.ReceiveData(revCarrier)
if err != nil { if err != nil {
log.Panicln("Read panic", id, err, src.RemoteAddr().String()) log.Panic("Read panic", id, err, src.RemoteAddr().String())
} }
log.Println("Reived ", nByte, "bytes:", id, string(cache[:nByte])) log.Info("Reived ", nByte, "bytes:", id, string(cache[:nByte]))
sendCarrier := &tscipher.Carrier{ sendCarrier := &tscipher.Carrier{
dest, dest,
tscipher.NewCipher("XOR"), tscipher.NewCipher("XOR"),
cache, //TODO:危险cache的容量容易被不小心修改 cache, //TODO:危险cache的容量容易被不小心修改
} }
_, err = tscipher.SendData(sendCarrier, nByte) _, err = tscipher.SendData(sendCarrier, nByte)
log.Println("Write") log.Info("Write")
if err != nil { if err != nil {
log.Panicln("Write panic", id, err, dest.RemoteAddr().String()) log.Panic("Write panic", id, err, dest.RemoteAddr().String())
} }
} }
@ -96,21 +97,23 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string) {
listener, err := this.createTCPListener("0.0.0.0", listenPort) listener, err := this.createTCPListener("0.0.0.0", listenPort)
if err != nil { if err != nil {
log.Fatalln("Failed to create listener.", err) log.Panic("Failed to create listener.", err)
os.Exit(0)
} }
for { for {
if listenerConn, err := listener.Accept(); err == nil { if listenerConn, err := listener.Accept(); err == nil {
log.Println("Incoming ", listenerConn.RemoteAddr().String()) log.Info("Incoming ", listenerConn.RemoteAddr().String())
//创建到目标的连接 //创建到目标的连接
destConn, err := this.createTCPClient(destIP, destPort) destConn, err := this.createTCPClient(destIP, destPort)
if err != nil { if err != nil {
log.Fatalln("Failed to connect to destination.", err) log.Panic("Failed to connect to destination.", err)
os.Exit(0)
} }
log.Println("Dial", destConn.RemoteAddr().String()) log.Info("Dial", destConn.RemoteAddr().String())
go this.tunnel(listenerConn, destConn, this.tunnelID()) go this.tunnel(listenerConn, destConn, this.tunnelID())
go this.tunnel(destConn, listenerConn, this.tunnelID()) go this.tunnel(destConn, listenerConn, this.tunnelID())
} else { } else {
log.Println("Failed to accept incoming connection.", err) log.Info("Failed to accept incoming connection.", err)
} }
} }
} }