Skip to content

Commit

Permalink
add customized logger
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Mar 22, 2020
1 parent d61907c commit 519a721
Show file tree
Hide file tree
Showing 10 changed files with 709 additions and 8 deletions.
3 changes: 2 additions & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
//_ "github.com/mattn/go-sqlite3"
)

type Runnable interface {
Expand Down Expand Up @@ -57,5 +57,6 @@ func ConnectDatabase(driverName, username, password, ip string, port int, dbName
}

func ConnectSQLite(dbName string) (*sql.DB, error) {
//for debug only
return sql.Open("sqlite3", dbName)
}
4 changes: 2 additions & 2 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ type GlobalConfig struct {
RemotePort uint16 `json:"remote_port"`

Hash map[string]string
Passwords []string `json:"password"`
LogLevel int
Passwords []string `json:"password"`
LogLevel int `json: "log_level"`
TLS TLSConfig `json:"ssl"`
TCP TCPConfig `json:"tcp"`
MySQL MySQLConfig `json:"mysql"`
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.13
require (
github.com/LiamHaworth/go-tproxy v0.0.0-20190726054950-ef7efd7f24ed
github.com/go-sql-driver/mysql v1.5.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/pkg/errors v0.9.1 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/smartystreets/goconvey v1.6.4
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a
github.com/withmandala/go-log v0.1.0
github.com/xtaci/smux v2.0.1+incompatible
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20200320220750-118fecf932d8
golang.org/x/sys v0.0.0-20200321134203-328b4cd54aae
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
Expand Down
42 changes: 42 additions & 0 deletions log/buffer/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Buffer-like byte slice
// Copyright (c) 2017 Fadhli Dzil Ikram

package buffer

// Buffer type wrap up byte slice built-in type
type Buffer []byte

// Reset buffer position to start
func (b *Buffer) Reset() {
*b = Buffer([]byte(*b)[:0])
}

// Append byte slice to buffer
func (b *Buffer) Append(data []byte) {
*b = append(*b, data...)
}

// AppendByte to buffer
func (b *Buffer) AppendByte(data byte) {
*b = append(*b, data)
}

// AppendInt to buffer
func (b *Buffer) AppendInt(val int, width int) {
var repr [8]byte
reprCount := len(repr) - 1
for val >= 10 || width > 1 {
reminder := val / 10
repr[reprCount] = byte('0' + val - reminder*10)
val = reminder
reprCount--
width--
}
repr[reprCount] = byte('0' + val)
b.Append(repr[reprCount:])
}

// Bytes return underlying slice data
func (b Buffer) Bytes() []byte {
return []byte(b)
}
76 changes: 76 additions & 0 deletions log/buffer/buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Buffer-like byte slice
// Copyright (c) 2017 Fadhli Dzil Ikram
//
// Test file for buffer

package buffer

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestBufferAllocation(t *testing.T) {
Convey("Given new unallocated buffer", t, func() {
var buf Buffer

Convey("When appended with data", func() {
data := []byte("Hello")
buf.Append(data)

Convey("It should have same content as the original data", func() {
So(buf.Bytes(), ShouldResemble, data)
})
})

Convey("When appended with single byte", func() {
data := byte('H')
buf.AppendByte(data)

Convey("It should have 1 byte length", func() {
So(len(buf), ShouldEqual, 1)
})

Convey("It should have same content", func() {
So(buf.Bytes()[0], ShouldEqual, data)
})
})

Convey("When appended with integer", func() {
data := 12345
repr := []byte("012345")
buf.AppendInt(data, len(repr))

Convey("Should have same content with the integer representation", func() {
So(buf.Bytes(), ShouldResemble, repr)
})
})
})
}

func TestBufferReset(t *testing.T) {
Convey("Given allocated buffer", t, func() {
var buf Buffer
data := []byte("Hello")
replace := []byte("World")
buf.Append(data)

Convey("When buffer reset", func() {
buf.Reset()

Convey("It should have zero length", func() {
So(len(buf), ShouldEqual, 0)
})
})

Convey("When buffer reset and replaced with another append", func() {
buf.Reset()
buf.Append(replace)

Convey("It should have same content with the replaced data", func() {
So(buf.Bytes(), ShouldResemble, replace)
})
})
})
}
104 changes: 104 additions & 0 deletions log/colorful/colorful.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// The color engine for the go-log library
// Copyright (c) 2017 Fadhli Dzil Ikram

package colorful

import "github.com/withmandala/go-log/buffer"

// ColorBuffer add color option to buffer append
type ColorBuffer struct {
buffer.Buffer
}

// color pallete map
var (
colorOff = []byte("\033[0m")
colorRed = []byte("\033[0;31m")
colorGreen = []byte("\033[0;32m")
colorOrange = []byte("\033[0;33m")
colorBlue = []byte("\033[0;34m")
colorPurple = []byte("\033[0;35m")
colorCyan = []byte("\033[0;36m")
colorGray = []byte("\033[0;37m")
)

// Off apply no color to the data
func (cb *ColorBuffer) Off() {
cb.Append(colorOff)
}

// Red apply red color to the data
func (cb *ColorBuffer) Red() {
cb.Append(colorRed)
}

// Green apply green color to the data
func (cb *ColorBuffer) Green() {
cb.Append(colorGreen)
}

// Orange apply orange color to the data
func (cb *ColorBuffer) Orange() {
cb.Append(colorOrange)
}

// Blue apply blue color to the data
func (cb *ColorBuffer) Blue() {
cb.Append(colorBlue)
}

// Purple apply purple color to the data
func (cb *ColorBuffer) Purple() {
cb.Append(colorPurple)
}

// Cyan apply cyan color to the data
func (cb *ColorBuffer) Cyan() {
cb.Append(colorCyan)
}

// Gray apply gray color to the data
func (cb *ColorBuffer) Gray() {
cb.Append(colorGray)
}

// mixer mix the color on and off byte with the actual data
func mixer(data []byte, color []byte) []byte {
var result []byte
return append(append(append(result, color...), data...), colorOff...)
}

// Red apply red color to the data
func Red(data []byte) []byte {
return mixer(data, colorRed)
}

// Green apply green color to the data
func Green(data []byte) []byte {
return mixer(data, colorGreen)
}

// Orange apply orange color to the data
func Orange(data []byte) []byte {
return mixer(data, colorOrange)
}

// Blue apply blue color to the data
func Blue(data []byte) []byte {
return mixer(data, colorBlue)
}

// Purple apply purple color to the data
func Purple(data []byte) []byte {
return mixer(data, colorPurple)
}

// Cyan apply cyan color to the data
func Cyan(data []byte) []byte {
return mixer(data, colorCyan)
}

// Gray apply gray color to the data
func Gray(data []byte) []byte {
return mixer(data, colorGray)
}
117 changes: 117 additions & 0 deletions log/colorful/colorful_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// The color engine for the go-log library
// Copyright (c) 2017 Fadhli Dzil Ikram
//
// Test file

package colorful

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/withmandala/go-log/buffer"
)

func TestColorBuffer(t *testing.T) {
Convey("Given empty color buffer and test data", t, func() {
var cb ColorBuffer
var result buffer.Buffer

// Add color to the result buffer
result.Append(colorRed)
result.Append(colorGreen)
result.Append(colorOrange)
result.Append(colorBlue)
result.Append(colorPurple)
result.Append(colorCyan)
result.Append(colorGray)
result.Append(colorOff)

Convey("When appended with color", func() {
cb.Red()
cb.Green()
cb.Orange()
cb.Blue()
cb.Purple()
cb.Cyan()
cb.Gray()
cb.Off()

Convey("It should have same content with the test data", func() {
So(result.Bytes(), ShouldResemble, cb.Bytes())
})
})
})
}

func TestColorMixer(t *testing.T) {
Convey("Given mixer test result data", t, func() {
var (
data = []byte("Hello")
resultRed buffer.Buffer
resultGreen buffer.Buffer
resultOrange buffer.Buffer
resultBlue buffer.Buffer
resultPurple buffer.Buffer
resultCyan buffer.Buffer
resultGray buffer.Buffer
)

// Add result to buffer
resultRed.Append(colorRed)
resultRed.Append(data)
resultRed.Append(colorOff)

resultGreen.Append(colorGreen)
resultGreen.Append(data)
resultGreen.Append(colorOff)

resultOrange.Append(colorOrange)
resultOrange.Append(data)
resultOrange.Append(colorOff)

resultBlue.Append(colorBlue)
resultBlue.Append(data)
resultBlue.Append(colorOff)

resultPurple.Append(colorPurple)
resultPurple.Append(data)
resultPurple.Append(colorOff)

resultCyan.Append(colorCyan)
resultCyan.Append(data)
resultCyan.Append(colorOff)

resultGray.Append(colorGray)
resultGray.Append(data)
resultGray.Append(colorOff)

Convey("It should have same result when data appended with Red color", func() {
So(Red(data), ShouldResemble, resultRed.Bytes())
})

Convey("It should have same result when data appended with Green color", func() {
So(Green(data), ShouldResemble, resultGreen.Bytes())
})

Convey("It should have same result when data appended with Orange color", func() {
So(Orange(data), ShouldResemble, resultOrange.Bytes())
})

Convey("It should have same result when data appended with Blue color", func() {
So(Blue(data), ShouldResemble, resultBlue.Bytes())
})

Convey("It should have same result when data appended with Purple color", func() {
So(Purple(data), ShouldResemble, resultPurple.Bytes())
})

Convey("It should have same result when data appended with Cyan color", func() {
So(Cyan(data), ShouldResemble, resultCyan.Bytes())
})

Convey("It should have same result when data appended with Gray color", func() {
So(Gray(data), ShouldResemble, resultGray.Bytes())
})
})
}
Loading

0 comments on commit 519a721

Please sign in to comment.