-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
064622c
commit 8184483
Showing
14 changed files
with
530 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,99 @@ | ||
package log | ||
|
||
import "github.com/v-byte-cpu/sx/pkg/scan/arp" | ||
import ( | ||
"bufio" | ||
"io" | ||
"time" | ||
|
||
"github.com/v-byte-cpu/sx/pkg/scan/arp" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Logger interface { | ||
Error(err error) | ||
LogResults(results <-chan *arp.ScanResult) | ||
} | ||
|
||
type FlushWriter interface { | ||
io.Writer | ||
Flush() error | ||
} | ||
|
||
type ResultWriter interface { | ||
Write(w io.Writer, result *arp.ScanResult) error | ||
} | ||
|
||
type logger struct { | ||
zapl *zap.Logger | ||
label string | ||
|
||
w io.Writer | ||
rw ResultWriter | ||
flushInterval time.Duration | ||
} | ||
|
||
type LoggerOption func(*logger) | ||
|
||
func JSON() LoggerOption { | ||
return func(l *logger) { | ||
l.rw = &JSONResultWriter{} | ||
} | ||
} | ||
|
||
func Plain() LoggerOption { | ||
return func(l *logger) { | ||
l.rw = &PlainResultWriter{} | ||
} | ||
} | ||
|
||
func FlushInterval(interval time.Duration) LoggerOption { | ||
return func(l *logger) { | ||
l.flushInterval = interval | ||
} | ||
} | ||
|
||
func NewLogger(w io.Writer, label string, opts ...LoggerOption) (Logger, error) { | ||
zapl, err := zap.NewProduction() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l := &logger{ | ||
zapl: zapl, | ||
label: label, | ||
rw: &PlainResultWriter{}, | ||
w: w, | ||
flushInterval: 1 * time.Second, | ||
} | ||
for _, o := range opts { | ||
o(l) | ||
} | ||
return l, nil | ||
} | ||
|
||
func (l *logger) Error(err error) { | ||
l.zapl.Error(l.label, zap.Error(err)) | ||
} | ||
|
||
func (l *logger) LogResults(results <-chan *arp.ScanResult) { | ||
bw := bufio.NewWriter(l.w) | ||
defer bw.Flush() | ||
var err error | ||
timec := time.After(l.flushInterval) | ||
for { | ||
select { | ||
case result, ok := <-results: | ||
if !ok { | ||
return | ||
} | ||
if err := l.rw.Write(l.w, result); err != nil { | ||
l.Error(err) | ||
} | ||
case <-timec: | ||
if err = bw.Flush(); err != nil { | ||
l.Error(err) | ||
} | ||
timec = time.After(l.flushInterval) | ||
} | ||
} | ||
} |
Oops, something went wrong.