Skip to content

Commit

Permalink
mod: 默认不输出到文件,需要指定-o filename
Browse files Browse the repository at this point in the history
  • Loading branch information
XinRoom committed Mar 17, 2023
1 parent f901b43 commit 6a62fc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 9 additions & 1 deletion cmd/go-portScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
netLive bool
maxOpenPort int
oCsv string
oFile string
)

func parseFlag(c *cli.Context) {
Expand All @@ -56,14 +57,15 @@ func parseFlag(c *cli.Context) {
netLive = c.Bool("netLive")
maxOpenPort = c.Int("maxOpenPort")
oCsv = c.String("oCsv")
oFile = c.String("oFile")
}

func run(c *cli.Context) error {
myLog := util.NewLogger("output.log", true)
if c.NumFlags() == 0 {
cli.ShowAppHelpAndExit(c, 0)
}
parseFlag(c)
myLog := util.NewLogger(oFile, true)
if devices {
if r, err := syn.GetAllDevs(); err != nil {
myLog.Fatal(err.Error())
Expand Down Expand Up @@ -430,6 +432,12 @@ func main() {
Usage: "output csv file",
Value: "",
},
&cli.StringFlag{
Name: "oFile",
Aliases: []string{"o"},
Usage: "output to file",
Value: "",
},
},
}

Expand Down
12 changes: 8 additions & 4 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (

func NewLogger(filename string, std bool) *log.Logger {
var out io.Writer
outFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if std {
out = io.MultiWriter(os.Stdout, outFile)
if filename == "" {
out = os.Stdout
} else {
out = outFile
outFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if std {
out = io.MultiWriter(os.Stdout, outFile)
} else {
out = outFile
}
}
logger := log.New(out, "", 0)
return logger
Expand Down

0 comments on commit 6a62fc3

Please sign in to comment.