9076ce7d by 1fei

Update conn.go

使用json直接初始化相关变量,代码更简单。如果把协议的首字母改大写会更简单,但不好改谢大的协议。
1 parent 0269a669
...@@ -10,45 +10,29 @@ import ( ...@@ -10,45 +10,29 @@ import (
10 type ConnWriter struct { 10 type ConnWriter struct {
11 lg *log.Logger 11 lg *log.Logger
12 innerWriter io.WriteCloser 12 innerWriter io.WriteCloser
13 reconnectOnMsg bool 13 ReconnectOnMsg bool `json:"reconnectOnMsg"`
14 reconnect bool 14 Reconnect bool `json:"reconnect"`
15 net string 15 Net string `json:"net"`
16 addr string 16 Addr string `json:"addr"`
17 level int 17 Level int `json:"level"`
18 } 18 }
19 19
20 func NewConn() LoggerInterface { 20 func NewConn() LoggerInterface {
21 conn := new(ConnWriter) 21 conn := new(ConnWriter)
22 conn.level = LevelTrace 22 conn.Level = LevelTrace
23 return conn 23 return conn
24 } 24 }
25 25
26 func (c *ConnWriter) Init(jsonconfig string) error { 26 func (c *ConnWriter) Init(jsonconfig string) error {
27 var m map[string]interface{} 27 err := json.Unmarshal([]byte(jsonconfig), c)
28 err := json.Unmarshal([]byte(jsonconfig), &m)
29 if err != nil { 28 if err != nil {
30 return err 29 return err
31 } 30 }
32 if rom, ok := m["reconnectOnMsg"]; ok {
33 c.reconnectOnMsg = rom.(bool)
34 }
35 if rc, ok := m["reconnect"]; ok {
36 c.reconnect = rc.(bool)
37 }
38 if nt, ok := m["net"]; ok {
39 c.net = nt.(string)
40 }
41 if addr, ok := m["addr"]; ok {
42 c.addr = addr.(string)
43 }
44 if lv, ok := m["level"]; ok {
45 c.level = int(lv.(float64))
46 }
47 return nil 31 return nil
48 } 32 }
49 33
50 func (c *ConnWriter) WriteMsg(msg string, level int) error { 34 func (c *ConnWriter) WriteMsg(msg string, level int) error {
51 if level < c.level { 35 if level < c.Level {
52 return nil 36 return nil
53 } 37 }
54 if c.neddedConnectOnMsg() { 38 if c.neddedConnectOnMsg() {
...@@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error { ...@@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
58 } 42 }
59 } 43 }
60 44
61 if c.reconnectOnMsg { 45 if c.ReconnectOnMsg {
62 defer c.innerWriter.Close() 46 defer c.innerWriter.Close()
63 } 47 }
64 c.lg.Println(msg) 48 c.lg.Println(msg)
...@@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error { ...@@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error {
82 c.innerWriter = nil 66 c.innerWriter = nil
83 } 67 }
84 68
85 conn, err := net.Dial(c.net, c.addr) 69 conn, err := net.Dial(c.Net, c.Addr)
86 if err != nil { 70 if err != nil {
87 return err 71 return err
88 } 72 }
...@@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error { ...@@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error {
97 } 81 }
98 82
99 func (c *ConnWriter) neddedConnectOnMsg() bool { 83 func (c *ConnWriter) neddedConnectOnMsg() bool {
100 if c.reconnect { 84 if c.Reconnect {
101 c.reconnect = false 85 c.Reconnect = false
102 return true 86 return true
103 } 87 }
104 88
...@@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool { ...@@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool {
106 return true 90 return true
107 } 91 }
108 92
109 return c.reconnectOnMsg 93 return c.ReconnectOnMsg
110 } 94 }
111 95
112 func init() { 96 func init() {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!