move: log parser variadics to its own lecture
This commit is contained in:
@ -11,7 +11,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,15 +29,13 @@ func main() {
|
|||||||
|
|
||||||
// summarize summarizes and prints the parsing result
|
// summarize summarizes and prints the parsing result
|
||||||
func summarize(p *parser) {
|
func summarize(p *parser) {
|
||||||
sort.Strings(p.domains)
|
|
||||||
|
|
||||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||||
fmt.Println(strings.Repeat("-", 45))
|
fmt.Println(strings.Repeat("-", 45))
|
||||||
|
|
||||||
for _, domain := range p.domains {
|
loop(p, func(r result) {
|
||||||
fmt.Printf("%-30s %10d\n", domain, p.sum[domain].visits)
|
fmt.Printf("%-30s %10d\n", r.domain, r.visits)
|
||||||
}
|
})
|
||||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
fmt.Printf("\n%-30s %10d\n", "TOTAL", totalVisits(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
// dumpErrs simplifies handling multiple errors
|
// dumpErrs simplifies handling multiple errors
|
@ -9,6 +9,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -59,7 +60,7 @@ func parse(p *parser, line string) (r result) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// update updates the errors for the given parsing result
|
// update updates all the parsing results using the given parsing result
|
||||||
func update(p *parser, r result) {
|
func update(p *parser, r result) {
|
||||||
if p.lerr != nil {
|
if p.lerr != nil {
|
||||||
return
|
return
|
||||||
@ -84,3 +85,17 @@ func update(p *parser, r result) {
|
|||||||
func err(p *parser) error {
|
func err(p *parser) error {
|
||||||
return p.lerr
|
return p.lerr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loop allows a func to access all the copies of parsing results
|
||||||
|
func loop(p *parser, feed func(result)) {
|
||||||
|
sort.Strings(p.domains)
|
||||||
|
|
||||||
|
for _, domain := range p.domains {
|
||||||
|
feed(p.sum[domain])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// total returns the total visits
|
||||||
|
func totalVisits(p *parser) int {
|
||||||
|
return p.total
|
||||||
|
}
|
Reference in New Issue
Block a user