Skip to content

Commit 5f88590

Browse files
author
Yusuke Kato
committed
remove mutex and add lockfree algorithm (#7)
1 parent 693e6d8 commit 5f88590

File tree

7 files changed

+768
-379
lines changed

7 files changed

+768
-379
lines changed

Makefile

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GO_VERSION:=$(shell go version)
22

3-
.PHONY: bench profile
3+
.PHONY: bench profile clean test
44

55
all: install
66

@@ -10,15 +10,26 @@ bench:
1010
profile:
1111
mkdir bench
1212
go test -count=10 -run=NONE -bench . -benchmem -o pprof/test.bin -cpuprofile pprof/cpu.out -memprofile pprof/mem.out
13-
go tool pprof --svg pprof/test.bin pprof/mem.out > mem.svg
14-
go tool pprof --svg pprof/test.bin pprof/cpu.out > cpu.svg
13+
go tool pprof --svg pprof/test.bin pprof/mem.out > bench/mem.svg
14+
go tool pprof --svg pprof/test.bin pprof/cpu.out > bench/cpu.svg
1515
rm -rf pprof
16+
mkdir pprof
1617
go test -count=10 -run=NONE -bench=BenchmarkGlg -benchmem -o pprof/test.bin -cpuprofile pprof/cpu-glg.out -memprofile pprof/mem-glg.out
17-
go tool pprof --svg pprof/test.bin pprof/cpu-glg.out > cpu-glg.svg
18-
go tool pprof --svg pprof/test.bin pprof/mem-glg.out > mem-glg.svg
18+
go tool pprof --svg pprof/test.bin pprof/cpu-glg.out > bench/cpu-glg.svg
19+
go tool pprof --svg pprof/test.bin pprof/mem-glg.out > bench/mem-glg.svg
1920
rm -rf pprof
21+
mkdir pprof
2022
go test -count=10 -run=NONE -bench=BenchmarkDefaultLog -benchmem -o pprof/test.bin -cpuprofile pprof/cpu-def.out -memprofile pprof/mem-def.out
21-
go tool pprof --svg pprof/test.bin pprof/mem-def.out > mem-def.svg
22-
go tool pprof --svg pprof/test.bin pprof/cpu-def.out > cpu-def.svg
23+
go tool pprof --svg pprof/test.bin pprof/mem-def.out > bench/mem-def.svg
24+
go tool pprof --svg pprof/test.bin pprof/cpu-def.out > bench/cpu-def.svg
2325
rm -rf pprof
24-
mv ./*.svg bench/
26+
27+
clean:
28+
rm -rf bench
29+
rm -rf pprof
30+
rm -rf ./*.svg
31+
rm -rf ./*.log
32+
33+
test:
34+
go test --race ./...
35+

circle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ machine:
66
PATH: "${GOPATH}/bin:${PATH}"
77
BUILD_PATH: "${GOPATH}/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
88
GO15VENDOREXPERIMENT: 1
9-
GODIST: "go1.8.3.linux-amd64.tar.gz"
9+
GODIST: "go1.10.2.linux-amd64.tar.gz"
1010
CODECOV_TOKEN: "1227405d-f3e6-47e6-8ac5-5cd16ba2d1f7"
1111
post:
1212
- mkdir -p downloads

example/example

-5.88 MB
Binary file not shown.

example/main.go

+45-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"net/http"
5+
"time"
56

67
"github.com/kpango/glg"
78
)
@@ -12,7 +13,7 @@ type NetWorkLogger struct{}
1213
func (n NetWorkLogger) Write(b []byte) (int, error) {
1314
// http.Post("localhost:8080/log", "", bytes.NewReader(b))
1415
http.Get("http://127.0.0.1:8080/log")
15-
// glg.Success("Requested")
16+
glg.Success("Requested")
1617
return 1, nil
1718
}
1819

@@ -22,12 +23,12 @@ func main() {
2223
// var customWriter io.Writer
2324
infolog := glg.FileWriter("/tmp/info.log", 0666)
2425

25-
customLevel := "FINE"
26-
customErrLevel := "CRIT"
26+
customTag := "FINE"
27+
customErrTag := "CRIT"
2728

28-
// errlog := glg.FileWriter("/tmp/error.log", 0666)
29+
errlog := glg.FileWriter("/tmp/error.log", 0666)
2930
defer infolog.Close()
30-
// defer errlog.Close()
31+
defer errlog.Close()
3132
glg.Get().
3233
SetMode(glg.BOTH). // default is STD
3334
// DisableColor().
@@ -45,11 +46,12 @@ func main() {
4546
// SetLevelWriter(glg.INFO, customWriter).
4647
// SetLevelWriter(glg.WARN, customWriter).
4748
// SetLevelWriter(glg.ERR, customWriter).
48-
AddLevelWriter(glg.INFO, infolog). // add info log file destination
49-
// AddLevelWriter(glg.ERR, errlog). // add error log file destination
50-
AddStdLevel(customLevel, glg.STD, false). //user custom log level
51-
AddErrLevel(customErrLevel, glg.STD, true). // user custom error log level
52-
SetLevelColor(customErrLevel, glg.Red) // set color output to user custom level
49+
AddLevelWriter(glg.INFO, infolog). // add info log file destination
50+
AddLevelWriter(glg.ERR, errlog). // add error log file destination
51+
AddStdLevel(customTag, glg.STD, false). //user custom log level
52+
AddErrLevel(customErrTag, glg.STD, true). // user custom error log level
53+
SetLevelColor(glg.TagStringToLevel(customTag), glg.Cyan). // set color output to user custom level
54+
SetLevelColor(glg.TagStringToLevel(customErrTag), glg.Red) // set color output to user custom level
5355

5456
glg.Info("info")
5557
glg.Infof("%s : %s", "info", "formatted")
@@ -68,15 +70,41 @@ func main() {
6870
glg.Print("Print")
6971
glg.Println("Println")
7072
glg.Printf("%s : %s", "printf", "formatted")
71-
glg.CustomLog(customLevel, "custom logging")
72-
glg.CustomLog(customErrLevel, "custom error logging")
73+
glg.CustomLog(customTag, "custom logging")
74+
glg.CustomLog(customErrTag, "custom error logging")
7375

74-
for i := 0; i < 100; i++ {
75-
glg.Error("error")
76-
glg.CustomLog(customLevel, "custom logging")
77-
}
76+
go func() {
77+
time.Sleep(time.Second * 5)
78+
for i := 0; i < 100; i++ {
79+
glg.Info("info")
80+
}
81+
}()
7882

79-
// glg.Get().AddLevelWriter(glg.DEBG, NetWorkLogger{}) // add info log file destination
83+
go func() {
84+
time.Sleep(time.Second * 5)
85+
for i := 0; i < 100; i++ {
86+
glg.Debug("debug")
87+
time.Sleep(time.Millisecond * 100)
88+
}
89+
}()
90+
91+
go func() {
92+
time.Sleep(time.Second * 5)
93+
for i := 0; i < 100; i++ {
94+
glg.Warn("warn")
95+
}
96+
}()
97+
98+
go func() {
99+
time.Sleep(time.Second * 5)
100+
for i := 0; i < 100; i++ {
101+
glg.Error("error")
102+
time.Sleep(time.Millisecond * 100)
103+
glg.CustomLog(customTag, "custom logging")
104+
}
105+
}()
106+
107+
glg.Get().AddLevelWriter(glg.DEBG, NetWorkLogger{}) // add info log file destination
80108

81109
http.Handle("/glg", glg.HTTPLoggerFunc("glg sample", func(w http.ResponseWriter, r *http.Request) {
82110
glg.Info("glg HTTP server logger sample")

0 commit comments

Comments
 (0)