Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better logs #5

Merged
merged 3 commits into from
Jun 13, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat(logs): adapte client normal running logs to progress logs.
genshen committed Jun 13, 2019
commit bdcb904a84d9b04f427acc2f7764a01f678b0055
22 changes: 20 additions & 2 deletions wss/term_view/log.go
Original file line number Diff line number Diff line change
@@ -55,8 +55,13 @@ func (p *ProgressLog) Update(status Status) {
log.Fatal("bad connection size")
}
}

// update log
p.setLogBuffer() // call Writer.Write() to set log data into buffer
p.Writer.Flush(nil) // flush buffer
}

// update progress log.
func (p *ProgressLog) setLogBuffer() {
_, terminalRows := getTermSize()
// log size is ok for terminal (at least one row)
p.log.WithField("size", p.ConnSize).Trace("size of proxy connection(s).")
@@ -79,6 +84,19 @@ func (p *ProgressLog) Update(status Status) {
}
}
}
}

p.Writer.Flush()
// write buffer data directly to stdout.
func (p *ProgressLog) Write(buf [] byte) (int, error) {
p.mtx.Lock()
defer p.mtx.Unlock()

p.setLogBuffer() // call Writer.Write() to set log data into buffer
err := p.Writer.Flush(func() error { // flush buffer
if _, err := p.Writer.OutDev.Write(buf); err != nil { // just write buff to stdout, and keep progress log.
return err
}
return nil
})
return len(buf), err
}
7 changes: 6 additions & 1 deletion wss/term_view/writer.go
Original file line number Diff line number Diff line change
@@ -41,14 +41,19 @@ func (w *Writer) Write(buf []byte) (n int, err error) {
}

// Flush writes to the out and resets the buffer.
func (w *Writer) Flush() error {
func (w *Writer) Flush(onLinesCleared func() error) error {
w.mtx.Lock()
defer w.mtx.Unlock()

if len(w.buf.Bytes()) == 0 {
return nil
}
w.clearLines()
if onLinesCleared != nil {
if err := onLinesCleared(); err != nil { // callback if lines is cleared.
return err
}
}

lines := 0
var currentLine bytes.Buffer
5 changes: 4 additions & 1 deletion wss/wssocks.go
Original file line number Diff line number Diff line change
@@ -14,8 +14,11 @@ func ListenAndServe(wsc *WebSocketClient, tick *ticker.Ticker, address string) e
return err
}
log.WithField("local address", address).Info("listening on local address for incoming proxy request.")
var client Client

plog := term_view.NewPLog()
log.SetOutput(plog) // change log stdout to plog

var client Client
for {
c, err := s.Accept()
if err != nil {