调用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
import (
log "github.com/alecthomas/log4go"
"fmt"
log "github.com/alecthomas/log4go"
)
var root log.Logger = make(log.Logger)
@ -54,6 +54,7 @@ type Logger interface {
Info(string, ...interface{})
Warn(string, ...interface{}) error
Error(string, ...interface{}) error
Panic(string, ...interface{})
}
type PrefixLogger struct {
@ -91,6 +92,10 @@ func (pl *PrefixLogger) Error(arg0 string, args ...interface{}) error {
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) {
if len(pl.prefix) > 0 {
pl.prefix += " "
@ -119,3 +124,7 @@ func Warn(arg0 string, args ...interface{}) error {
func Error(arg0 string, args ...interface{}) error {
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 (
"fmt"
// "github.com/TransX/log"
"github.com/TransX/log"
)
func Tunnel() {
@ -13,5 +13,6 @@ func Tunnel() {
func main() {
fmt.Println("Hello World!")
log.LogTo("stdout", "FINEST")
Tunnel()
}

View File

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

23
tcp.go
View File

@ -4,9 +4,10 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
"github.com/TransX/log"
"github.com/TransX/tscipher"
"log"
"net"
"os"
"strconv"
"sync/atomic"
"time"
@ -68,18 +69,18 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) {
}
nByte, err := tscipher.ReceiveData(revCarrier)
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{
dest,
tscipher.NewCipher("XOR"),
cache, //TODO:危险cache的容量容易被不小心修改
}
_, err = tscipher.SendData(sendCarrier, nByte)
log.Println("Write")
log.Info("Write")
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)
if err != nil {
log.Fatalln("Failed to create listener.", err)
log.Panic("Failed to create listener.", err)
os.Exit(0)
}
for {
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)
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(destConn, listenerConn, this.tunnelID())
} else {
log.Println("Failed to accept incoming connection.", err)
log.Info("Failed to accept incoming connection.", err)
}
}
}