Skip to content

Commit

Permalink
Add simple logging facility.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 12, 2012
1 parent 511acfa commit a37ff3f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"server_port":8388,
"local_port":1080,
"password":"barfoo!",
"timeout":60
"timeout":60,
"debug":true
}
2 changes: 2 additions & 0 deletions shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
LocalPort int `json:"local_port"`
Password string `json:"password"`
PortPassword map[string]string `json:"port_password"`
Debug bool `json:"debug"`
}

func ParseConfig(path string) *Config {
Expand All @@ -36,5 +37,6 @@ func ParseConfig(path string) *Config {
if err = json.Unmarshal(data, &config); err != nil {
log.Fatal("can not parse config:", err)
}
Debug = DebugLog(config.Debug)
return &config
}
3 changes: 3 additions & 0 deletions shadowsocks/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func TestParseConfig1Password(t *testing.T) {
if config.Password != "barfoo!" {
t.Error("wrong password from config")
}
if config.Debug != true {
t.Error("debug option wrong")
}
}

func TestParseConfigMultiPassword(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions shadowsocks/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shadowsocks

import (
"log"
"os"
)

type DebugLog bool

var Debug DebugLog

var dbgLog = log.New(os.Stdout, "[DEBUG] ", log.Ltime)

func (d DebugLog) Printf(format string, args ...interface{}) {
if d {
dbgLog.Printf(format, args...)
}
}

func (d DebugLog) Println(args ...interface{}) {
if d {
dbgLog.Println(args...)
}
}
3 changes: 2 additions & 1 deletion shadowsocks/testdata/config-one-passwd.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"server_port":8388,
"local_port":1081,
"password":"barfoo!",
"timeout":60
"timeout":60,
"debug": true
}

0 comments on commit a37ff3f

Please sign in to comment.