Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
9076ce7d
authored
2013-12-19 18:13:10 +0800
by
1fei
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Update conn.go
使用json直接初始化相关变量,代码更简单。如果把协议的首字母改大写会更简单,但不好改谢大的协议。
1 parent
0269a669
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
29 deletions
logs/conn.go
logs/conn.go
View file @
9076ce7
...
...
@@ -10,45 +10,29 @@ import (
type
ConnWriter
struct
{
lg
*
log
.
Logger
innerWriter
io
.
WriteCloser
reconnectOnMsg
bool
reconnect
bool
net
string
addr
string
level
int
ReconnectOnMsg
bool
`json:"reconnectOnMsg"`
Reconnect
bool
`json:"reconnect"`
Net
string
`json:"net"`
Addr
string
`json:"addr"`
Level
int
`json:"level"`
}
func
NewConn
()
LoggerInterface
{
conn
:=
new
(
ConnWriter
)
conn
.
l
evel
=
LevelTrace
conn
.
L
evel
=
LevelTrace
return
conn
}
func
(
c
*
ConnWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
c
)
if
err
!=
nil
{
return
err
}
if
rom
,
ok
:=
m
[
"reconnectOnMsg"
];
ok
{
c
.
reconnectOnMsg
=
rom
.
(
bool
)
}
if
rc
,
ok
:=
m
[
"reconnect"
];
ok
{
c
.
reconnect
=
rc
.
(
bool
)
}
if
nt
,
ok
:=
m
[
"net"
];
ok
{
c
.
net
=
nt
.
(
string
)
}
if
addr
,
ok
:=
m
[
"addr"
];
ok
{
c
.
addr
=
addr
.
(
string
)
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
c
.
level
=
int
(
lv
.
(
float64
))
}
return
nil
}
func
(
c
*
ConnWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
c
.
l
evel
{
if
level
<
c
.
L
evel
{
return
nil
}
if
c
.
neddedConnectOnMsg
()
{
...
...
@@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
}
}
if
c
.
r
econnectOnMsg
{
if
c
.
R
econnectOnMsg
{
defer
c
.
innerWriter
.
Close
()
}
c
.
lg
.
Println
(
msg
)
...
...
@@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error {
c
.
innerWriter
=
nil
}
conn
,
err
:=
net
.
Dial
(
c
.
net
,
c
.
a
ddr
)
conn
,
err
:=
net
.
Dial
(
c
.
Net
,
c
.
A
ddr
)
if
err
!=
nil
{
return
err
}
...
...
@@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error {
}
func
(
c
*
ConnWriter
)
neddedConnectOnMsg
()
bool
{
if
c
.
r
econnect
{
c
.
r
econnect
=
false
if
c
.
R
econnect
{
c
.
R
econnect
=
false
return
true
}
...
...
@@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool {
return
true
}
return
c
.
r
econnectOnMsg
return
c
.
R
econnectOnMsg
}
func
init
()
{
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment