-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
63 lines (54 loc) · 1.65 KB
/
log.go
File metadata and controls
63 lines (54 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"os"
"path"
"time"
)
func logLoop(interval time.Duration) {
for t := range time.Tick(1 * time.Second) {
logsleep(t, interval)
}
}
func logsleep(tick time.Time, interval time.Duration) {
printStatus("")
time.Sleep(interval)
}
func printStatus(announce string) {
percentage := math.Round(100 * float64(stats.completed) / float64(stats.total))
elapsed := math.Round(time.Since(stats.startTime).Minutes()*10) / 10
elapsedSec := float64(time.Since(stats.startTime).Seconds())
findRate := math.Round(float64(stats.found) / float64(elapsedSec))
pingRate := math.Round(float64(stats.pinged) / float64(elapsedSec))
completedRate := math.Round(float64(stats.completed) / float64(elapsedSec))
remaining := math.Round((float64(stats.total-stats.completed) / float64(completedRate))/6)/10
fmt.Printf("%v%% | Found: %v at %v/s | Pinged: %v at %v/s | Time: %vm, %vm rem | %v \n", percentage, stats.found, findRate, stats.pinged, pingRate, elapsed, remaining, announce)
}
var outputExists bool = false
func record(dataJSON formattedOutput) {
if !outputExists {
_, err := os.Stat(path.Dir(conf.outputPath))
if os.IsNotExist(err) {
os.Mkdir(path.Dir(conf.outputPath), 0750)
}
outputExists = true
}
dataBytes, _ := json.Marshal(dataJSON)
dataString := string(dataBytes)
f, err := os.OpenFile(conf.outputPath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
handleError("Could not open output file")
}
defer f.Close()
if _, err := f.WriteString(dataString + "\n"); err != nil {
log.Println(err)
}
}
func handleError(err string) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}